Buffer.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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<IList<TSource>> Buffer<TSource>(this IAsyncEnumerable<TSource> source, int count)
  13. {
  14. if (source == null)
  15. throw Error.ArgumentNull(nameof(source));
  16. if (count <= 0)
  17. throw Error.ArgumentOutOfRange(nameof(count));
  18. #if USE_ASYNC_ITERATOR
  19. return AsyncEnumerable.Create(Core);
  20. async IAsyncEnumerator<IList<TSource>> Core(CancellationToken cancellationToken)
  21. {
  22. var buffer = new List<TSource>(count);
  23. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  24. {
  25. buffer.Add(item);
  26. if (buffer.Count == count)
  27. {
  28. yield return buffer;
  29. buffer = new List<TSource>(count);
  30. }
  31. }
  32. if (buffer.Count > 0)
  33. {
  34. yield return buffer;
  35. }
  36. }
  37. #else
  38. return new BufferAsyncIterator<TSource>(source, count, count);
  39. #endif
  40. }
  41. public static IAsyncEnumerable<IList<TSource>> Buffer<TSource>(this IAsyncEnumerable<TSource> source, int count, int skip)
  42. {
  43. if (source == null)
  44. throw Error.ArgumentNull(nameof(source));
  45. if (count <= 0)
  46. throw Error.ArgumentOutOfRange(nameof(count));
  47. if (skip <= 0)
  48. throw Error.ArgumentOutOfRange(nameof(skip));
  49. #if USE_ASYNC_ITERATOR
  50. return AsyncEnumerable.Create(Core);
  51. async IAsyncEnumerator<IList<TSource>> Core(CancellationToken cancellationToken)
  52. {
  53. var buffers = new Queue<IList<TSource>>();
  54. var index = 0;
  55. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  56. {
  57. if (index++ % skip == 0)
  58. {
  59. buffers.Enqueue(new List<TSource>(count));
  60. }
  61. foreach (var buffer in buffers)
  62. {
  63. buffer.Add(item);
  64. }
  65. if (buffers.Count > 0 && buffers.Peek().Count == count)
  66. {
  67. yield return buffers.Dequeue();
  68. }
  69. }
  70. while (buffers.Count > 0)
  71. {
  72. yield return buffers.Dequeue();
  73. }
  74. }
  75. #else
  76. return new BufferAsyncIterator<TSource>(source, count, skip);
  77. #endif
  78. }
  79. #if !USE_ASYNC_ITERATOR
  80. private sealed class BufferAsyncIterator<TSource> : AsyncIterator<IList<TSource>>
  81. {
  82. private readonly int _count;
  83. private readonly int _skip;
  84. private readonly IAsyncEnumerable<TSource> _source;
  85. private Queue<IList<TSource>> _buffers;
  86. private IAsyncEnumerator<TSource> _enumerator;
  87. private int _index;
  88. private bool _stopped;
  89. public BufferAsyncIterator(IAsyncEnumerable<TSource> source, int count, int skip)
  90. {
  91. Debug.Assert(source != null);
  92. _source = source;
  93. _count = count;
  94. _skip = skip;
  95. }
  96. public override AsyncIteratorBase<IList<TSource>> Clone()
  97. {
  98. return new BufferAsyncIterator<TSource>(_source, _count, _skip);
  99. }
  100. public override async ValueTask DisposeAsync()
  101. {
  102. if (_enumerator != null)
  103. {
  104. await _enumerator.DisposeAsync().ConfigureAwait(false);
  105. _enumerator = null;
  106. }
  107. _buffers = null;
  108. await base.DisposeAsync().ConfigureAwait(false);
  109. }
  110. protected override async ValueTask<bool> MoveNextCore()
  111. {
  112. switch (_state)
  113. {
  114. case AsyncIteratorState.Allocated:
  115. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  116. _buffers = new Queue<IList<TSource>>();
  117. _index = 0;
  118. _stopped = false;
  119. _state = AsyncIteratorState.Iterating;
  120. goto case AsyncIteratorState.Iterating;
  121. case AsyncIteratorState.Iterating:
  122. while (true)
  123. {
  124. if (!_stopped)
  125. {
  126. if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  127. {
  128. var item = _enumerator.Current;
  129. if (_index++ % _skip == 0)
  130. {
  131. _buffers.Enqueue(new List<TSource>(_count));
  132. }
  133. foreach (var buffer in _buffers)
  134. {
  135. buffer.Add(item);
  136. }
  137. if (_buffers.Count > 0 && _buffers.Peek().Count == _count)
  138. {
  139. _current = _buffers.Dequeue();
  140. return true;
  141. }
  142. continue; // loop
  143. }
  144. _stopped = true;
  145. await _enumerator.DisposeAsync().ConfigureAwait(false);
  146. _enumerator = null;
  147. continue; // loop
  148. }
  149. if (_buffers.Count > 0)
  150. {
  151. _current = _buffers.Dequeue();
  152. return true;
  153. }
  154. break; // exit the while
  155. }
  156. break; // case
  157. }
  158. await DisposeAsync().ConfigureAwait(false);
  159. return false;
  160. }
  161. }
  162. #endif
  163. }
  164. }