Merge.cs 4.8 KB

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