Union.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 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. internal readonly IEqualityComparer<TSource>? _comparer;
  50. private IAsyncEnumerator<TSource>? _enumerator;
  51. private Set<TSource>? _set;
  52. private int _index;
  53. protected UnionAsyncIterator(IEqualityComparer<TSource>? comparer)
  54. {
  55. _comparer = comparer;
  56. }
  57. public sealed override async ValueTask DisposeAsync()
  58. {
  59. if (_enumerator != null)
  60. {
  61. await _enumerator.DisposeAsync().ConfigureAwait(false);
  62. _enumerator = null;
  63. _set = null;
  64. }
  65. await base.DisposeAsync().ConfigureAwait(false);
  66. }
  67. internal abstract IAsyncEnumerable<TSource>? GetEnumerable(int index);
  68. internal abstract UnionAsyncIterator<TSource> Union(IAsyncEnumerable<TSource> next);
  69. private async Task SetEnumeratorAsync(IAsyncEnumerator<TSource> enumerator)
  70. {
  71. if (_enumerator != null)
  72. {
  73. await _enumerator.DisposeAsync().ConfigureAwait(false);
  74. }
  75. _enumerator = enumerator;
  76. }
  77. private void StoreFirst()
  78. {
  79. var set = new Set<TSource>(_comparer);
  80. var element = _enumerator!.Current;
  81. set.Add(element);
  82. _current = element;
  83. _set = set;
  84. }
  85. private async ValueTask<bool> GetNextAsync()
  86. {
  87. var set = _set;
  88. Debug.Assert(set != null);
  89. while (await _enumerator!.MoveNextAsync().ConfigureAwait(false))
  90. {
  91. var element = _enumerator.Current;
  92. if (set!.Add(element))
  93. {
  94. _current = element;
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. protected sealed override async ValueTask<bool> MoveNextCore()
  101. {
  102. switch (_state)
  103. {
  104. case AsyncIteratorState.Allocated:
  105. _index = 0;
  106. for (var enumerable = GetEnumerable(0); enumerable != null; enumerable = GetEnumerable(_index))
  107. {
  108. ++_index;
  109. var enumerator = enumerable.GetAsyncEnumerator(_cancellationToken);
  110. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  111. {
  112. await SetEnumeratorAsync(enumerator).ConfigureAwait(false);
  113. StoreFirst();
  114. _state = AsyncIteratorState.Iterating;
  115. return true;
  116. }
  117. }
  118. break;
  119. case AsyncIteratorState.Iterating:
  120. while (true)
  121. {
  122. if (await GetNextAsync().ConfigureAwait(false))
  123. {
  124. return true;
  125. }
  126. var enumerable = GetEnumerable(_index);
  127. if (enumerable == null)
  128. {
  129. break;
  130. }
  131. await SetEnumeratorAsync(enumerable.GetAsyncEnumerator(_cancellationToken)).ConfigureAwait(false);
  132. ++_index;
  133. }
  134. break;
  135. }
  136. await DisposeAsync().ConfigureAwait(false);
  137. return false;
  138. }
  139. private async Task<Set<TSource>> FillSetAsync(CancellationToken cancellationToken)
  140. {
  141. cancellationToken.ThrowIfCancellationRequested();
  142. var set = new Set<TSource>(_comparer);
  143. for (var index = 0; ; ++index)
  144. {
  145. var enumerable = GetEnumerable(index);
  146. if (enumerable == null)
  147. {
  148. return set;
  149. }
  150. await foreach (var item in enumerable.WithCancellation(cancellationToken).ConfigureAwait(false))
  151. {
  152. set.Add(item);
  153. }
  154. }
  155. }
  156. public async ValueTask<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  157. {
  158. var set = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  159. return set.ToArray();
  160. }
  161. public async ValueTask<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  162. {
  163. var set = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  164. return set.ToList();
  165. }
  166. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  167. {
  168. if (onlyIfCheap)
  169. {
  170. return new ValueTask<int>(-1);
  171. }
  172. return Core();
  173. async ValueTask<int> Core()
  174. {
  175. var set = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  176. return set.Count;
  177. }
  178. }
  179. }
  180. /// <summary>
  181. /// An iterator that yields distinct values from two <see cref="IAsyncEnumerable{TSource}"/>.
  182. /// </summary>
  183. /// <typeparam name="TSource">The type of the source enumerables.</typeparam>
  184. private sealed class UnionAsyncIterator2<TSource> : UnionAsyncIterator<TSource>
  185. {
  186. private readonly IAsyncEnumerable<TSource> _first;
  187. private readonly IAsyncEnumerable<TSource> _second;
  188. public UnionAsyncIterator2(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource>? comparer)
  189. : base(comparer)
  190. {
  191. _first = first;
  192. _second = second;
  193. }
  194. public override AsyncIteratorBase<TSource> Clone() => new UnionAsyncIterator2<TSource>(_first, _second, _comparer);
  195. internal override IAsyncEnumerable<TSource>? GetEnumerable(int index)
  196. {
  197. Debug.Assert(index >= 0 && index <= 2);
  198. return index switch
  199. {
  200. 0 => _first,
  201. 1 => _second,
  202. _ => null,
  203. };
  204. }
  205. internal override UnionAsyncIterator<TSource> Union(IAsyncEnumerable<TSource> next)
  206. {
  207. var sources = new SingleLinkedNode<IAsyncEnumerable<TSource>>(_first).Add(_second).Add(next);
  208. return new UnionAsyncIteratorN<TSource>(sources, 2, _comparer);
  209. }
  210. }
  211. /// <summary>
  212. /// An iterator that yields distinct values from three or more <see cref="IAsyncEnumerable{TSource}"/>.
  213. /// </summary>
  214. /// <typeparam name="TSource">The type of the source enumerables.</typeparam>
  215. private sealed class UnionAsyncIteratorN<TSource> : UnionAsyncIterator<TSource>
  216. {
  217. private readonly SingleLinkedNode<IAsyncEnumerable<TSource>> _sources;
  218. private readonly int _headIndex;
  219. public UnionAsyncIteratorN(SingleLinkedNode<IAsyncEnumerable<TSource>> sources, int headIndex, IEqualityComparer<TSource>? comparer)
  220. : base(comparer)
  221. {
  222. Debug.Assert(headIndex >= 2);
  223. Debug.Assert(sources.GetCount() == headIndex + 1);
  224. _sources = sources;
  225. _headIndex = headIndex;
  226. }
  227. public override AsyncIteratorBase<TSource> Clone() => new UnionAsyncIteratorN<TSource>(_sources, _headIndex, _comparer);
  228. internal override IAsyncEnumerable<TSource>? GetEnumerable(int index) => index > _headIndex ? null : _sources.GetNode(_headIndex - index).Item;
  229. internal override UnionAsyncIterator<TSource> Union(IAsyncEnumerable<TSource> next)
  230. {
  231. if (_headIndex == int.MaxValue - 2)
  232. {
  233. // In the unlikely case of this many unions, if we produced a UnionIteratorN
  234. // with int.MaxValue then state would overflow before it matched it's index.
  235. // So we use the naïve approach of just having a left and right sequence.
  236. return new UnionAsyncIterator2<TSource>(this, next, _comparer);
  237. }
  238. return new UnionAsyncIteratorN<TSource>(_sources.Add(next), _headIndex + 1, _comparer);
  239. }
  240. }
  241. }
  242. }