Buffer.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. return Core(source, count);
  27. static async IAsyncEnumerable<IList<TSource>> Core(IAsyncEnumerable<TSource> source, int count, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  28. {
  29. var buffer = new List<TSource>(count);
  30. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  31. {
  32. buffer.Add(item);
  33. if (buffer.Count == count)
  34. {
  35. yield return buffer;
  36. buffer = new List<TSource>(count);
  37. }
  38. }
  39. if (buffer.Count > 0)
  40. {
  41. yield return buffer;
  42. }
  43. }
  44. }
  45. /// <summary>
  46. /// Projects each element of an async-enumerable sequence into zero or more buffers which are produced based on element count information.
  47. /// </summary>
  48. /// <typeparam name="TSource">The type of the elements in the source sequence, and in the lists in the result sequence.</typeparam>
  49. /// <param name="source">Source sequence to produce buffers over.</param>
  50. /// <param name="count">Length of each buffer.</param>
  51. /// <param name="skip">Number of elements to skip between creation of consecutive buffers.</param>
  52. /// <returns>An async-enumerable sequence of buffers.</returns>
  53. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  54. /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> or <paramref name="skip"/> is less than or equal to zero.</exception>
  55. public static IAsyncEnumerable<IList<TSource>> Buffer<TSource>(this IAsyncEnumerable<TSource> source, int count, int skip)
  56. {
  57. if (source == null)
  58. throw Error.ArgumentNull(nameof(source));
  59. if (count <= 0)
  60. throw Error.ArgumentOutOfRange(nameof(count));
  61. if (skip <= 0)
  62. throw Error.ArgumentOutOfRange(nameof(skip));
  63. return Core(source, count, skip);
  64. static async IAsyncEnumerable<IList<TSource>> Core(IAsyncEnumerable<TSource> source, int count, int skip, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  65. {
  66. var buffers = new Queue<IList<TSource>>();
  67. var index = 0;
  68. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  69. {
  70. if (index++ % skip == 0)
  71. {
  72. buffers.Enqueue(new List<TSource>(count));
  73. }
  74. foreach (var buffer in buffers)
  75. {
  76. buffer.Add(item);
  77. }
  78. if (buffers.Count > 0 && buffers.Peek().Count == count)
  79. {
  80. yield return buffers.Dequeue();
  81. }
  82. }
  83. while (buffers.Count > 0)
  84. {
  85. yield return buffers.Dequeue();
  86. }
  87. }
  88. }
  89. }
  90. }