Zip.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static IAsyncEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector)
  14. {
  15. if (first == null)
  16. throw new ArgumentNullException(nameof(first));
  17. if (second == null)
  18. throw new ArgumentNullException(nameof(second));
  19. if (selector == null)
  20. throw new ArgumentNullException(nameof(selector));
  21. return new ZipAsyncIterator<TFirst, TSecond, TResult>(first, second, selector);
  22. }
  23. private sealed class ZipAsyncIterator<TFirst, TSecond, TResult> : AsyncIterator<TResult>
  24. {
  25. private readonly IAsyncEnumerable<TFirst> first;
  26. private readonly IAsyncEnumerable<TSecond> second;
  27. private readonly Func<TFirst, TSecond, TResult> selector;
  28. private IAsyncEnumerator<TFirst> firstEnumerator;
  29. private IAsyncEnumerator<TSecond> secondEnumerator;
  30. public ZipAsyncIterator(IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector)
  31. {
  32. this.first = first;
  33. this.second = second;
  34. this.selector = selector;
  35. }
  36. public override AsyncIterator<TResult> Clone()
  37. {
  38. return new ZipAsyncIterator<TFirst, TSecond, TResult>(first, second, selector);
  39. }
  40. public override void Dispose()
  41. {
  42. if (firstEnumerator != null)
  43. {
  44. firstEnumerator.Dispose();
  45. firstEnumerator = null;
  46. }
  47. if (secondEnumerator != null)
  48. {
  49. secondEnumerator.Dispose();
  50. secondEnumerator = null;
  51. }
  52. base.Dispose();
  53. }
  54. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  55. {
  56. switch (state)
  57. {
  58. case AsyncIteratorState.Allocated:
  59. firstEnumerator = first.GetEnumerator();
  60. secondEnumerator = second.GetEnumerator();
  61. state = AsyncIteratorState.Iterating;
  62. goto case AsyncIteratorState.Iterating;
  63. case AsyncIteratorState.Iterating:
  64. // We kick these off and join so they can potentially run in parallel
  65. var ft = firstEnumerator.MoveNext(cancellationToken);
  66. var st = secondEnumerator.MoveNext(cancellationToken);
  67. await Task.WhenAll(ft, st)
  68. .ConfigureAwait(false);
  69. if (ft.Result && st.Result)
  70. {
  71. current = selector(firstEnumerator.Current, secondEnumerator.Current);
  72. return true;
  73. }
  74. Dispose();
  75. break;
  76. }
  77. return false;
  78. }
  79. }
  80. }
  81. }