public IEnumerable <AggregationResult> Modify(AggregationContext context, ITuple tuple, IEnumerable <IFact> facts) { var keys = new List <TKey>(); var resultLookup = new DefaultKeyMap <TKey, AggregationResult>(); var removedKeys = new HashSet <TKey>(); foreach (var fact in facts) { var key = (TKey)_keySelector.Invoke(context, tuple, fact); var element = (TElement)_elementSelector.Invoke(context, tuple, fact); var oldKey = _sourceToKey[fact]; var oldElement = _sourceToElement[fact]; _sourceToKey[fact] = key; _sourceToElement[fact] = element; if (Equals(key, oldKey)) { var result = Modify(fact, key, element); if (!resultLookup.ContainsKey(key)) { keys.Add(key); resultLookup[key] = result; } } else { var result1 = RemoveElementOnly(fact, oldKey, oldElement); if (!resultLookup.ContainsKey(oldKey)) { keys.Add(oldKey); } if (result1.Action == AggregationAction.Removed) { removedKeys.Add(oldKey); } resultLookup[oldKey] = result1; var result2 = Add(fact, key, element); if (!resultLookup.TryGetValue(key, out var previousResult)) { keys.Add(key); resultLookup[key] = result2; } else if (previousResult.Action == AggregationAction.Removed) { resultLookup[key] = result2; removedKeys.Remove(key); } } } foreach (var removedKey in removedKeys) { _groups.Remove(removedKey); } var results = GetResults(keys, resultLookup); return(results); }
public IEnumerable <AggregationResult> Modify(ITuple tuple, IEnumerable <IFact> facts) { var keys = new List <TKey>(); var resultLookup = new DefaultKeyMap <TKey, AggregationResult>(); foreach (var fact in facts) { var source = (TSource)fact.Value; var key = (TKey)_keySelector.Invoke(tuple, fact); var element = (TElement)_elementSelector.Invoke(tuple, fact); var oldKey = _sourceToKey[source]; var oldElement = _sourceToElement[source]; _sourceToKey[source] = key; _sourceToElement[source] = element; if (Equals(key, oldKey)) { var result = Modify(key, oldElement, element); if (!resultLookup.ContainsKey(key)) { keys.Add(key); resultLookup[key] = result; } } else { var result1 = Remove(oldKey, oldElement); if (!resultLookup.ContainsKey(oldKey)) { keys.Add(oldKey); } resultLookup[oldKey] = result1; var result2 = Add(key, element); AggregationResult previousResult; if (!resultLookup.TryGetValue(key, out previousResult)) { keys.Add(key); resultLookup[key] = result2; } else if (previousResult.Action == AggregationAction.Removed || result2.Action == AggregationAction.Added) { resultLookup[key] = AggregationResult.Modified(previousResult.Aggregate); } } } var results = GetResults(keys, resultLookup); return(results); }
public IEnumerable <AggregationResult> Add(ITuple tuple, IEnumerable <IFact> facts) { var keys = new List <TKey>(); var resultLookup = new DefaultKeyMap <TKey, AggregationResult>(); foreach (var fact in facts) { var key = (TKey)_keySelector.Invoke(tuple, fact); var element = (TElement)_elementSelector.Invoke(tuple, fact); _sourceToKey[fact] = key; _sourceToElement[fact] = element; var result = Add(fact, key, element); if (!resultLookup.ContainsKey(key)) { keys.Add(key); resultLookup[key] = result; } } var results = GetResults(keys, resultLookup); return(results); }
public IEnumerable <AggregationResult> Remove(ITuple tuple, IEnumerable <IFact> facts) { var keys = new List <TKey>(); var resultLookup = new DefaultKeyMap <TKey, AggregationResult>(); foreach (var fact in facts) { var oldKey = _sourceToKey[fact]; var oldElement = _sourceToElement[fact]; _sourceToKey.Remove(fact); _sourceToElement.Remove(fact); var result = Remove(fact, oldKey, oldElement); if (!resultLookup.ContainsKey(oldKey)) { keys.Add(oldKey); } resultLookup[oldKey] = result; } var results = GetResults(keys, resultLookup); return(results); }
private static IEnumerable <AggregationResult> GetResults(IEnumerable <TKey> keys, DefaultKeyMap <TKey, AggregationResult> lookup) { var results = new List <AggregationResult>(); foreach (var key in keys) { var result = lookup[key]; results.Add(result); } return(results); }