// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information. using System.Collections.Generic; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; namespace System.Linq { public static partial class AsyncEnumerable { public static IAsyncEnumerable> Buffer(this IAsyncEnumerable source, int count) { if (source == null) throw new ArgumentNullException(nameof(source)); if (count <= 0) throw new ArgumentOutOfRangeException(nameof(count)); return new BufferAsyncIterator(source, count, count); } public static IAsyncEnumerable> Buffer(this IAsyncEnumerable source, int count, int skip) { if (source == null) throw new ArgumentNullException(nameof(source)); if (count <= 0) throw new ArgumentOutOfRangeException(nameof(count)); if (skip <= 0) throw new ArgumentOutOfRangeException(nameof(skip)); return new BufferAsyncIterator(source, count, skip); } private sealed class BufferAsyncIterator : AsyncIterator> { private readonly int count; private readonly int skip; private readonly IAsyncEnumerable source; private Queue> buffers; private IAsyncEnumerator enumerator; private int index; private bool stopped; public BufferAsyncIterator(IAsyncEnumerable source, int count, int skip) { Debug.Assert(source != null); this.source = source; this.count = count; this.skip = skip; } public override AsyncIterator> Clone() { return new BufferAsyncIterator(source, count, skip); } public override void Dispose() { if (enumerator != null) { enumerator.Dispose(); enumerator = null; } buffers = null; base.Dispose(); } protected override async Task MoveNextCore(CancellationToken cancellationToken) { switch (state) { case AsyncIteratorState.Allocated: enumerator = source.GetEnumerator(); buffers = new Queue>(); index = 0; stopped = false; state = AsyncIteratorState.Iterating; goto case AsyncIteratorState.Iterating; case AsyncIteratorState.Iterating: while (true) { if (!stopped) { if (await enumerator.MoveNext(cancellationToken) .ConfigureAwait(false)) { var item = enumerator.Current; if (index++ % skip == 0) { buffers.Enqueue(new List(count)); } foreach (var buffer in buffers) { buffer.Add(item); } if (buffers.Count > 0 && buffers.Peek() .Count == count) { current = buffers.Dequeue(); return true; } continue; // loop } stopped = true; enumerator.Dispose(); enumerator = null; continue; // loop } if (buffers.Count > 0) { current = buffers.Dequeue(); return true; } break; // exit the while } break; // case } Dispose(); return false; } } } }