Buffer.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. throw new ArgumentNullException(nameof(source));
  20. if (count <= 0)
  21. throw new ArgumentOutOfRangeException(nameof(count));
  22. return source.Buffer_(count, count);
  23. }
  24. /// <summary>
  25. /// Generates a sequence of buffers over the source sequence, with specified length and possible overlap.
  26. /// </summary>
  27. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  28. /// <param name="source">Source sequence.</param>
  29. /// <param name="count">Number of elements for allocated buffers.</param>
  30. /// <param name="skip">Number of elements to skip between the start of consecutive buffers.</param>
  31. /// <returns>Sequence of buffers containing source sequence elements.</returns>
  32. public static IEnumerable<IList<TSource>> Buffer<TSource>(this IEnumerable<TSource> source, int count, int skip)
  33. {
  34. if (source == null)
  35. throw new ArgumentNullException(nameof(source));
  36. if (count <= 0)
  37. throw new ArgumentOutOfRangeException(nameof(count));
  38. if (skip <= 0)
  39. throw new ArgumentOutOfRangeException(nameof(skip));
  40. return source.Buffer_(count, skip);
  41. }
  42. private static IEnumerable<IList<TSource>> Buffer_<TSource>(this IEnumerable<TSource> source, int count, int skip)
  43. {
  44. var buffers = new Queue<IList<TSource>>();
  45. var i = 0;
  46. foreach (var item in source)
  47. {
  48. if (i%skip == 0)
  49. buffers.Enqueue(new List<TSource>(count));
  50. foreach (var buffer in buffers)
  51. buffer.Add(item);
  52. if (buffers.Count > 0 && buffers.Peek()
  53. .Count == count)
  54. yield return buffers.Dequeue();
  55. i++;
  56. }
  57. while (buffers.Count > 0)
  58. yield return buffers.Dequeue();
  59. }
  60. }
  61. /// <summary>
  62. /// Represents a buffer exposing a shared view over an underlying enumerable sequence.
  63. /// </summary>
  64. /// <typeparam name="T">Element type.</typeparam>
  65. public interface IBuffer<out T> : IEnumerable<T>, IDisposable
  66. {
  67. }
  68. }