Zip.cs 3.7 KB

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