Buffer.cs 4.9 KB

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