Buffer.cs 4.9 KB

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