Merge.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 AsyncEnumerableEx
  11. {
  12. public static IAsyncEnumerable<TSource> Merge<TSource>(params IAsyncEnumerable<TSource>[] sources)
  13. {
  14. if (sources == null)
  15. throw Error.ArgumentNull(nameof(sources));
  16. return new MergeAsyncIterator<TSource>(sources);
  17. }
  18. public static IAsyncEnumerable<TSource> Merge<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  19. {
  20. if (sources == null)
  21. throw Error.ArgumentNull(nameof(sources));
  22. return sources.ToAsyncEnumerable().SelectMany(source => source);
  23. }
  24. public static IAsyncEnumerable<TSource> Merge<TSource>(this IAsyncEnumerable<IAsyncEnumerable<TSource>> sources)
  25. {
  26. if (sources == null)
  27. throw Error.ArgumentNull(nameof(sources));
  28. return sources.SelectMany(source => source);
  29. }
  30. private sealed class MergeAsyncIterator<TSource> : AsyncIterator<TSource>
  31. {
  32. private readonly IAsyncEnumerable<TSource>[] _sources;
  33. private IAsyncEnumerator<TSource>[] _enumerators;
  34. private ValueTask<bool>[] _moveNexts;
  35. private int _active;
  36. public MergeAsyncIterator(IAsyncEnumerable<TSource>[] sources)
  37. {
  38. Debug.Assert(sources != null);
  39. _sources = sources;
  40. }
  41. public override AsyncIterator<TSource> Clone()
  42. {
  43. return new MergeAsyncIterator<TSource>(_sources);
  44. }
  45. public override async ValueTask DisposeAsync()
  46. {
  47. if (_enumerators != null)
  48. {
  49. var n = _enumerators.Length;
  50. var disposes = new ValueTask[n];
  51. for (var i = 0; i < n; i++)
  52. {
  53. var dispose = _enumerators[i].DisposeAsync();
  54. disposes[i] = dispose;
  55. }
  56. await Task.WhenAll(disposes.Select(t => t.AsTask())).ConfigureAwait(false);
  57. _enumerators = null;
  58. }
  59. await base.DisposeAsync().ConfigureAwait(false);
  60. }
  61. protected override async ValueTask<bool> MoveNextCore()
  62. {
  63. switch (state)
  64. {
  65. case AsyncIteratorState.Allocated:
  66. var n = _sources.Length;
  67. _enumerators = new IAsyncEnumerator<TSource>[n];
  68. _moveNexts = new ValueTask<bool>[n];
  69. _active = n;
  70. for (var i = 0; i < n; i++)
  71. {
  72. var enumerator = _sources[i].GetAsyncEnumerator(cancellationToken);
  73. _enumerators[i] = enumerator;
  74. _moveNexts[i] = enumerator.MoveNextAsync();
  75. }
  76. state = AsyncIteratorState.Iterating;
  77. goto case AsyncIteratorState.Iterating;
  78. case AsyncIteratorState.Iterating:
  79. while (_active > 0)
  80. {
  81. //
  82. // REVIEW: This approach does have a bias towards giving sources on the left
  83. // priority over sources on the right when yielding values. We may
  84. // want to consider a "prefer fairness" option.
  85. //
  86. var moveNext = await Task.WhenAny(_moveNexts.Select(t => t.AsTask())).ConfigureAwait(false);
  87. var index = Array.IndexOf(_moveNexts, moveNext);
  88. if (!await moveNext.ConfigureAwait(false))
  89. {
  90. _moveNexts[index] = TaskExt.Never;
  91. _active--;
  92. }
  93. else
  94. {
  95. var enumerator = _enumerators[index];
  96. current = enumerator.Current;
  97. _moveNexts[index] = enumerator.MoveNextAsync();
  98. return true;
  99. }
  100. }
  101. break;
  102. }
  103. await DisposeAsync().ConfigureAwait(false);
  104. return false;
  105. }
  106. }
  107. }
  108. }