Buffer.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. namespace System.Linq
  6. {
  7. public static partial class EnumerableEx
  8. {
  9. /// <summary>
  10. /// Generates a sequence of non-overlapping adjacent buffers over the source sequence.
  11. /// </summary>
  12. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  13. /// <param name="source">Source sequence.</param>
  14. /// <param name="count">Number of elements for allocated buffers.</param>
  15. /// <returns>Sequence of buffers containing source sequence elements.</returns>
  16. public static IEnumerable<IList<TSource>> Buffer<TSource>(this IEnumerable<TSource> source, int count)
  17. {
  18. if (source == null)
  19. {
  20. throw new ArgumentNullException(nameof(source));
  21. }
  22. if (count <= 0)
  23. {
  24. throw new ArgumentOutOfRangeException(nameof(count));
  25. }
  26. return BufferExact(source, count);
  27. }
  28. /// <summary>
  29. /// Generates a sequence of buffers over the source sequence, with specified length and possible overlap.
  30. /// </summary>
  31. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  32. /// <param name="source">Source sequence.</param>
  33. /// <param name="count">Number of elements for allocated buffers.</param>
  34. /// <param name="skip">Number of elements to skip between the start of consecutive buffers.</param>
  35. /// <returns>Sequence of buffers containing source sequence elements.</returns>
  36. public static IEnumerable<IList<TSource>> Buffer<TSource>(this IEnumerable<TSource> source, int count, int skip)
  37. {
  38. if (source == null)
  39. {
  40. throw new ArgumentNullException(nameof(source));
  41. }
  42. if (count <= 0)
  43. {
  44. throw new ArgumentOutOfRangeException(nameof(count));
  45. }
  46. if (skip <= 0)
  47. {
  48. throw new ArgumentOutOfRangeException(nameof(skip));
  49. }
  50. if (count == skip)
  51. {
  52. return BufferExact(source, count);
  53. }
  54. if (count < skip)
  55. {
  56. return BufferSkip(source, count, skip);
  57. }
  58. return source.Buffer_(count, skip);
  59. }
  60. private static IEnumerable<IList<TSource>> Buffer_<TSource>(this IEnumerable<TSource> source, int count, int skip)
  61. {
  62. var buffers = new Queue<IList<TSource>>();
  63. var i = 0;
  64. foreach (var item in source)
  65. {
  66. if (i % skip == 0)
  67. {
  68. buffers.Enqueue(new List<TSource>(count));
  69. }
  70. foreach (var buffer in buffers)
  71. {
  72. buffer.Add(item);
  73. }
  74. if (buffers.Count > 0 && buffers.Peek()
  75. .Count == count)
  76. {
  77. yield return buffers.Dequeue();
  78. }
  79. i++;
  80. }
  81. while (buffers.Count > 0)
  82. {
  83. yield return buffers.Dequeue();
  84. }
  85. }
  86. private static IEnumerable<IList<TSource>> BufferExact<TSource>(IEnumerable<TSource> source, int count)
  87. {
  88. IList<TSource> buffer = null;
  89. foreach (var v in source)
  90. {
  91. if (buffer == null)
  92. {
  93. buffer = new List<TSource>();
  94. }
  95. buffer.Add(v);
  96. if (buffer.Count == count)
  97. {
  98. yield return buffer;
  99. buffer = null;
  100. }
  101. }
  102. if (buffer != null)
  103. {
  104. yield return buffer;
  105. }
  106. }
  107. private static IEnumerable<IList<TSource>> BufferSkip<TSource>(IEnumerable<TSource> source, int count, int skip)
  108. {
  109. IList<TSource> buffer = null;
  110. var index = 0;
  111. foreach (var v in source)
  112. {
  113. if (index == 0)
  114. {
  115. buffer = new List<TSource>();
  116. }
  117. buffer?.Add(v);
  118. if (++index == count)
  119. {
  120. yield return buffer;
  121. buffer = null;
  122. }
  123. if (index == skip)
  124. {
  125. index = 0;
  126. }
  127. }
  128. if (buffer != null)
  129. {
  130. yield return buffer;
  131. }
  132. }
  133. }
  134. /// <summary>
  135. /// Represents a buffer exposing a shared view over an underlying enumerable sequence.
  136. /// </summary>
  137. /// <typeparam name="T">Element type.</typeparam>
  138. public interface IBuffer<out T> : IEnumerable<T>, IDisposable
  139. {
  140. }
  141. }