Union.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerable
  11. {
  12. /// <summary>
  13. /// Produces the set union of two sequences by using the default equality comparer.
  14. /// </summary>
  15. /// <typeparam name="TSource">The type of the elements of the input sequences.</typeparam>
  16. /// <param name="first">An async-enumerable sequence whose distinct elements form the first set for the union.</param>
  17. /// <param name="second">An async-enumerable sequence whose distinct elements form the second set for the union.</param>
  18. /// <returns>An async-enumerable sequence that contains the elements from both input sequences, excluding duplicates.</returns>
  19. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> is null.</exception>
  20. public static IAsyncEnumerable<TSource> Union<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second) =>
  21. Union(first, second, comparer: null);
  22. /// <summary>
  23. /// Produces the set union of two sequences by using a specified equality comparer.
  24. /// </summary>
  25. /// <typeparam name="TSource">The type of the elements of the input sequences.</typeparam>
  26. /// <param name="first">An async-enumerable sequence whose distinct elements form the first set for the union.</param>
  27. /// <param name="second">An async-enumerable sequence whose distinct elements form the second set for the union.</param>
  28. /// <param name="comparer">The equality comparer to compare values.</param>
  29. /// <returns>An async-enumerable sequence that contains the elements from both input sequences, excluding duplicates.</returns>
  30. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> is null.</exception>
  31. public static IAsyncEnumerable<TSource> Union<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource>? comparer)
  32. {
  33. if (first == null)
  34. throw Error.ArgumentNull(nameof(first));
  35. if (second == null)
  36. throw Error.ArgumentNull(nameof(second));
  37. return first is UnionAsyncIterator<TSource> union && AreEqualityComparersEqual(comparer, union._comparer) ? union.Union(second) : new UnionAsyncIterator2<TSource>(first, second, comparer);
  38. }
  39. private static bool AreEqualityComparersEqual<TSource>(IEqualityComparer<TSource>? first, IEqualityComparer<TSource>? second)
  40. {
  41. return first == second || (first != null && second != null && first.Equals(second));
  42. }
  43. /// <summary>
  44. /// An iterator that yields distinct values from two or more <see cref="IAsyncEnumerable{TSource}"/>.
  45. /// </summary>
  46. /// <typeparam name="TSource">The type of the source enumerables.</typeparam>
  47. private abstract class UnionAsyncIterator<TSource> : AsyncIterator<TSource>, IAsyncIListProvider<TSource>
  48. {
  49. #pragma warning disable IDE1006 // Naming Styles
  50. internal readonly IEqualityComparer<TSource>? _comparer;
  51. #pragma warning restore IDE1006 // Naming Styles
  52. private IAsyncEnumerator<TSource>? _enumerator;
  53. private Set<TSource>? _set;
  54. private int _index;
  55. protected UnionAsyncIterator(IEqualityComparer<TSource>? comparer)
  56. {
  57. _comparer = comparer;
  58. }
  59. public sealed override async ValueTask DisposeAsync()
  60. {
  61. if (_enumerator != null)
  62. {
  63. await _enumerator.DisposeAsync().ConfigureAwait(false);
  64. _enumerator = null;
  65. _set = null;
  66. }
  67. await base.DisposeAsync().ConfigureAwait(false);
  68. }
  69. internal abstract IAsyncEnumerable<TSource>? GetEnumerable(int index);
  70. internal abstract UnionAsyncIterator<TSource> Union(IAsyncEnumerable<TSource> next);
  71. private async Task SetEnumeratorAsync(IAsyncEnumerator<TSource> enumerator)
  72. {
  73. if (_enumerator != null)
  74. {
  75. await _enumerator.DisposeAsync().ConfigureAwait(false);
  76. }
  77. _enumerator = enumerator;
  78. }
  79. private void StoreFirst()
  80. {
  81. var set = new Set<TSource>(_comparer);
  82. var element = _enumerator!.Current;
  83. set.Add(element);
  84. _current = element;
  85. _set = set;
  86. }
  87. private async ValueTask<bool> GetNextAsync()
  88. {
  89. var set = _set;
  90. Debug.Assert(set != null);
  91. while (await _enumerator!.MoveNextAsync().ConfigureAwait(false))
  92. {
  93. var element = _enumerator.Current;
  94. if (set!.Add(element))
  95. {
  96. _current = element;
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. protected sealed override async ValueTask<bool> MoveNextCore()
  103. {
  104. switch (_state)
  105. {
  106. case AsyncIteratorState.Allocated:
  107. _index = 0;
  108. for (var enumerable = GetEnumerable(0); enumerable != null; enumerable = GetEnumerable(_index))
  109. {
  110. ++_index;
  111. var enumerator = enumerable.GetAsyncEnumerator(_cancellationToken);
  112. await SetEnumeratorAsync(enumerator).ConfigureAwait(false);
  113. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  114. {
  115. StoreFirst();
  116. _state = AsyncIteratorState.Iterating;
  117. return true;
  118. }
  119. }
  120. break;
  121. case AsyncIteratorState.Iterating:
  122. while (true)
  123. {
  124. if (await GetNextAsync().ConfigureAwait(false))
  125. {
  126. return true;
  127. }
  128. var enumerable = GetEnumerable(_index);
  129. if (enumerable == null)
  130. {
  131. break;
  132. }
  133. await SetEnumeratorAsync(enumerable.GetAsyncEnumerator(_cancellationToken)).ConfigureAwait(false);
  134. ++_index;
  135. }
  136. break;
  137. }
  138. await DisposeAsync().ConfigureAwait(false);
  139. return false;
  140. }
  141. private async Task<Set<TSource>> FillSetAsync(CancellationToken cancellationToken)
  142. {
  143. cancellationToken.ThrowIfCancellationRequested();
  144. var set = new Set<TSource>(_comparer);
  145. for (var index = 0; ; ++index)
  146. {
  147. var enumerable = GetEnumerable(index);
  148. if (enumerable == null)
  149. {
  150. return set;
  151. }
  152. await foreach (var item in enumerable.WithCancellation(cancellationToken).ConfigureAwait(false))
  153. {
  154. set.Add(item);
  155. }
  156. }
  157. }
  158. public async ValueTask<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  159. {
  160. var set = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  161. return set.ToArray();
  162. }
  163. public async ValueTask<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  164. {
  165. var set = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  166. return set.ToList();
  167. }
  168. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  169. {
  170. if (onlyIfCheap)
  171. {
  172. return new ValueTask<int>(-1);
  173. }
  174. return Core();
  175. async ValueTask<int> Core()
  176. {
  177. var set = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  178. return set.Count;
  179. }
  180. }
  181. }
  182. /// <summary>
  183. /// An iterator that yields distinct values from two <see cref="IAsyncEnumerable{TSource}"/>.
  184. /// </summary>
  185. /// <typeparam name="TSource">The type of the source enumerables.</typeparam>
  186. private sealed class UnionAsyncIterator2<TSource> : UnionAsyncIterator<TSource>
  187. {
  188. private readonly IAsyncEnumerable<TSource> _first;
  189. private readonly IAsyncEnumerable<TSource> _second;
  190. public UnionAsyncIterator2(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource>? comparer)
  191. : base(comparer)
  192. {
  193. _first = first;
  194. _second = second;
  195. }
  196. public override AsyncIteratorBase<TSource> Clone() => new UnionAsyncIterator2<TSource>(_first, _second, _comparer);
  197. internal override IAsyncEnumerable<TSource>? GetEnumerable(int index)
  198. {
  199. Debug.Assert(index >= 0 && index <= 2);
  200. return index switch
  201. {
  202. 0 => _first,
  203. 1 => _second,
  204. _ => null,
  205. };
  206. }
  207. internal override UnionAsyncIterator<TSource> Union(IAsyncEnumerable<TSource> next)
  208. {
  209. var sources = new SingleLinkedNode<IAsyncEnumerable<TSource>>(_first).Add(_second).Add(next);
  210. return new UnionAsyncIteratorN<TSource>(sources, 2, _comparer);
  211. }
  212. }
  213. /// <summary>
  214. /// An iterator that yields distinct values from three or more <see cref="IAsyncEnumerable{TSource}"/>.
  215. /// </summary>
  216. /// <typeparam name="TSource">The type of the source enumerables.</typeparam>
  217. private sealed class UnionAsyncIteratorN<TSource> : UnionAsyncIterator<TSource>
  218. {
  219. private readonly SingleLinkedNode<IAsyncEnumerable<TSource>> _sources;
  220. private readonly int _headIndex;
  221. public UnionAsyncIteratorN(SingleLinkedNode<IAsyncEnumerable<TSource>> sources, int headIndex, IEqualityComparer<TSource>? comparer)
  222. : base(comparer)
  223. {
  224. Debug.Assert(headIndex >= 2);
  225. Debug.Assert(sources.GetCount() == headIndex + 1);
  226. _sources = sources;
  227. _headIndex = headIndex;
  228. }
  229. public override AsyncIteratorBase<TSource> Clone() => new UnionAsyncIteratorN<TSource>(_sources, _headIndex, _comparer);
  230. internal override IAsyncEnumerable<TSource>? GetEnumerable(int index) => index > _headIndex ? null : _sources.GetNode(_headIndex - index).Item;
  231. internal override UnionAsyncIterator<TSource> Union(IAsyncEnumerable<TSource> next)
  232. {
  233. if (_headIndex == int.MaxValue - 2)
  234. {
  235. // In the unlikely case of this many unions, if we produced a UnionIteratorN
  236. // with int.MaxValue then state would overflow before it matched it's index.
  237. // So we use the naïve approach of just having a left and right sequence.
  238. return new UnionAsyncIterator2<TSource>(this, next, _comparer);
  239. }
  240. return new UnionAsyncIteratorN<TSource>(_sources.Add(next), _headIndex + 1, _comparer);
  241. }
  242. }
  243. }
  244. }