// 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 Scan(this IAsyncEnumerable source, TAccumulate seed, Func accumulator) { if (source == null) throw new ArgumentNullException(nameof(source)); if (accumulator == null) throw new ArgumentNullException(nameof(accumulator)); return new ScanAsyncEnumerable(source, seed, accumulator); } public static IAsyncEnumerable Scan(this IAsyncEnumerable source, Func accumulator) { if (source == null) throw new ArgumentNullException(nameof(source)); if (accumulator == null) throw new ArgumentNullException(nameof(accumulator)); return new ScanAsyncEnumerable(source, accumulator); } private sealed class ScanAsyncEnumerable : AsyncIterator { private readonly Func accumulator; private readonly TAccumulate seed; private readonly IAsyncEnumerable source; private TAccumulate accumulated; private IAsyncEnumerator enumerator; public ScanAsyncEnumerable(IAsyncEnumerable source, TAccumulate seed, Func accumulator) { Debug.Assert(source != null); Debug.Assert(accumulator != null); this.source = source; this.seed = seed; this.accumulator = accumulator; } public override AsyncIterator Clone() { return new ScanAsyncEnumerable(source, seed, accumulator); } public override void Dispose() { if (enumerator != null) { enumerator.Dispose(); enumerator = null; accumulated = default(TAccumulate); } base.Dispose(); } protected override async Task MoveNextCore(CancellationToken cancellationToken) { switch (state) { case AsyncIteratorState.Allocated: enumerator = source.GetEnumerator(); accumulated = seed; state = AsyncIteratorState.Iterating; goto case AsyncIteratorState.Iterating; case AsyncIteratorState.Iterating: if (await enumerator.MoveNext(cancellationToken) .ConfigureAwait(false)) { var item = enumerator.Current; accumulated = accumulator(accumulated, item); current = accumulated; return true; } break; } Dispose(); return false; } } private sealed class ScanAsyncEnumerable : AsyncIterator { private readonly Func accumulator; private readonly IAsyncEnumerable source; private TSource accumulated; private IAsyncEnumerator enumerator; private bool hasSeed; public ScanAsyncEnumerable(IAsyncEnumerable source, Func accumulator) { Debug.Assert(source != null); Debug.Assert(accumulator != null); this.source = source; this.accumulator = accumulator; } public override AsyncIterator Clone() { return new ScanAsyncEnumerable(source, accumulator); } public override void Dispose() { if (enumerator != null) { enumerator.Dispose(); enumerator = null; accumulated = default(TSource); } base.Dispose(); } protected override async Task MoveNextCore(CancellationToken cancellationToken) { switch (state) { case AsyncIteratorState.Allocated: enumerator = source.GetEnumerator(); hasSeed = false; accumulated = default(TSource); state = AsyncIteratorState.Iterating; goto case AsyncIteratorState.Iterating; case AsyncIteratorState.Iterating: while (await enumerator.MoveNext(cancellationToken) .ConfigureAwait(false)) { var item = enumerator.Current; if (!hasSeed) { hasSeed = true; accumulated = item; continue; // loop } accumulated = accumulator(accumulated, item); current = accumulated; return true; } break; // case } Dispose(); return false; } } } }