// 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 AsyncEnumerableEx { public static IAsyncEnumerable Scan(this IAsyncEnumerable source, Func accumulator) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (accumulator == null) throw Error.ArgumentNull(nameof(accumulator)); return new ScanAsyncEnumerable(source, accumulator); } public static IAsyncEnumerable Scan(this IAsyncEnumerable source, TAccumulate seed, Func accumulator) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (accumulator == null) throw Error.ArgumentNull(nameof(accumulator)); return new ScanAsyncEnumerable(source, seed, accumulator); } public static IAsyncEnumerable Scan(this IAsyncEnumerable source, Func> accumulator) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (accumulator == null) throw Error.ArgumentNull(nameof(accumulator)); return new ScanAsyncEnumerableWithTask(source, accumulator); } public static IAsyncEnumerable Scan(this IAsyncEnumerable source, TAccumulate seed, Func> accumulator) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (accumulator == null) throw Error.ArgumentNull(nameof(accumulator)); return new ScanAsyncEnumerableWithTask(source, seed, accumulator); } 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); _source = source; _accumulator = accumulator; } public override AsyncIterator Clone() { return new ScanAsyncEnumerable(_source, _accumulator); } public override async ValueTask DisposeAsync() { if (_enumerator != null) { await _enumerator.DisposeAsync().ConfigureAwait(false); _enumerator = null; _accumulated = default; } await base.DisposeAsync().ConfigureAwait(false); } protected override async ValueTask MoveNextCore(CancellationToken cancellationToken) { switch (state) { case AsyncIteratorState.Allocated: _enumerator = _source.GetAsyncEnumerator(cancellationToken); _hasSeed = false; _accumulated = default; state = AsyncIteratorState.Iterating; goto case AsyncIteratorState.Iterating; case AsyncIteratorState.Iterating: while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) { var item = _enumerator.Current; if (!_hasSeed) { _hasSeed = true; _accumulated = item; continue; // loop } _accumulated = _accumulator(_accumulated, item); current = _accumulated; return true; } break; // case } await DisposeAsync().ConfigureAwait(false); return false; } } 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); _source = source; _seed = seed; _accumulator = accumulator; } public override AsyncIterator Clone() { return new ScanAsyncEnumerable(_source, _seed, _accumulator); } public override async ValueTask DisposeAsync() { if (_enumerator != null) { await _enumerator.DisposeAsync().ConfigureAwait(false); _enumerator = null; _accumulated = default; } await base.DisposeAsync().ConfigureAwait(false); } protected override async ValueTask MoveNextCore(CancellationToken cancellationToken) { switch (state) { case AsyncIteratorState.Allocated: _enumerator = _source.GetAsyncEnumerator(cancellationToken); _accumulated = _seed; state = AsyncIteratorState.Iterating; goto case AsyncIteratorState.Iterating; case AsyncIteratorState.Iterating: if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) { var item = _enumerator.Current; _accumulated = _accumulator(_accumulated, item); current = _accumulated; return true; } break; } await DisposeAsync().ConfigureAwait(false); return false; } } private sealed class ScanAsyncEnumerableWithTask : AsyncIterator { private readonly Func> _accumulator; private readonly IAsyncEnumerable _source; private TSource _accumulated; private IAsyncEnumerator _enumerator; private bool _hasSeed; public ScanAsyncEnumerableWithTask(IAsyncEnumerable source, Func> accumulator) { Debug.Assert(source != null); Debug.Assert(accumulator != null); _source = source; _accumulator = accumulator; } public override AsyncIterator Clone() { return new ScanAsyncEnumerableWithTask(_source, _accumulator); } public override async ValueTask DisposeAsync() { if (_enumerator != null) { await _enumerator.DisposeAsync().ConfigureAwait(false); _enumerator = null; _accumulated = default; } await base.DisposeAsync().ConfigureAwait(false); } protected override async ValueTask MoveNextCore(CancellationToken cancellationToken) { switch (state) { case AsyncIteratorState.Allocated: _enumerator = _source.GetAsyncEnumerator(cancellationToken); _hasSeed = false; _accumulated = default; state = AsyncIteratorState.Iterating; goto case AsyncIteratorState.Iterating; case AsyncIteratorState.Iterating: while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) { var item = _enumerator.Current; if (!_hasSeed) { _hasSeed = true; _accumulated = item; continue; // loop } _accumulated = await _accumulator(_accumulated, item).ConfigureAwait(false); current = _accumulated; return true; } break; // case } await DisposeAsync().ConfigureAwait(false); return false; } } private sealed class ScanAsyncEnumerableWithTask : AsyncIterator { private readonly Func> _accumulator; private readonly TAccumulate _seed; private readonly IAsyncEnumerable _source; private TAccumulate _accumulated; private IAsyncEnumerator _enumerator; public ScanAsyncEnumerableWithTask(IAsyncEnumerable source, TAccumulate seed, Func> accumulator) { Debug.Assert(source != null); Debug.Assert(accumulator != null); _source = source; _seed = seed; _accumulator = accumulator; } public override AsyncIterator Clone() { return new ScanAsyncEnumerableWithTask(_source, _seed, _accumulator); } public override async ValueTask DisposeAsync() { if (_enumerator != null) { await _enumerator.DisposeAsync().ConfigureAwait(false); _enumerator = null; _accumulated = default; } await base.DisposeAsync().ConfigureAwait(false); } protected override async ValueTask MoveNextCore(CancellationToken cancellationToken) { switch (state) { case AsyncIteratorState.Allocated: _enumerator = _source.GetAsyncEnumerator(cancellationToken); _accumulated = _seed; state = AsyncIteratorState.Iterating; goto case AsyncIteratorState.Iterating; case AsyncIteratorState.Iterating: if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) { var item = _enumerator.Current; _accumulated = await _accumulator(_accumulated, item).ConfigureAwait(false); current = _accumulated; return true; } break; } await DisposeAsync().ConfigureAwait(false); return false; } } } }