123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.ExceptionServices;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Linq
- {
- public static partial class AsyncEnumerable
- {
- public static IAsyncEnumerable<IAsyncGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (keySelector == null)
- throw new ArgumentNullException(nameof(keySelector));
- if (elementSelector == null)
- throw new ArgumentNullException(nameof(elementSelector));
- if (comparer == null)
- throw new ArgumentNullException(nameof(comparer));
- return CreateEnumerable(() =>
- {
- var gate = new object();
- var e = source.GetEnumerator();
- var count = 1;
- var map = new Dictionary<TKey, AsyncGrouping<TKey, TElement>>(comparer);
- var list = new List<IAsyncGrouping<TKey, TElement>>();
- var index = 0;
- var current = default(IAsyncGrouping<TKey, TElement>);
- var faulted = default(ExceptionDispatchInfo);
- var res = default(bool?);
- var cts = new CancellationTokenDisposable();
- var refCount = new Disposable(
- () =>
- {
- if (Interlocked.Decrement(ref count) == 0)
- e.Dispose();
- }
- );
- var d = Disposable.Create(cts, refCount);
- var iterateSource = default(Func<CancellationToken, Task<bool>>);
- iterateSource = async ct =>
- {
- lock (gate)
- {
- if (res != null)
- {
- return res.Value;
- }
- res = null;
- }
- faulted?.Throw();
- try
- {
- res = await e.MoveNext(ct)
- .ConfigureAwait(false);
- if (res == true)
- {
- var key = default(TKey);
- var element = default(TElement);
- var cur = e.Current;
- try
- {
- key = keySelector(cur);
- element = elementSelector(cur);
- }
- catch (Exception exception)
- {
- foreach (var v in map.Values)
- v.Error(exception);
- throw;
- }
- var group = default(AsyncGrouping<TKey, TElement>);
- if (!map.TryGetValue(key, out group))
- {
- group = new AsyncGrouping<TKey, TElement>(key, iterateSource, refCount);
- map.Add(key, group);
- lock (list)
- list.Add(group);
- Interlocked.Increment(ref count);
- }
- group.Add(element);
- }
- return res.Value;
- }
- catch (Exception ex)
- {
- foreach (var v in map.Values)
- v.Error(ex);
- faulted = ExceptionDispatchInfo.Capture(ex);
- throw;
- }
- finally
- {
- res = null;
- }
- };
- var f = default(Func<CancellationToken, Task<bool>>);
- f = async ct =>
- {
- var result = await iterateSource(ct)
- .ConfigureAwait(false);
- current = null;
- lock (list)
- {
- if (index < list.Count)
- current = list[index++];
- }
- if (current != null)
- {
- return true;
- }
- return result && await f(ct)
- .ConfigureAwait(false);
- };
- return CreateEnumerator(
- f,
- () => current,
- d.Dispose,
- e
- );
- });
- }
- public static IAsyncEnumerable<IAsyncGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (keySelector == null)
- throw new ArgumentNullException(nameof(keySelector));
- if (elementSelector == null)
- throw new ArgumentNullException(nameof(elementSelector));
- return source.GroupBy(keySelector, elementSelector, EqualityComparer<TKey>.Default);
- }
- public static IAsyncEnumerable<IAsyncGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (keySelector == null)
- throw new ArgumentNullException(nameof(keySelector));
- if (comparer == null)
- throw new ArgumentNullException(nameof(comparer));
- return source.GroupBy(keySelector, x => x, comparer);
- }
- public static IAsyncEnumerable<IAsyncGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (keySelector == null)
- throw new ArgumentNullException(nameof(keySelector));
- return source.GroupBy(keySelector, x => x, EqualityComparer<TKey>.Default);
- }
- public static IAsyncEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IAsyncEnumerable<TElement>, TResult> resultSelector, IEqualityComparer<TKey> comparer)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (keySelector == null)
- throw new ArgumentNullException(nameof(keySelector));
- if (elementSelector == null)
- throw new ArgumentNullException(nameof(elementSelector));
- if (resultSelector == null)
- throw new ArgumentNullException(nameof(resultSelector));
- if (comparer == null)
- throw new ArgumentNullException(nameof(comparer));
- return source.GroupBy(keySelector, elementSelector, comparer)
- .Select(g => resultSelector(g.Key, g));
- }
- public static IAsyncEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IAsyncEnumerable<TElement>, TResult> resultSelector)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (keySelector == null)
- throw new ArgumentNullException(nameof(keySelector));
- if (elementSelector == null)
- throw new ArgumentNullException(nameof(elementSelector));
- if (resultSelector == null)
- throw new ArgumentNullException(nameof(resultSelector));
- return source.GroupBy(keySelector, elementSelector, EqualityComparer<TKey>.Default)
- .Select(g => resultSelector(g.Key, g));
- }
- public static IAsyncEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IAsyncEnumerable<TSource>, TResult> resultSelector, IEqualityComparer<TKey> comparer)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (keySelector == null)
- throw new ArgumentNullException(nameof(keySelector));
- if (resultSelector == null)
- throw new ArgumentNullException(nameof(resultSelector));
- if (comparer == null)
- throw new ArgumentNullException(nameof(comparer));
- return source.GroupBy(keySelector, x => x, comparer)
- .Select(g => resultSelector(g.Key, g));
- }
- public static IAsyncEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IAsyncEnumerable<TSource>, TResult> resultSelector)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (keySelector == null)
- throw new ArgumentNullException(nameof(keySelector));
- if (resultSelector == null)
- throw new ArgumentNullException(nameof(resultSelector));
- return source.GroupBy(keySelector, x => x, EqualityComparer<TKey>.Default)
- .Select(g => resultSelector(g.Key, g));
- }
- private static IEnumerable<IGrouping<TKey, TElement>> GroupUntil<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IComparer<TKey> comparer)
- {
- var group = default(EnumerableGrouping<TKey, TElement>);
- foreach (var x in source)
- {
- var key = keySelector(x);
- if (group == null || comparer.Compare(group.Key, key) != 0)
- {
- group = new EnumerableGrouping<TKey, TElement>(key);
- yield return group;
- }
- group.Add(elementSelector(x));
- }
- }
- private class AsyncGrouping<TKey, TElement> : IAsyncGrouping<TKey, TElement>
- {
- private readonly List<TElement> elements = new List<TElement>();
- private readonly Func<CancellationToken, Task<bool>> iterateSource;
- private readonly IDisposable sourceDisposable;
- private bool done;
- private ExceptionDispatchInfo exception;
- public AsyncGrouping(TKey key, Func<CancellationToken, Task<bool>> iterateSource, IDisposable sourceDisposable)
- {
- this.iterateSource = iterateSource;
- this.sourceDisposable = sourceDisposable;
- Key = key;
- }
- public TKey Key { get; }
- public IAsyncEnumerator<TElement> GetEnumerator()
- {
- var index = -1;
- var cts = new CancellationTokenDisposable();
- var d = Disposable.Create(cts, sourceDisposable);
- var f = default(Func<CancellationToken, Task<bool>>);
- f = async ct =>
- {
- var size = 0;
- lock (elements)
- size = elements.Count;
- if (index < size)
- {
- return true;
- }
- if (done)
- {
- exception?.Throw();
- return false;
- }
- if (await iterateSource(ct)
- .ConfigureAwait(false))
- {
- return await f(ct)
- .ConfigureAwait(false);
- }
- return false;
- };
- return CreateEnumerator(
- ct =>
- {
- ++index;
- return f(cts.Token);
- },
- () => elements[index],
- d.Dispose,
- null
- );
- }
- public void Add(TElement element)
- {
- lock (elements)
- elements.Add(element);
- }
- public void Error(Exception exception)
- {
- done = true;
- this.exception = ExceptionDispatchInfo.Capture(exception);
- }
- }
- }
- }
- // Note: The type here has to be internal as System.Linq has it's own public copy we're not using
- namespace System.Linq.Internal
- {
- /// Adapted from System.Linq.Grouping from .NET Framework
- /// Source: https://github.com/dotnet/corefx/blob/b90532bc97b07234a7d18073819d019645285f1c/src/System.Linq/src/System/Linq/Grouping.cs#L64
- internal class Grouping<TKey, TElement> : IGrouping<TKey, TElement>, IList<TElement>
- {
- internal int _count;
- internal TElement[] _elements;
- internal int _hashCode;
- internal Grouping<TKey, TElement> _hashNext;
- internal TKey _key;
- internal Grouping<TKey, TElement> _next;
- public IEnumerator<TElement> GetEnumerator()
- {
- for (var i = 0; i < _count; i++)
- {
- yield return _elements[i];
- }
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- // DDB195907: implement IGrouping<>.Key implicitly
- // so that WPF binding works on this property.
- public TKey Key
- {
- get { return _key; }
- }
- int ICollection<TElement>.Count
- {
- get { return _count; }
- }
- bool ICollection<TElement>.IsReadOnly
- {
- get { return true; }
- }
- void ICollection<TElement>.Add(TElement item)
- {
- throw new NotSupportedException(Strings.NOT_SUPPORTED);
- }
- void ICollection<TElement>.Clear()
- {
- throw new NotSupportedException(Strings.NOT_SUPPORTED);
- }
- bool ICollection<TElement>.Contains(TElement item)
- {
- return Array.IndexOf(_elements, item, 0, _count) >= 0;
- }
- void ICollection<TElement>.CopyTo(TElement[] array, int arrayIndex)
- {
- Array.Copy(_elements, 0, array, arrayIndex, _count);
- }
- bool ICollection<TElement>.Remove(TElement item)
- {
- throw new NotSupportedException(Strings.NOT_SUPPORTED);
- }
- int IList<TElement>.IndexOf(TElement item)
- {
- return Array.IndexOf(_elements, item, 0, _count);
- }
- void IList<TElement>.Insert(int index, TElement item)
- {
- throw new NotSupportedException(Strings.NOT_SUPPORTED);
- }
- void IList<TElement>.RemoveAt(int index)
- {
- throw new NotSupportedException(Strings.NOT_SUPPORTED);
- }
- TElement IList<TElement>.this[int index]
- {
- get
- {
- if (index < 0 || index >= _count)
- {
- throw new ArgumentOutOfRangeException(nameof(index));
- }
- return _elements[index];
- }
- set { throw new NotSupportedException(Strings.NOT_SUPPORTED); }
- }
- internal void Add(TElement element)
- {
- if (_elements.Length == _count)
- {
- Array.Resize(ref _elements, checked(_count*2));
- }
- _elements[_count] = element;
- _count++;
- }
- internal void Trim()
- {
- if (_elements.Length != _count)
- {
- Array.Resize(ref _elements, _count);
- }
- }
- }
- }
|