mirror of https://github.com/ErsatzTV/ErsatzTV.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.4 KiB
48 lines
1.4 KiB
using System.Collections; |
|
|
|
namespace ErsatzTV.Core.Extensions; |
|
|
|
public static class IEnumerableExtensions |
|
{ |
|
public static IEnumerable<IGrouping<TKey, TSource>> GroupConsecutiveBy<TSource, TKey>( |
|
this IEnumerable<TSource> source, |
|
Func<TSource, TKey> keySelector) |
|
{ |
|
EqualityComparer<TKey> comparer = EqualityComparer<TKey>.Default; |
|
|
|
List<TSource> currentGroup = null; |
|
|
|
foreach (TSource item in source) |
|
{ |
|
TKey key = keySelector(item); |
|
|
|
if (currentGroup == null) |
|
{ |
|
currentGroup = [item]; |
|
continue; |
|
} |
|
|
|
if (comparer.Equals(keySelector(currentGroup[0]), key)) |
|
{ |
|
currentGroup.Add(item); |
|
} |
|
else |
|
{ |
|
yield return new Grouping<TKey, TSource>(keySelector(currentGroup[0]), currentGroup); |
|
currentGroup = [item]; |
|
} |
|
} |
|
|
|
if (currentGroup != null) |
|
{ |
|
yield return new Grouping<TKey, TSource>(keySelector(currentGroup[0]), currentGroup); |
|
} |
|
} |
|
|
|
private class Grouping<TKey, TElement>(TKey key, IEnumerable<TElement> elements) : IGrouping<TKey, TElement> |
|
{ |
|
public TKey Key { get; } = key; |
|
public IEnumerator<TElement> GetEnumerator() => elements.GetEnumerator(); |
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
|
} |
|
}
|
|
|