Union.cs 12 KB

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