Concat.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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> Concat<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  12. {
  13. if (sources == null)
  14. throw new ArgumentNullException(nameof(sources));
  15. return ConcatCore(sources);
  16. }
  17. public static IAsyncEnumerable<TSource> Concat<TSource>(params IAsyncEnumerable<TSource>[] sources)
  18. {
  19. if (sources == null)
  20. throw new ArgumentNullException(nameof(sources));
  21. return ConcatCore(sources);
  22. }
  23. private static IAsyncEnumerable<TSource> ConcatCore<TSource>(IEnumerable<IAsyncEnumerable<TSource>> sources)
  24. {
  25. return new ConcatEnumerableAsyncIterator<TSource>(sources);
  26. }
  27. private sealed class ConcatEnumerableAsyncIterator<TSource> : AsyncIterator<TSource>
  28. {
  29. private readonly IEnumerable<IAsyncEnumerable<TSource>> source;
  30. public ConcatEnumerableAsyncIterator(IEnumerable<IAsyncEnumerable<TSource>> source)
  31. {
  32. Debug.Assert(source != null);
  33. this.source = source;
  34. }
  35. public override AsyncIterator<TSource> Clone()
  36. {
  37. return new ConcatEnumerableAsyncIterator<TSource>(source);
  38. }
  39. public override async Task DisposeAsync()
  40. {
  41. if (outerEnumerator != null)
  42. {
  43. outerEnumerator.Dispose();
  44. outerEnumerator = null;
  45. }
  46. if (currentEnumerator != null)
  47. {
  48. await currentEnumerator.DisposeAsync().ConfigureAwait(false);
  49. currentEnumerator = null;
  50. }
  51. await base.DisposeAsync().ConfigureAwait(false);
  52. }
  53. // State machine vars
  54. private IEnumerator<IAsyncEnumerable<TSource>> outerEnumerator;
  55. private IAsyncEnumerator<TSource> currentEnumerator;
  56. private int mode;
  57. private const int State_OuterNext = 1;
  58. private const int State_While = 4;
  59. protected override async Task<bool> MoveNextCore()
  60. {
  61. switch (state)
  62. {
  63. case AsyncIteratorState.Allocated:
  64. outerEnumerator = source.GetEnumerator();
  65. mode = State_OuterNext;
  66. state = AsyncIteratorState.Iterating;
  67. goto case AsyncIteratorState.Iterating;
  68. case AsyncIteratorState.Iterating:
  69. switch (mode)
  70. {
  71. case State_OuterNext:
  72. if (outerEnumerator.MoveNext())
  73. {
  74. // make sure we dispose the previous one if we're about to replace it
  75. if (currentEnumerator != null)
  76. {
  77. await currentEnumerator.DisposeAsync().ConfigureAwait(false);
  78. }
  79. currentEnumerator = outerEnumerator.Current.GetAsyncEnumerator();
  80. mode = State_While;
  81. goto case State_While;
  82. }
  83. break;
  84. case State_While:
  85. if (await currentEnumerator.MoveNextAsync().ConfigureAwait(false))
  86. {
  87. current = currentEnumerator.Current;
  88. return true;
  89. }
  90. // No more on the inner enumerator, move to the next outer
  91. goto case State_OuterNext;
  92. }
  93. await DisposeAsync().ConfigureAwait(false);
  94. break;
  95. }
  96. return false;
  97. }
  98. }
  99. }
  100. }