Buffer.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerableEx
  10. {
  11. /// <summary>
  12. /// Projects each element of an async-enumerable sequence into consecutive non-overlapping buffers which are produced based on element count information.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence, and in the lists in the result sequence.</typeparam>
  15. /// <param name="source">Source sequence to produce buffers over.</param>
  16. /// <param name="count">Length of each buffer.</param>
  17. /// <returns>An async-enumerable sequence of buffers.</returns>
  18. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  19. /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than or equal to zero.</exception>
  20. public static IAsyncEnumerable<IList<TSource>> Buffer<TSource>(this IAsyncEnumerable<TSource> source, int count)
  21. {
  22. if (source == null)
  23. throw Error.ArgumentNull(nameof(source));
  24. if (count <= 0)
  25. throw Error.ArgumentOutOfRange(nameof(count));
  26. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  27. return Core(source, count);
  28. static async IAsyncEnumerable<IList<TSource>> Core(IAsyncEnumerable<TSource> source, int count, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  29. #else
  30. return AsyncEnumerable.Create(Core);
  31. async IAsyncEnumerator<IList<TSource>> Core(CancellationToken cancellationToken)
  32. #endif
  33. {
  34. var buffer = new List<TSource>(count);
  35. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  36. {
  37. buffer.Add(item);
  38. if (buffer.Count == count)
  39. {
  40. yield return buffer;
  41. buffer = new List<TSource>(count);
  42. }
  43. }
  44. if (buffer.Count > 0)
  45. {
  46. yield return buffer;
  47. }
  48. }
  49. }
  50. /// <summary>
  51. /// Projects each element of an async-enumerable sequence into zero or more buffers which are produced based on element count information.
  52. /// </summary>
  53. /// <typeparam name="TSource">The type of the elements in the source sequence, and in the lists in the result sequence.</typeparam>
  54. /// <param name="source">Source sequence to produce buffers over.</param>
  55. /// <param name="count">Length of each buffer.</param>
  56. /// <param name="skip">Number of elements to skip between creation of consecutive buffers.</param>
  57. /// <returns>An async-enumerable sequence of buffers.</returns>
  58. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  59. /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> or <paramref name="skip"/> is less than or equal to zero.</exception>
  60. public static IAsyncEnumerable<IList<TSource>> Buffer<TSource>(this IAsyncEnumerable<TSource> source, int count, int skip)
  61. {
  62. if (source == null)
  63. throw Error.ArgumentNull(nameof(source));
  64. if (count <= 0)
  65. throw Error.ArgumentOutOfRange(nameof(count));
  66. if (skip <= 0)
  67. throw Error.ArgumentOutOfRange(nameof(skip));
  68. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  69. return Core(source, count, skip);
  70. static async IAsyncEnumerable<IList<TSource>> Core(IAsyncEnumerable<TSource> source, int count, int skip, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  71. #else
  72. return AsyncEnumerable.Create(Core);
  73. async IAsyncEnumerator<IList<TSource>> Core(CancellationToken cancellationToken)
  74. #endif
  75. {
  76. var buffers = new Queue<IList<TSource>>();
  77. var index = 0;
  78. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  79. {
  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().Count == count)
  89. {
  90. yield return buffers.Dequeue();
  91. }
  92. }
  93. while (buffers.Count > 0)
  94. {
  95. yield return buffers.Dequeue();
  96. }
  97. }
  98. }
  99. }
  100. }