Zip.cs 14 KB

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