Zip.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. public static IAsyncEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector)
  13. {
  14. if (first == null)
  15. throw Error.ArgumentNull(nameof(first));
  16. if (second == null)
  17. throw Error.ArgumentNull(nameof(second));
  18. if (selector == null)
  19. throw Error.ArgumentNull(nameof(selector));
  20. return new ZipAsyncIterator<TFirst, TSecond, TResult>(first, second, selector);
  21. }
  22. public static IAsyncEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, ValueTask<TResult>> selector)
  23. {
  24. if (first == null)
  25. throw Error.ArgumentNull(nameof(first));
  26. if (second == null)
  27. throw Error.ArgumentNull(nameof(second));
  28. if (selector == null)
  29. throw Error.ArgumentNull(nameof(selector));
  30. return new ZipAsyncIteratorWithTask<TFirst, TSecond, TResult>(first, second, selector);
  31. }
  32. #if !NO_DEEP_CANCELLATION
  33. public static IAsyncEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, CancellationToken, ValueTask<TResult>> selector)
  34. {
  35. if (first == null)
  36. throw Error.ArgumentNull(nameof(first));
  37. if (second == null)
  38. throw Error.ArgumentNull(nameof(second));
  39. if (selector == null)
  40. throw Error.ArgumentNull(nameof(selector));
  41. return new ZipAsyncIteratorWithTaskAndCancellation<TFirst, TSecond, TResult>(first, second, selector);
  42. }
  43. #endif
  44. private sealed class ZipAsyncIterator<TFirst, TSecond, TResult> : AsyncIterator<TResult>
  45. {
  46. private readonly IAsyncEnumerable<TFirst> _first;
  47. private readonly IAsyncEnumerable<TSecond> _second;
  48. private readonly Func<TFirst, TSecond, TResult> _selector;
  49. private IAsyncEnumerator<TFirst> _firstEnumerator;
  50. private IAsyncEnumerator<TSecond> _secondEnumerator;
  51. public ZipAsyncIterator(IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector)
  52. {
  53. Debug.Assert(first != null);
  54. Debug.Assert(second != null);
  55. Debug.Assert(selector != null);
  56. _first = first;
  57. _second = second;
  58. _selector = selector;
  59. }
  60. public override AsyncIteratorBase<TResult> Clone()
  61. {
  62. return new ZipAsyncIterator<TFirst, TSecond, TResult>(_first, _second, _selector);
  63. }
  64. public override async ValueTask DisposeAsync()
  65. {
  66. if (_secondEnumerator != null)
  67. {
  68. await _secondEnumerator.DisposeAsync().ConfigureAwait(false);
  69. _secondEnumerator = null;
  70. }
  71. if (_firstEnumerator != null)
  72. {
  73. await _firstEnumerator.DisposeAsync().ConfigureAwait(false);
  74. _firstEnumerator = null;
  75. }
  76. await base.DisposeAsync().ConfigureAwait(false);
  77. }
  78. protected override async ValueTask<bool> MoveNextCore()
  79. {
  80. // REVIEW: Earlier versions of this operator performed concurrent MoveNextAsync calls, which isn't a great default and
  81. // results in an unexpected source of concurrency. However, a concurrent Zip may be a worthy addition to the
  82. // API or System.Interactive.Async as a complementary implementation besides the conservative default.
  83. switch (_state)
  84. {
  85. case AsyncIteratorState.Allocated:
  86. _firstEnumerator = _first.GetAsyncEnumerator(_cancellationToken);
  87. _secondEnumerator = _second.GetAsyncEnumerator(_cancellationToken);
  88. _state = AsyncIteratorState.Iterating;
  89. goto case AsyncIteratorState.Iterating;
  90. case AsyncIteratorState.Iterating:
  91. if (await _firstEnumerator.MoveNextAsync().ConfigureAwait(false) && await _secondEnumerator.MoveNextAsync().ConfigureAwait(false))
  92. {
  93. _current = _selector(_firstEnumerator.Current, _secondEnumerator.Current);
  94. return true;
  95. }
  96. await DisposeAsync().ConfigureAwait(false);
  97. break;
  98. }
  99. return false;
  100. }
  101. }
  102. private sealed class ZipAsyncIteratorWithTask<TFirst, TSecond, TResult> : AsyncIterator<TResult>
  103. {
  104. private readonly IAsyncEnumerable<TFirst> _first;
  105. private readonly IAsyncEnumerable<TSecond> _second;
  106. private readonly Func<TFirst, TSecond, ValueTask<TResult>> _selector;
  107. private IAsyncEnumerator<TFirst> _firstEnumerator;
  108. private IAsyncEnumerator<TSecond> _secondEnumerator;
  109. public ZipAsyncIteratorWithTask(IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, ValueTask<TResult>> selector)
  110. {
  111. Debug.Assert(first != null);
  112. Debug.Assert(second != null);
  113. Debug.Assert(selector != null);
  114. _first = first;
  115. _second = second;
  116. _selector = selector;
  117. }
  118. public override AsyncIteratorBase<TResult> Clone()
  119. {
  120. return new ZipAsyncIteratorWithTask<TFirst, TSecond, TResult>(_first, _second, _selector);
  121. }
  122. public override async ValueTask DisposeAsync()
  123. {
  124. if (_secondEnumerator != null)
  125. {
  126. await _secondEnumerator.DisposeAsync().ConfigureAwait(false);
  127. _secondEnumerator = null;
  128. }
  129. if (_firstEnumerator != null)
  130. {
  131. await _firstEnumerator.DisposeAsync().ConfigureAwait(false);
  132. _firstEnumerator = null;
  133. }
  134. await base.DisposeAsync().ConfigureAwait(false);
  135. }
  136. protected override async ValueTask<bool> MoveNextCore()
  137. {
  138. // REVIEW: Earlier versions of this operator performed concurrent MoveNextAsync calls, which isn't a great default and
  139. // results in an unexpected source of concurrency. However, a concurrent Zip may be a worthy addition to the
  140. // API or System.Interactive.Async as a complementary implementation besides the conservative default.
  141. switch (_state)
  142. {
  143. case AsyncIteratorState.Allocated:
  144. _firstEnumerator = _first.GetAsyncEnumerator(_cancellationToken);
  145. _secondEnumerator = _second.GetAsyncEnumerator(_cancellationToken);
  146. _state = AsyncIteratorState.Iterating;
  147. goto case AsyncIteratorState.Iterating;
  148. case AsyncIteratorState.Iterating:
  149. if (await _firstEnumerator.MoveNextAsync().ConfigureAwait(false) && await _secondEnumerator.MoveNextAsync().ConfigureAwait(false))
  150. {
  151. _current = await _selector(_firstEnumerator.Current, _secondEnumerator.Current).ConfigureAwait(false);
  152. return true;
  153. }
  154. await DisposeAsync().ConfigureAwait(false);
  155. break;
  156. }
  157. return false;
  158. }
  159. }
  160. #if !NO_DEEP_CANCELLATION
  161. private sealed class ZipAsyncIteratorWithTaskAndCancellation<TFirst, TSecond, TResult> : AsyncIterator<TResult>
  162. {
  163. private readonly IAsyncEnumerable<TFirst> _first;
  164. private readonly IAsyncEnumerable<TSecond> _second;
  165. private readonly Func<TFirst, TSecond, CancellationToken, ValueTask<TResult>> _selector;
  166. private IAsyncEnumerator<TFirst> _firstEnumerator;
  167. private IAsyncEnumerator<TSecond> _secondEnumerator;
  168. public ZipAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, CancellationToken, ValueTask<TResult>> selector)
  169. {
  170. Debug.Assert(first != null);
  171. Debug.Assert(second != null);
  172. Debug.Assert(selector != null);
  173. _first = first;
  174. _second = second;
  175. _selector = selector;
  176. }
  177. public override AsyncIteratorBase<TResult> Clone()
  178. {
  179. return new ZipAsyncIteratorWithTaskAndCancellation<TFirst, TSecond, TResult>(_first, _second, _selector);
  180. }
  181. public override async ValueTask DisposeAsync()
  182. {
  183. if (_secondEnumerator != null)
  184. {
  185. await _secondEnumerator.DisposeAsync().ConfigureAwait(false);
  186. _secondEnumerator = null;
  187. }
  188. if (_firstEnumerator != null)
  189. {
  190. await _firstEnumerator.DisposeAsync().ConfigureAwait(false);
  191. _firstEnumerator = null;
  192. }
  193. await base.DisposeAsync().ConfigureAwait(false);
  194. }
  195. protected override async ValueTask<bool> MoveNextCore()
  196. {
  197. // REVIEW: Earlier versions of this operator performed concurrent MoveNextAsync calls, which isn't a great default and
  198. // results in an unexpected source of concurrency. However, a concurrent Zip may be a worthy addition to the
  199. // API or System.Interactive.Async as a complementary implementation besides the conservative default.
  200. switch (_state)
  201. {
  202. case AsyncIteratorState.Allocated:
  203. _firstEnumerator = _first.GetAsyncEnumerator(_cancellationToken);
  204. _secondEnumerator = _second.GetAsyncEnumerator(_cancellationToken);
  205. _state = AsyncIteratorState.Iterating;
  206. goto case AsyncIteratorState.Iterating;
  207. case AsyncIteratorState.Iterating:
  208. if (await _firstEnumerator.MoveNextAsync().ConfigureAwait(false) && await _secondEnumerator.MoveNextAsync().ConfigureAwait(false))
  209. {
  210. _current = await _selector(_firstEnumerator.Current, _secondEnumerator.Current, _cancellationToken).ConfigureAwait(false);
  211. return true;
  212. }
  213. await DisposeAsync().ConfigureAwait(false);
  214. break;
  215. }
  216. return false;
  217. }
  218. }
  219. #endif
  220. }
  221. }