Buffer.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 AsyncEnumerable
  11. {
  12. public static IAsyncEnumerable<IList<TSource>> Buffer<TSource>(this IAsyncEnumerable<TSource> source, int count)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. if (count <= 0)
  17. throw new ArgumentOutOfRangeException(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 new ArgumentNullException(nameof(source));
  24. if (count <= 0)
  25. throw new ArgumentOutOfRangeException(nameof(count));
  26. if (skip <= 0)
  27. throw new ArgumentOutOfRangeException(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. this.source = source;
  43. this.count = count;
  44. this.skip = skip;
  45. }
  46. public override AsyncIterator<IList<TSource>> Clone()
  47. {
  48. return new BufferAsyncIterator<TSource>(source, count, skip);
  49. }
  50. public override void Dispose()
  51. {
  52. if (enumerator != null)
  53. {
  54. enumerator.Dispose();
  55. enumerator = null;
  56. }
  57. buffers = null;
  58. base.Dispose();
  59. }
  60. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  61. {
  62. switch (state)
  63. {
  64. case AsyncIteratorState.Allocated:
  65. enumerator = source.GetEnumerator();
  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.MoveNext(cancellationToken)
  77. .ConfigureAwait(false))
  78. {
  79. var item = enumerator.Current;
  80. if (index++ % skip == 0)
  81. {
  82. buffers.Enqueue(new List<TSource>(count));
  83. }
  84. foreach (var buffer in buffers)
  85. {
  86. buffer.Add(item);
  87. }
  88. if (buffers.Count > 0 && buffers.Peek()
  89. .Count == count)
  90. {
  91. current = buffers.Dequeue();
  92. return true;
  93. }
  94. continue; // loop
  95. }
  96. stopped = true;
  97. enumerator.Dispose();
  98. enumerator = null;
  99. continue; // loop
  100. }
  101. if (buffers.Count > 0)
  102. {
  103. current = buffers.Dequeue();
  104. return true;
  105. }
  106. break; // exit the while
  107. }
  108. break; // case
  109. }
  110. Dispose();
  111. return false;
  112. }
  113. }
  114. }
  115. }