Skip to content

Commit

Permalink
Remove look-up optimizations in "CountBy"/"ScanBy" (#855)
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz authored Oct 26, 2022
1 parent 212be62 commit af98ba4
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 25 deletions.
11 changes: 1 addition & 10 deletions MoreLinq/CountBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,12 @@ void Loop(IEqualityComparer<TKey> cmp)

keys = new List<TKey>();
counts = new List<int>();
(bool, TKey) prevKey = default;
var index = 0;

foreach (var item in source)
{
var key = keySelector(item);

if (// key same as the previous? then re-use the index
prevKey is (true, {} pk)
&& cmp.GetHashCode(pk) == cmp.GetHashCode(key)
&& cmp.Equals(pk, key)
// otherwise try & find index of the key
|| dic.TryGetValue(key, out index))
if (dic.TryGetValue(key, out var index))
{
counts[index]++;
}
Expand All @@ -105,8 +98,6 @@ prevKey is (true, {} pk)
keys.Add(key);
counts.Add(1);
}

prevKey = (true, key);
}
}
}
Expand Down
16 changes: 1 addition & 15 deletions MoreLinq/ScanBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,13 @@ IEnumerable<KeyValuePair<TKey, TState>> _(IEqualityComparer<TKey> comparer)
{
var stateByKey = new Collections.Dictionary<TKey, TState>(comparer);

(bool, TKey, TState) prev = default;

foreach (var item in source)
{
var key = keySelector(item);

var state = // key same as the previous? then re-use the state
prev is (true, {} pk, {} ps)
&& comparer.GetHashCode(pk) == comparer.GetHashCode(key)
&& comparer.Equals(pk, key) ? ps
: // otherwise try & find state of the key
stateByKey.TryGetValue(key, out var ns) ? ns
: seedSelector(key);

var state = stateByKey.TryGetValue(key, out var s) ? s : seedSelector(key);
state = accumulator(state, key, item);

stateByKey[key] = state;

yield return new KeyValuePair<TKey, TState>(key, state);

prev = (true, key, state);
}
}
}
Expand Down

0 comments on commit af98ba4

Please sign in to comment.