Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Enumerable.AggregateBy #109110

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 29 additions & 55 deletions src/libraries/System.Linq/src/System/Linq/AggregateBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,26 @@ public static IEnumerable<KeyValuePair<TKey, TAccumulate>> AggregateBy<TSource,
return [];
}

return AggregateByIterator(source, keySelector, seed, func, keyComparer);
using IEnumerator<TSource> enumerator = source.GetEnumerator();

if (!enumerator.MoveNext())
{
return [];
}

Dictionary<TKey, TAccumulate> dict = new(keyComparer);

do
{
TSource value = enumerator.Current;
TKey key = keySelector(value);

ref TAccumulate? acc = ref CollectionsMarshal.GetValueRefOrAddDefault(dict, key, out bool exists);
acc = func(exists ? acc! : seed, value);
}
while (enumerator.MoveNext());

return dict;
}

/// <summary>
Expand Down Expand Up @@ -97,71 +116,26 @@ public static IEnumerable<KeyValuePair<TKey, TAccumulate>> AggregateBy<TSource,
return [];
}

return AggregateByIterator(source, keySelector, seedSelector, func, keyComparer);
}

private static IEnumerable<KeyValuePair<TKey, TAccumulate>> AggregateByIterator<TSource, TKey, TAccumulate>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, IEqualityComparer<TKey>? keyComparer) where TKey : notnull
{
using IEnumerator<TSource> enumerator = source.GetEnumerator();

if (!enumerator.MoveNext())
{
yield break;
}

foreach (KeyValuePair<TKey, TAccumulate> countBy in PopulateDictionary(enumerator, keySelector, seed, func, keyComparer))
{
yield return countBy;
}

static Dictionary<TKey, TAccumulate> PopulateDictionary(IEnumerator<TSource> enumerator, Func<TSource, TKey> keySelector, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, IEqualityComparer<TKey>? keyComparer)
{
Dictionary<TKey, TAccumulate> dict = new(keyComparer);

do
{
TSource value = enumerator.Current;
TKey key = keySelector(value);

ref TAccumulate? acc = ref CollectionsMarshal.GetValueRefOrAddDefault(dict, key, out bool exists);
acc = func(exists ? acc! : seed, value);
}
while (enumerator.MoveNext());

return dict;
return [];
}
}

private static IEnumerable<KeyValuePair<TKey, TAccumulate>> AggregateByIterator<TSource, TKey, TAccumulate>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TAccumulate> seedSelector, Func<TAccumulate, TSource, TAccumulate> func, IEqualityComparer<TKey>? keyComparer) where TKey : notnull
{
using IEnumerator<TSource> enumerator = source.GetEnumerator();
Dictionary<TKey, TAccumulate> dict = new(keyComparer);

if (!enumerator.MoveNext())
do
{
yield break;
}
TSource value = enumerator.Current;
TKey key = keySelector(value);

foreach (KeyValuePair<TKey, TAccumulate> countBy in PopulateDictionary(enumerator, keySelector, seedSelector, func, keyComparer))
{
yield return countBy;
ref TAccumulate? acc = ref CollectionsMarshal.GetValueRefOrAddDefault(dict, key, out bool exists);
acc = func(exists ? acc! : seedSelector(key), value);
}
while (enumerator.MoveNext());

static Dictionary<TKey, TAccumulate> PopulateDictionary(IEnumerator<TSource> enumerator, Func<TSource, TKey> keySelector, Func<TKey, TAccumulate> seedSelector, Func<TAccumulate, TSource, TAccumulate> func, IEqualityComparer<TKey>? keyComparer)
{
Dictionary<TKey, TAccumulate> dict = new(keyComparer);

do
{
TSource value = enumerator.Current;
TKey key = keySelector(value);

ref TAccumulate? acc = ref CollectionsMarshal.GetValueRefOrAddDefault(dict, key, out bool exists);
acc = func(exists ? acc! : seedSelector(key), value);
}
while (enumerator.MoveNext());

return dict;
}
return dict;
}
}
}
15 changes: 1 addition & 14 deletions src/libraries/System.Linq/src/System/Linq/CountBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,13 @@ public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this I
return [];
}

return CountByIterator(source, keySelector, keyComparer);
}

private static IEnumerable<KeyValuePair<TKey, int>> CountByIterator<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? keyComparer) where TKey : notnull
{
using IEnumerator<TSource> enumerator = source.GetEnumerator();

if (!enumerator.MoveNext())
{
yield break;
}

foreach (KeyValuePair<TKey, int> countBy in BuildCountDictionary(enumerator, keySelector, keyComparer))
{
yield return countBy;
return [];
}
}

private static Dictionary<TKey, int> BuildCountDictionary<TSource, TKey>(IEnumerator<TSource> enumerator, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? keyComparer) where TKey : notnull
{
Dictionary<TKey, int> countsBy = new(keyComparer);

do
Expand Down
Loading