Buffer.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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<IList<TSource>> Buffer<TSource>(this IAsyncEnumerable<TSource> source, int count)
  12. {
  13. if (source == null)
  14. {
  15. throw new ArgumentNullException(nameof(source));
  16. }
  17. if (count <= 0)
  18. {
  19. throw new ArgumentOutOfRangeException(nameof(count));
  20. }
  21. return new BufferAsyncIterator<TSource>(source, count, count);
  22. }
  23. public static IAsyncEnumerable<IList<TSource>> Buffer<TSource>(this IAsyncEnumerable<TSource> source, int count, int skip)
  24. {
  25. if (source == null)
  26. {
  27. throw new ArgumentNullException(nameof(source));
  28. }
  29. if (count <= 0)
  30. {
  31. throw new ArgumentOutOfRangeException(nameof(count));
  32. }
  33. if (skip <= 0)
  34. {
  35. throw new ArgumentOutOfRangeException(nameof(skip));
  36. }
  37. return new BufferAsyncIterator<TSource>(source, count, skip);
  38. }
  39. private sealed class BufferAsyncIterator<TSource> : AsyncIterator<IList<TSource>>
  40. {
  41. private readonly int count;
  42. private readonly int skip;
  43. private readonly IAsyncEnumerable<TSource> source;
  44. private Queue<IList<TSource>> buffers;
  45. private IAsyncEnumerator<TSource> enumerator;
  46. private int index;
  47. private bool stopped;
  48. public BufferAsyncIterator(IAsyncEnumerable<TSource> source, int count, int skip)
  49. {
  50. Debug.Assert(source != null);
  51. this.source = source;
  52. this.count = count;
  53. this.skip = skip;
  54. }
  55. public override AsyncIterator<IList<TSource>> Clone()
  56. {
  57. return new BufferAsyncIterator<TSource>(source, count, skip);
  58. }
  59. public override async ValueTask DisposeAsync()
  60. {
  61. if (enumerator != null)
  62. {
  63. await enumerator.DisposeAsync().ConfigureAwait(false);
  64. enumerator = null;
  65. }
  66. buffers = null;
  67. await base.DisposeAsync().ConfigureAwait(false);
  68. }
  69. protected override async ValueTask<bool> MoveNextCore()
  70. {
  71. switch (state)
  72. {
  73. case AsyncIteratorState.Allocated:
  74. enumerator = source.GetAsyncEnumerator();
  75. buffers = new Queue<IList<TSource>>();
  76. index = 0;
  77. stopped = false;
  78. state = AsyncIteratorState.Iterating;
  79. goto case AsyncIteratorState.Iterating;
  80. case AsyncIteratorState.Iterating:
  81. while (true)
  82. {
  83. if (!stopped)
  84. {
  85. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  86. {
  87. var item = enumerator.Current;
  88. if (index++ % skip == 0)
  89. {
  90. buffers.Enqueue(new List<TSource>(count));
  91. }
  92. foreach (var buffer in buffers)
  93. {
  94. buffer.Add(item);
  95. }
  96. if (buffers.Count > 0 && buffers.Peek().Count == count)
  97. {
  98. current = buffers.Dequeue();
  99. return true;
  100. }
  101. continue; // loop
  102. }
  103. stopped = true;
  104. await enumerator.DisposeAsync().ConfigureAwait(false);
  105. enumerator = null;
  106. continue; // loop
  107. }
  108. if (buffers.Count > 0)
  109. {
  110. current = buffers.Dequeue();
  111. return true;
  112. }
  113. break; // exit the while
  114. }
  115. break; // case
  116. }
  117. await DisposeAsync().ConfigureAwait(false);
  118. return false;
  119. }
  120. }
  121. }
  122. }