12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- // 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<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
- {
- if (condition == null)
- throw Error.ArgumentNull(nameof(condition));
- if (iterate == null)
- throw Error.ArgumentNull(nameof(iterate));
- if (resultSelector == null)
- throw Error.ArgumentNull(nameof(resultSelector));
- return new GenerateAsyncIterator<TState, TResult>(initialState, condition, iterate, resultSelector);
- }
- private sealed class GenerateAsyncIterator<TState, TResult> : AsyncIterator<TResult>
- {
- private readonly Func<TState, bool> _condition;
- private readonly TState _initialState;
- private readonly Func<TState, TState> _iterate;
- private readonly Func<TState, TResult> _resultSelector;
- private TState _currentState;
- private bool _started;
- public GenerateAsyncIterator(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
- {
- Debug.Assert(condition != null);
- Debug.Assert(iterate != null);
- Debug.Assert(resultSelector != null);
- _initialState = initialState;
- _condition = condition;
- _iterate = iterate;
- _resultSelector = resultSelector;
- }
- public override AsyncIterator<TResult> Clone()
- {
- return new GenerateAsyncIterator<TState, TResult>(_initialState, _condition, _iterate, _resultSelector);
- }
- public override async ValueTask DisposeAsync()
- {
- _currentState = default;
- await base.DisposeAsync().ConfigureAwait(false);
- }
- protected override async ValueTask<bool> MoveNextCore()
- {
- switch (state)
- {
- case AsyncIteratorState.Allocated:
- _started = false;
- _currentState = _initialState;
- state = AsyncIteratorState.Iterating;
- goto case AsyncIteratorState.Iterating;
- case AsyncIteratorState.Iterating:
- if (_started)
- {
- _currentState = _iterate(_currentState);
- }
- _started = true;
- if (_condition(_currentState))
- {
- current = _resultSelector(_currentState);
- return true;
- }
- break;
- }
- await DisposeAsync().ConfigureAwait(false);
- return false;
- }
- }
- }
- }
|