Zip.cs 3.8 KB

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