From 633519497897ce1acdfdfe8e25fa0b956d4b08b7 Mon Sep 17 00:00:00 2001 From: Bart De Smet Date: Fri, 24 May 2019 02:37:52 -0700 Subject: [PATCH] Remove dead code of hand-rolled iterators. --- .../System/Linq/Operators/Amb.cs | 217 ---- .../System/Linq/Operators/Buffer.cs | 114 --- .../System/Linq/Operators/Catch.cs | 394 -------- .../System/Linq/Operators/Concat.cs | 198 ---- .../System/Linq/Operators/Defer.cs | 187 ---- .../Linq/Operators/DistinctUntilChanged.cs | 308 ------ .../System/Linq/Operators/Do.cs | 251 ----- .../System/Linq/Operators/Expand.cs | 287 ------ .../System/Linq/Operators/Finally.cs | 131 +-- .../System/Linq/Operators/Generate.cs | 80 +- .../System/Linq/Operators/IgnoreElements.cs | 57 -- .../System/Linq/Operators/Merge.cs | 107 +- .../System/Linq/Operators/Repeat.cs | 106 -- .../System/Linq/Operators/Scan.cs | 450 --------- .../System/Linq/Operators/Using.cs | 219 ---- .../System/Linq/Operators/SkipLast.cs | 2 - .../System/Linq/Operators/Cast.cs | 56 -- .../System/Linq/Operators/Except.cs | 85 -- .../System/Linq/Operators/GroupJoin.cs | 310 ------ .../System/Linq/Operators/Intersect.cs | 85 -- .../System/Linq/Operators/Join.cs | 397 -------- .../System/Linq/Operators/OfType.cs | 60 -- .../System/Linq/Operators/Select.cs | 210 ---- .../System/Linq/Operators/SelectMany.cs | 950 ------------------ .../System/Linq/Operators/SkipLast.cs | 74 -- .../System/Linq/Operators/SkipWhile.cs | 510 ---------- .../System/Linq/Operators/TakeLast.cs | 99 -- .../System/Linq/Operators/TakeWhile.cs | 430 -------- .../System/Linq/Operators/Where.cs | 222 ---- .../System/Linq/Operators/Zip.cs | 236 ----- 30 files changed, 5 insertions(+), 6827 deletions(-) diff --git a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Amb.cs b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Amb.cs index 4cb26ab517..0de698fa22 100644 --- a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Amb.cs +++ b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Amb.cs @@ -3,7 +3,6 @@ // 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; @@ -18,7 +17,6 @@ public static IAsyncEnumerable Amb(this IAsyncEnumerable Core(CancellationToken cancellationToken) @@ -126,9 +124,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) await disposeLoser.ConfigureAwait(false); } } -#else - return new AmbAsyncIterator(first, second); -#endif } public static IAsyncEnumerable Amb(params IAsyncEnumerable[] sources) @@ -136,7 +131,6 @@ public static IAsyncEnumerable Amb(params IAsyncEnumerable Core(CancellationToken cancellationToken) @@ -224,9 +218,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) await cleanupLosers.ConfigureAwait(false); } } -#else - return new AmbAsyncIteratorN(sources); -#endif } public static IAsyncEnumerable Amb(this IEnumerable> sources) @@ -234,14 +225,9 @@ public static IAsyncEnumerable Amb(this IEnumerable(sources.ToArray()); -#endif } -#if USE_ASYNC_ITERATOR private static async Task AwaitMoveNextAsyncAndDispose(Task moveNextAsync, IAsyncEnumerator enumerator) { if (enumerator != null) @@ -255,208 +241,5 @@ private static async Task AwaitMoveNextAsyncAndDispose(Task moveNextAsy } } } -#endif - -#if !USE_ASYNC_ITERATOR - private sealed class AmbAsyncIterator : AsyncIterator - { - private readonly IAsyncEnumerable _first; - private readonly IAsyncEnumerable _second; - - private IAsyncEnumerator _enumerator; - - public AmbAsyncIterator(IAsyncEnumerable first, IAsyncEnumerable second) - { - Debug.Assert(first != null); - Debug.Assert(second != null); - - _first = first; - _second = second; - } - - public override AsyncIteratorBase Clone() - { - return new AmbAsyncIterator(_first, _second); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - // - // REVIEW: Exceptions in any of these steps don't cause cleanup. This has been fixed in the new implementation. - // - - var firstEnumerator = _first.GetAsyncEnumerator(_cancellationToken); - var secondEnumerator = _second.GetAsyncEnumerator(_cancellationToken); - - var firstMoveNext = firstEnumerator.MoveNextAsync().AsTask(); - var secondMoveNext = secondEnumerator.MoveNextAsync().AsTask(); - - var winner = await Task.WhenAny(firstMoveNext, secondMoveNext).ConfigureAwait(false); - - // - // REVIEW: An alternative option is to call DisposeAsync on the other and await it, but this has two drawbacks: - // - // 1. Concurrent DisposeAsync while a MoveNextAsync is in flight. - // 2. The winner elected by Amb is blocked to yield results until the loser unblocks. - // - // The approach below has one drawback, namely that exceptions raised by loser are dropped on the floor. - // - - if (winner == firstMoveNext) - { - _enumerator = firstEnumerator; - - _ = secondMoveNext.ContinueWith(_ => - { - secondEnumerator.DisposeAsync(); - }); - } - else - { - _enumerator = secondEnumerator; - - _ = firstMoveNext.ContinueWith(_ => - { - firstEnumerator.DisposeAsync(); - }); - } - - _state = AsyncIteratorState.Iterating; - - if (await winner.ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - break; - - case AsyncIteratorState.Iterating: - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - - private sealed class AmbAsyncIteratorN : AsyncIterator - { - private readonly IAsyncEnumerable[] _sources; - - private IAsyncEnumerator _enumerator; - - public AmbAsyncIteratorN(IAsyncEnumerable[] sources) - { - Debug.Assert(sources != null); - - _sources = sources; - } - - public override AsyncIteratorBase Clone() - { - return new AmbAsyncIteratorN(_sources); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - var n = _sources.Length; - - var enumerators = new IAsyncEnumerator[n]; - var moveNexts = new Task[n]; - - for (var i = 0; i < n; i++) - { - var enumerator = _sources[i].GetAsyncEnumerator(_cancellationToken); - - enumerators[i] = enumerator; - moveNexts[i] = enumerator.MoveNextAsync().AsTask(); - } - - var winner = await Task.WhenAny(moveNexts).ConfigureAwait(false); - - // - // REVIEW: An alternative option is to call DisposeAsync on the other and await it, but this has two drawbacks: - // - // 1. Concurrent DisposeAsync while a MoveNextAsync is in flight. - // 2. The winner elected by Amb is blocked to yield results until all losers unblocks. - // - // The approach below has one drawback, namely that exceptions raised by any loser are dropped on the floor. - // - - var winnerIndex = Array.IndexOf(moveNexts, winner); - - _enumerator = enumerators[winnerIndex]; - - for (var i = 0; i < n; i++) - { - if (i != winnerIndex) - { - _ = moveNexts[i].ContinueWith(_ => - { - enumerators[i].DisposeAsync(); - }); - } - } - - _state = AsyncIteratorState.Iterating; - - if (await winner.ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - break; - - case AsyncIteratorState.Iterating: - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } -#endif } } diff --git a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Buffer.cs b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Buffer.cs index f58107295c..3289ac45e7 100644 --- a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Buffer.cs +++ b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Buffer.cs @@ -3,7 +3,6 @@ // 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; @@ -18,7 +17,6 @@ public static IAsyncEnumerable> Buffer(this IAsyncEnumer if (count <= 0) throw Error.ArgumentOutOfRange(nameof(count)); -#if USE_ASYNC_ITERATOR return AsyncEnumerable.Create(Core); async IAsyncEnumerator> Core(CancellationToken cancellationToken) @@ -42,9 +40,6 @@ async IAsyncEnumerator> Core(CancellationToken cancellationToken) yield return buffer; } } -#else - return new BufferAsyncIterator(source, count, count); -#endif } public static IAsyncEnumerable> Buffer(this IAsyncEnumerable source, int count, int skip) @@ -56,7 +51,6 @@ public static IAsyncEnumerable> Buffer(this IAsyncEnumer if (skip <= 0) throw Error.ArgumentOutOfRange(nameof(skip)); -#if USE_ASYNC_ITERATOR return AsyncEnumerable.Create(Core); async IAsyncEnumerator> Core(CancellationToken cancellationToken) @@ -88,114 +82,6 @@ async IAsyncEnumerator> Core(CancellationToken cancellationToken) yield return buffers.Dequeue(); } } -#else - return new BufferAsyncIterator(source, count, skip); -#endif } - -#if !USE_ASYNC_ITERATOR - 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); - - _source = source; - _count = count; - _skip = skip; - } - - public override AsyncIteratorBase> Clone() - { - return new BufferAsyncIterator(_source, _count, _skip); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - _buffers = null; - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _buffers = new Queue>(); - _index = 0; - _stopped = false; - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (true) - { - if (!_stopped) - { - if (await _enumerator.MoveNextAsync().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; - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - - continue; // loop - } - - if (_buffers.Count > 0) - { - _current = _buffers.Dequeue(); - return true; - } - - break; // exit the while - } - - break; // case - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } -#endif } } diff --git a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Catch.cs b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Catch.cs index b08ea8afd3..958ec44ba9 100644 --- a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Catch.cs +++ b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Catch.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; -using System.Diagnostics; using System.Runtime.ExceptionServices; using System.Threading; using System.Threading.Tasks; @@ -25,7 +24,6 @@ public static IAsyncEnumerable Catch(this IAsyncEn if (handler == null) throw Error.ArgumentNull(nameof(handler)); -#if USE_ASYNC_ITERATOR return AsyncEnumerable.Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -70,9 +68,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new CatchAsyncIterator(source, handler); -#endif } public static IAsyncEnumerable Catch(this IAsyncEnumerable source, Func>> handler) @@ -83,7 +78,6 @@ public static IAsyncEnumerable Catch(this IAsyncEn if (handler == null) throw Error.ArgumentNull(nameof(handler)); -#if USE_ASYNC_ITERATOR return AsyncEnumerable.Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -128,9 +122,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new CatchAsyncIteratorWithTask(source, handler); -#endif } #if !NO_DEEP_CANCELLATION @@ -142,7 +133,6 @@ public static IAsyncEnumerable Catch(this IAsyncEn if (handler == null) throw Error.ArgumentNull(nameof(handler)); -#if USE_ASYNC_ITERATOR return AsyncEnumerable.Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -187,9 +177,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new CatchAsyncIteratorWithTaskAndCancellation(source, handler); -#endif } #endif @@ -221,7 +208,6 @@ public static IAsyncEnumerable Catch(this IAsyncEnumerable CatchCore(IEnumerable> sources) { -#if USE_ASYNC_ITERATOR return AsyncEnumerable.Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -261,386 +247,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) error?.Throw(); } -#else - return new CatchAsyncIterator(sources); -#endif - } - -#if !USE_ASYNC_ITERATOR - private sealed class CatchAsyncIterator : AsyncIterator where TException : Exception - { - private readonly Func> _handler; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - private bool _isDone; - - public CatchAsyncIterator(IAsyncEnumerable source, Func> handler) - { - Debug.Assert(source != null); - Debug.Assert(handler != null); - - _source = source; - _handler = handler; - } - - public override AsyncIteratorBase Clone() - { - return new CatchAsyncIterator(_source, _handler); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _isDone = false; - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (true) - { - if (!_isDone) - { - try - { - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - } - catch (TException ex) - { - // Note: Ideally we'd dispose of the previous enumerator before - // invoking the handler, but we use this order to preserve - // current behavior - var inner = _handler(ex); - var err = inner.GetAsyncEnumerator(_cancellationToken); - - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - } - - _enumerator = err; - _isDone = true; - continue; // loop so we hit the catch state - } - } - - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - break; // while - } - - break; // case - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - - private sealed class CatchAsyncIteratorWithTask : AsyncIterator where TException : Exception - { - private readonly Func>> _handler; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - private bool _isDone; - - public CatchAsyncIteratorWithTask(IAsyncEnumerable source, Func>> handler) - { - Debug.Assert(source != null); - Debug.Assert(handler != null); - - _source = source; - _handler = handler; - } - - public override AsyncIteratorBase Clone() - { - return new CatchAsyncIteratorWithTask(_source, _handler); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _isDone = false; - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (true) - { - if (!_isDone) - { - try - { - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - } - catch (TException ex) - { - // Note: Ideally we'd dispose of the previous enumerator before - // invoking the handler, but we use this order to preserve - // current behavior - var inner = await _handler(ex).ConfigureAwait(false); - var err = inner.GetAsyncEnumerator(_cancellationToken); - - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - } - - _enumerator = err; - _isDone = true; - continue; // loop so we hit the catch state - } - } - - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - break; // while - } - - break; // case - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - -#if !NO_DEEP_CANCELLATION - private sealed class CatchAsyncIteratorWithTaskAndCancellation : AsyncIterator where TException : Exception - { - private readonly Func>> _handler; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - private bool _isDone; - - public CatchAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable source, Func>> handler) - { - Debug.Assert(source != null); - Debug.Assert(handler != null); - - _source = source; - _handler = handler; - } - - public override AsyncIteratorBase Clone() - { - return new CatchAsyncIteratorWithTaskAndCancellation(_source, _handler); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _isDone = false; - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (true) - { - if (!_isDone) - { - try - { - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - } - catch (TException ex) - { - // Note: Ideally we'd dispose of the previous enumerator before - // invoking the handler, but we use this order to preserve - // current behavior - var inner = await _handler(ex, _cancellationToken).ConfigureAwait(false); - var err = inner.GetAsyncEnumerator(_cancellationToken); - - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - } - - _enumerator = err; - _isDone = true; - continue; // loop so we hit the catch state - } - } - - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - break; // while - } - - break; // case - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } -#endif - - private sealed class CatchAsyncIterator : AsyncIterator - { - private readonly IEnumerable> _sources; - - private IAsyncEnumerator _enumerator; - private ExceptionDispatchInfo _error; - - private IEnumerator> _sourcesEnumerator; - - public CatchAsyncIterator(IEnumerable> sources) - { - Debug.Assert(sources != null); - - _sources = sources; - } - - public override AsyncIteratorBase Clone() - { - return new CatchAsyncIterator(_sources); - } - - public override async ValueTask DisposeAsync() - { - if (_sourcesEnumerator != null) - { - _sourcesEnumerator.Dispose(); - _sourcesEnumerator = null; - } - - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - _error = null; - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _sourcesEnumerator = _sources.GetEnumerator(); - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (true) - { - if (_enumerator == null) - { - if (!_sourcesEnumerator.MoveNext()) - { - // only throw if we have an error on the last one - _error?.Throw(); - break; // done, nothing else to do - } - - _error = null; - _enumerator = _sourcesEnumerator.Current.GetAsyncEnumerator(_cancellationToken); - } - - try - { - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - } - catch (Exception ex) - { - // Done with the current one, go to the next - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - _error = ExceptionDispatchInfo.Capture(ex); - continue; - } - - break; // while - } - - break; // case - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } } -#endif } } diff --git a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Concat.cs b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Concat.cs index 94d6112a94..db5b6bb003 100644 --- a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Concat.cs +++ b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Concat.cs @@ -3,7 +3,6 @@ // 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; @@ -16,7 +15,6 @@ public static IAsyncEnumerable Concat(this IAsyncEnumerable Core(CancellationToken cancellationToken) @@ -29,9 +27,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new ConcatAsyncEnumerableAsyncIterator(sources); -#endif } public static IAsyncEnumerable Concat(this IEnumerable> sources) @@ -39,7 +34,6 @@ public static IAsyncEnumerable Concat(this IEnumerable Core(CancellationToken cancellationToken) @@ -52,9 +46,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return ConcatCore(sources); -#endif } public static IAsyncEnumerable Concat(params IAsyncEnumerable[] sources) @@ -62,7 +53,6 @@ public static IAsyncEnumerable Concat(params IAsyncEnumerable< if (sources == null) throw Error.ArgumentNull(nameof(sources)); -#if USE_ASYNC_ITERATOR return AsyncEnumerable.Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -75,194 +65,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return ConcatCore(sources); -#endif } - -#if !USE_ASYNC_ITERATOR - private static IAsyncEnumerable ConcatCore(IEnumerable> sources) - { - return new ConcatEnumerableAsyncIterator(sources); - } - - private sealed class ConcatEnumerableAsyncIterator : AsyncIterator - { - private readonly IEnumerable> _source; - - public ConcatEnumerableAsyncIterator(IEnumerable> source) - { - Debug.Assert(source != null); - - _source = source; - } - - public override AsyncIteratorBase Clone() - { - return new ConcatEnumerableAsyncIterator(_source); - } - - public override async ValueTask DisposeAsync() - { - if (_outerEnumerator != null) - { - _outerEnumerator.Dispose(); - _outerEnumerator = null; - } - - if (_currentEnumerator != null) - { - await _currentEnumerator.DisposeAsync().ConfigureAwait(false); - _currentEnumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - // State machine vars - private IEnumerator> _outerEnumerator; - private IAsyncEnumerator _currentEnumerator; - private int _mode; - - private const int State_OuterNext = 1; - private const int State_While = 4; - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _outerEnumerator = _source.GetEnumerator(); - _mode = State_OuterNext; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - switch (_mode) - { - case State_OuterNext: - if (_outerEnumerator.MoveNext()) - { - // make sure we dispose the previous one if we're about to replace it - if (_currentEnumerator != null) - { - await _currentEnumerator.DisposeAsync().ConfigureAwait(false); - } - - _currentEnumerator = _outerEnumerator.Current.GetAsyncEnumerator(_cancellationToken); - - _mode = State_While; - goto case State_While; - } - - break; - case State_While: - if (await _currentEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _currentEnumerator.Current; - return true; - } - - // No more on the inner enumerator, move to the next outer - goto case State_OuterNext; - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } - - private sealed class ConcatAsyncEnumerableAsyncIterator : AsyncIterator - { - private readonly IAsyncEnumerable> _source; - - public ConcatAsyncEnumerableAsyncIterator(IAsyncEnumerable> source) - { - Debug.Assert(source != null); - - _source = source; - } - - public override AsyncIteratorBase Clone() - { - return new ConcatAsyncEnumerableAsyncIterator(_source); - } - - public override async ValueTask DisposeAsync() - { - if (_outerEnumerator != null) - { - await _outerEnumerator.DisposeAsync().ConfigureAwait(false); - _outerEnumerator = null; - } - - if (_currentEnumerator != null) - { - await _currentEnumerator.DisposeAsync().ConfigureAwait(false); - _currentEnumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - // State machine vars - private IAsyncEnumerator> _outerEnumerator; - private IAsyncEnumerator _currentEnumerator; - private int _mode; - - private const int State_OuterNext = 1; - private const int State_While = 4; - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _outerEnumerator = _source.GetAsyncEnumerator(_cancellationToken); - _mode = State_OuterNext; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - switch (_mode) - { - case State_OuterNext: - if (await _outerEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - // make sure we dispose the previous one if we're about to replace it - if (_currentEnumerator != null) - { - await _currentEnumerator.DisposeAsync().ConfigureAwait(false); - } - - _currentEnumerator = _outerEnumerator.Current.GetAsyncEnumerator(_cancellationToken); - - _mode = State_While; - goto case State_While; - } - - break; - case State_While: - if (await _currentEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _currentEnumerator.Current; - return true; - } - - // No more on the inner enumerator, move to the next outer - goto case State_OuterNext; - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } -#endif } } diff --git a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Defer.cs b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Defer.cs index 4949dbfb57..06b45ad609 100644 --- a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Defer.cs +++ b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Defer.cs @@ -3,7 +3,6 @@ // 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; @@ -16,7 +15,6 @@ public static IAsyncEnumerable Defer(Func Core(CancellationToken cancellationToken) @@ -26,9 +24,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) yield return item; } } -#else - return new DeferIterator(factory); -#endif } public static IAsyncEnumerable Defer(Func>> factory) @@ -36,7 +31,6 @@ public static IAsyncEnumerable Defer(Func Core(CancellationToken cancellationToken) @@ -46,9 +40,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) yield return item; } } -#else - return new AsyncDeferIterator(factory); -#endif } #if !NO_DEEP_CANCELLATION @@ -57,7 +48,6 @@ public static IAsyncEnumerable Defer(Func Core(CancellationToken cancellationToken) @@ -67,184 +57,7 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) yield return item; } } -#else - return new AsyncDeferIteratorWithCancellation(factory); -#endif - } -#endif - -#if !USE_ASYNC_ITERATOR - private sealed class DeferIterator : AsyncIteratorBase - { - private readonly Func> _factory; - private IAsyncEnumerator _enumerator; - - public DeferIterator(Func> factory) - { - Debug.Assert(factory != null); - - _factory = factory; - } - - public override T Current => _enumerator == null ? default : _enumerator.Current; - - public override AsyncIteratorBase Clone() - { - return new DeferIterator(_factory); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override ValueTask MoveNextCore() - { - if (_enumerator == null) - { - return InitializeAndMoveNextAsync(); - } - - return _enumerator.MoveNextAsync(); - } - - private async ValueTask InitializeAndMoveNextAsync() - { - // NB: Using an async method to ensure any exception is reported via the task. - - try - { - _enumerator = _factory().GetAsyncEnumerator(_cancellationToken); - } - catch (Exception ex) - { - _enumerator = Throw(ex).GetAsyncEnumerator(_cancellationToken); - throw; - } - - return await _enumerator.MoveNextAsync().ConfigureAwait(false); - } - } - - private sealed class AsyncDeferIterator : AsyncIteratorBase - { - private readonly Func>> _factory; - private IAsyncEnumerator _enumerator; - - public AsyncDeferIterator(Func< Task>> factory) - { - Debug.Assert(factory != null); - - _factory = factory; - } - - public override T Current => _enumerator == null ? default : _enumerator.Current; - - public override AsyncIteratorBase Clone() - { - return new AsyncDeferIterator(_factory); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override ValueTask MoveNextCore() - { - if (_enumerator == null) - { - return InitializeAndMoveNextAsync(); - } - - return _enumerator.MoveNextAsync(); - } - - private async ValueTask InitializeAndMoveNextAsync() - { - try - { - _enumerator = (await _factory().ConfigureAwait(false)).GetAsyncEnumerator(_cancellationToken); - } - catch (Exception ex) - { - _enumerator = Throw(ex).GetAsyncEnumerator(_cancellationToken); - throw; - } - - return await _enumerator.MoveNextAsync().ConfigureAwait(false); - } - } - -#if !NO_DEEP_CANCELLATION - private sealed class AsyncDeferIteratorWithCancellation : AsyncIteratorBase - { - private readonly Func>> _factory; - private IAsyncEnumerator _enumerator; - - public AsyncDeferIteratorWithCancellation(Func>> factory) - { - Debug.Assert(factory != null); - - _factory = factory; - } - - public override T Current => _enumerator == null ? default : _enumerator.Current; - - public override AsyncIteratorBase Clone() - { - return new AsyncDeferIteratorWithCancellation(_factory); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override ValueTask MoveNextCore() - { - if (_enumerator == null) - { - return InitializeAndMoveNextAsync(); - } - - return _enumerator.MoveNextAsync(); - } - - private async ValueTask InitializeAndMoveNextAsync() - { - try - { - _enumerator = (await _factory(_cancellationToken).ConfigureAwait(false)).GetAsyncEnumerator(_cancellationToken); - } - catch (Exception ex) - { - _enumerator = Throw(ex).GetAsyncEnumerator(_cancellationToken); - throw; - } - - return await _enumerator.MoveNextAsync().ConfigureAwait(false); - } } -#endif #endif } } diff --git a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/DistinctUntilChanged.cs b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/DistinctUntilChanged.cs index edb320ee51..5767001fc0 100644 --- a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/DistinctUntilChanged.cs +++ b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/DistinctUntilChanged.cs @@ -3,7 +3,6 @@ // 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; @@ -93,7 +92,6 @@ public static IAsyncEnumerable DistinctUntilChanged(this private static IAsyncEnumerable DistinctUntilChangedCore(IAsyncEnumerable source, IEqualityComparer comparer) { -#if USE_ASYNC_ITERATOR comparer ??= EqualityComparer.Default; return AsyncEnumerable.Create(Core); @@ -124,14 +122,10 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new DistinctUntilChangedAsyncIterator(source, comparer); -#endif } private static IAsyncEnumerable DistinctUntilChangedCore(IAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) { -#if USE_ASYNC_ITERATOR comparer ??= EqualityComparer.Default; return AsyncEnumerable.Create(Core); @@ -166,14 +160,10 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new DistinctUntilChangedAsyncIterator(source, keySelector, comparer); -#endif } private static IAsyncEnumerable DistinctUntilChangedCore(IAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) { -#if USE_ASYNC_ITERATOR comparer ??= EqualityComparer.Default; return AsyncEnumerable.Create(Core); @@ -208,15 +198,11 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new DistinctUntilChangedAsyncIteratorWithTask(source, keySelector, comparer); -#endif } #if !NO_DEEP_CANCELLATION private static IAsyncEnumerable DistinctUntilChangedCore(IAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) { -#if USE_ASYNC_ITERATOR comparer ??= EqualityComparer.Default; return AsyncEnumerable.Create(Core); @@ -251,301 +237,7 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new DistinctUntilChangedAsyncIteratorWithTaskAndCancellation(source, keySelector, comparer); -#endif - } -#endif - -#if !USE_ASYNC_ITERATOR - private sealed class DistinctUntilChangedAsyncIterator : AsyncIterator - { - private readonly IEqualityComparer _comparer; - private readonly IAsyncEnumerable _source; - - private TSource _currentValue; - private IAsyncEnumerator _enumerator; - private bool _hasCurrentValue; - - public DistinctUntilChangedAsyncIterator(IAsyncEnumerable source, IEqualityComparer comparer) - { - Debug.Assert(source != null); - - _source = source; - _comparer = comparer ?? EqualityComparer.Default; - } - - public override AsyncIteratorBase Clone() - { - return new DistinctUntilChangedAsyncIterator(_source, _comparer); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - _currentValue = default; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - var comparerEquals = false; - - if (_hasCurrentValue) - { - comparerEquals = _comparer.Equals(_currentValue, item); - } - - if (!_hasCurrentValue || !comparerEquals) - { - _hasCurrentValue = true; - _currentValue = item; - _current = item; - return true; - } - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - - private sealed class DistinctUntilChangedAsyncIterator : AsyncIterator - { - private readonly IEqualityComparer _comparer; - private readonly Func _keySelector; - private readonly IAsyncEnumerable _source; - private TKey _currentKeyValue; - - private IAsyncEnumerator _enumerator; - private bool _hasCurrentKey; - - public DistinctUntilChangedAsyncIterator(IAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) - { - _source = source; - _keySelector = keySelector; - _comparer = comparer ?? EqualityComparer.Default; - } - - public override AsyncIteratorBase Clone() - { - return new DistinctUntilChangedAsyncIterator(_source, _keySelector, _comparer); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - _currentKeyValue = default; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - var key = _keySelector(item); - var comparerEquals = false; - - if (_hasCurrentKey) - { - comparerEquals = _comparer.Equals(_currentKeyValue, key); - } - - if (!_hasCurrentKey || !comparerEquals) - { - _hasCurrentKey = true; - _currentKeyValue = key; - _current = item; - return true; - } - } - - break; // case - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - - private sealed class DistinctUntilChangedAsyncIteratorWithTask : AsyncIterator - { - private readonly IEqualityComparer _comparer; - private readonly Func> _keySelector; - private readonly IAsyncEnumerable _source; - private TKey _currentKeyValue; - - private IAsyncEnumerator _enumerator; - private bool _hasCurrentKey; - - public DistinctUntilChangedAsyncIteratorWithTask(IAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) - { - _source = source; - _keySelector = keySelector; - _comparer = comparer ?? EqualityComparer.Default; - } - - public override AsyncIteratorBase Clone() - { - return new DistinctUntilChangedAsyncIteratorWithTask(_source, _keySelector, _comparer); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - _currentKeyValue = default; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - var key = await _keySelector(item).ConfigureAwait(false); - var comparerEquals = false; - - if (_hasCurrentKey) - { - comparerEquals = _comparer.Equals(_currentKeyValue, key); - } - - if (!_hasCurrentKey || !comparerEquals) - { - _hasCurrentKey = true; - _currentKeyValue = key; - _current = item; - return true; - } - } - - break; // case - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - -#if !NO_DEEP_CANCELLATION - private sealed class DistinctUntilChangedAsyncIteratorWithTaskAndCancellation : AsyncIterator - { - private readonly IEqualityComparer _comparer; - private readonly Func> _keySelector; - private readonly IAsyncEnumerable _source; - private TKey _currentKeyValue; - - private IAsyncEnumerator _enumerator; - private bool _hasCurrentKey; - - public DistinctUntilChangedAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) - { - _source = source; - _keySelector = keySelector; - _comparer = comparer ?? EqualityComparer.Default; - } - - public override AsyncIteratorBase Clone() - { - return new DistinctUntilChangedAsyncIteratorWithTaskAndCancellation(_source, _keySelector, _comparer); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - _currentKeyValue = default; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - var key = await _keySelector(item, _cancellationToken).ConfigureAwait(false); - var comparerEquals = false; - - if (_hasCurrentKey) - { - comparerEquals = _comparer.Equals(_currentKeyValue, key); - } - - if (!_hasCurrentKey || !comparerEquals) - { - _hasCurrentKey = true; - _currentKeyValue = key; - _current = item; - return true; - } - } - - break; // case - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } } -#endif #endif } } diff --git a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Do.cs b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Do.cs index 19ee9e02c6..00262e9ec9 100644 --- a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Do.cs +++ b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Do.cs @@ -3,7 +3,6 @@ // 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; @@ -171,7 +170,6 @@ public static IAsyncEnumerable Do(this IAsyncEnumerable DoCore(IAsyncEnumerable source, Action onNext, Action onError, Action onCompleted) { -#if USE_ASYNC_ITERATOR return AsyncEnumerable.Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -209,14 +207,10 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) onCompleted?.Invoke(); } } -#else - return new DoAsyncIterator(source, onNext, onError, onCompleted); -#endif } private static IAsyncEnumerable DoCore(IAsyncEnumerable source, Func onNext, Func onError, Func onCompleted) { -#if USE_ASYNC_ITERATOR return AsyncEnumerable.Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -257,15 +251,11 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new DoAsyncIteratorWithTask(source, onNext, onError, onCompleted); -#endif } #if !NO_DEEP_CANCELLATION private static IAsyncEnumerable DoCore(IAsyncEnumerable source, Func onNext, Func onError, Func onCompleted) { -#if USE_ASYNC_ITERATOR return AsyncEnumerable.Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -306,248 +296,7 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new DoAsyncIteratorWithTaskAndCancellation(source, onNext, onError, onCompleted); -#endif - } -#endif - -#if !USE_ASYNC_ITERATOR - private sealed class DoAsyncIterator : AsyncIterator - { - private readonly Action _onCompleted; - private readonly Action _onError; - private readonly Action _onNext; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - - public DoAsyncIterator(IAsyncEnumerable source, Action onNext, Action onError, Action onCompleted) - { - Debug.Assert(source != null); - Debug.Assert(onNext != null); - - _source = source; - _onNext = onNext; - _onError = onError; - _onCompleted = onCompleted; - } - - public override AsyncIteratorBase Clone() - { - return new DoAsyncIterator(_source, _onNext, _onError, _onCompleted); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - try - { - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - _onNext(_current); - - return true; - } - } - catch (OperationCanceledException) - { - throw; - } - catch (Exception ex) when (_onError != null) - { - _onError(ex); - throw; - } - - _onCompleted?.Invoke(); - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } } - - private sealed class DoAsyncIteratorWithTask : AsyncIterator - { - private readonly Func _onCompleted; - private readonly Func _onError; - private readonly Func _onNext; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - - public DoAsyncIteratorWithTask(IAsyncEnumerable source, Func onNext, Func onError, Func onCompleted) - { - Debug.Assert(source != null); - Debug.Assert(onNext != null); - - _source = source; - _onNext = onNext; - _onError = onError; - _onCompleted = onCompleted; - } - - public override AsyncIteratorBase Clone() - { - return new DoAsyncIteratorWithTask(_source, _onNext, _onError, _onCompleted); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - try - { - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - await _onNext(_current).ConfigureAwait(false); - - return true; - } - } - catch (OperationCanceledException) - { - throw; - } - catch (Exception ex) when (_onError != null) - { - await _onError(ex).ConfigureAwait(false); - throw; - } - - if (_onCompleted != null) - { - await _onCompleted().ConfigureAwait(false); - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } - -#if !NO_DEEP_CANCELLATION - private sealed class DoAsyncIteratorWithTaskAndCancellation : AsyncIterator - { - private readonly Func _onCompleted; - private readonly Func _onError; - private readonly Func _onNext; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - - public DoAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable source, Func onNext, Func onError, Func onCompleted) - { - Debug.Assert(source != null); - Debug.Assert(onNext != null); - - _source = source; - _onNext = onNext; - _onError = onError; - _onCompleted = onCompleted; - } - - public override AsyncIteratorBase Clone() - { - return new DoAsyncIteratorWithTaskAndCancellation(_source, _onNext, _onError, _onCompleted); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - try - { - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - await _onNext(_current, _cancellationToken).ConfigureAwait(false); - - return true; - } - } - catch (OperationCanceledException) - { - throw; - } - catch (Exception ex) when (_onError != null) - { - await _onError(ex, _cancellationToken).ConfigureAwait(false); - throw; - } - - if (_onCompleted != null) - { - await _onCompleted(_cancellationToken).ConfigureAwait(false); - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } -#endif #endif } } diff --git a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Expand.cs b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Expand.cs index d89d7668f7..3574a7396e 100644 --- a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Expand.cs +++ b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Expand.cs @@ -3,7 +3,6 @@ // 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; @@ -18,7 +17,6 @@ public static IAsyncEnumerable Expand(this IAsyncEnumerable Core(CancellationToken cancellationToken) @@ -37,9 +35,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new ExpandAsyncIterator(source, selector); -#endif } public static IAsyncEnumerable Expand(this IAsyncEnumerable source, Func>> selector) @@ -49,7 +44,6 @@ public static IAsyncEnumerable Expand(this IAsyncEnumerable Core(CancellationToken cancellationToken) @@ -68,9 +62,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new ExpandAsyncIteratorWithTask(source, selector); -#endif } #if !NO_DEEP_CANCELLATION @@ -81,7 +72,6 @@ public static IAsyncEnumerable Expand(this IAsyncEnumerable Core(CancellationToken cancellationToken) @@ -100,284 +90,7 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new ExpandAsyncIteratorWithTaskAndCancellation(source, selector); -#endif - } -#endif - -#if !USE_ASYNC_ITERATOR - private sealed class ExpandAsyncIterator : AsyncIterator - { - private readonly Func> _selector; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - - private Queue> _queue; - - public ExpandAsyncIterator(IAsyncEnumerable source, Func> selector) - { - Debug.Assert(source != null); - Debug.Assert(selector != null); - - _source = source; - _selector = selector; - } - - public override AsyncIteratorBase Clone() - { - return new ExpandAsyncIterator(_source, _selector); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - _queue = null; - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _queue = new Queue>(); - _queue.Enqueue(_source); - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (true) - { - if (_enumerator == null) - { - if (_queue.Count > 0) - { - var src = _queue.Dequeue(); - - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - } - - _enumerator = src.GetAsyncEnumerator(_cancellationToken); - - continue; // loop - } - - break; // while - } - - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - var next = _selector(item); - _queue.Enqueue(next); - _current = item; - return true; - } - - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - break; // case - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - - private sealed class ExpandAsyncIteratorWithTask : AsyncIterator - { - private readonly Func>> _selector; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - - private Queue> _queue; - - public ExpandAsyncIteratorWithTask(IAsyncEnumerable source, Func>> selector) - { - Debug.Assert(source != null); - Debug.Assert(selector != null); - - _source = source; - _selector = selector; - } - - public override AsyncIteratorBase Clone() - { - return new ExpandAsyncIteratorWithTask(_source, _selector); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - _queue = null; - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _queue = new Queue>(); - _queue.Enqueue(_source); - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (true) - { - if (_enumerator == null) - { - if (_queue.Count > 0) - { - var src = _queue.Dequeue(); - - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - } - - _enumerator = src.GetAsyncEnumerator(_cancellationToken); - - continue; // loop - } - - break; // while - } - - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - var next = await _selector(item).ConfigureAwait(false); - _queue.Enqueue(next); - _current = item; - return true; - } - - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - break; // case - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - -#if !NO_DEEP_CANCELLATION - private sealed class ExpandAsyncIteratorWithTaskAndCancellation : AsyncIterator - { - private readonly Func>> _selector; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - - private Queue> _queue; - - public ExpandAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable source, Func>> selector) - { - Debug.Assert(source != null); - Debug.Assert(selector != null); - - _source = source; - _selector = selector; - } - - public override AsyncIteratorBase Clone() - { - return new ExpandAsyncIteratorWithTaskAndCancellation(_source, _selector); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - _queue = null; - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _queue = new Queue>(); - _queue.Enqueue(_source); - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (true) - { - if (_enumerator == null) - { - if (_queue.Count > 0) - { - var src = _queue.Dequeue(); - - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - } - - _enumerator = src.GetAsyncEnumerator(_cancellationToken); - - continue; // loop - } - - break; // while - } - - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - var next = await _selector(item, _cancellationToken).ConfigureAwait(false); - _queue.Enqueue(next); - _current = item; - return true; - } - - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - break; // case - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } } -#endif #endif } } diff --git a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Finally.cs b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Finally.cs index b8d7c3ae39..46ed242765 100644 --- a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Finally.cs +++ b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Finally.cs @@ -3,7 +3,6 @@ // 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; @@ -18,7 +17,6 @@ public static IAsyncEnumerable Finally(this IAsyncEnumerable Core(CancellationToken cancellationToken) @@ -35,9 +33,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) finallyAction(); } } -#else - return new FinallyAsyncIterator(source, finallyAction); -#endif } public static IAsyncEnumerable Finally(this IAsyncEnumerable source, Func finallyAction) @@ -47,7 +42,6 @@ public static IAsyncEnumerable Finally(this IAsyncEnumerable Core(CancellationToken cancellationToken) @@ -61,132 +55,11 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } finally { - await finallyAction().ConfigureAwait(false); - } - } -#else - return new FinallyAsyncIteratorWithTask(source, finallyAction); -#endif - } - - // REVIEW: No cancellation support for finally action. - -#if !USE_ASYNC_ITERATOR - private sealed class FinallyAsyncIterator : AsyncIterator - { - private readonly Action _finallyAction; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - - public FinallyAsyncIterator(IAsyncEnumerable source, Action finallyAction) - { - Debug.Assert(source != null); - Debug.Assert(finallyAction != null); - - _source = source; - _finallyAction = finallyAction; - } - - public override AsyncIteratorBase Clone() - { - return new FinallyAsyncIterator(_source, _finallyAction); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - - _finallyAction(); - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } - - private sealed class FinallyAsyncIteratorWithTask : AsyncIterator - { - private readonly Func _finallyAction; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - - public FinallyAsyncIteratorWithTask(IAsyncEnumerable source, Func finallyAction) - { - Debug.Assert(source != null); - Debug.Assert(finallyAction != null); - - _source = source; - _finallyAction = finallyAction; - } - - public override AsyncIteratorBase Clone() - { - return new FinallyAsyncIteratorWithTask(_source, _finallyAction); - } + // REVIEW: No cancellation support for finally action. - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - - await _finallyAction().ConfigureAwait(false); - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - await DisposeAsync().ConfigureAwait(false); - break; + await finallyAction().ConfigureAwait(false); } - - return false; } } -#endif } } diff --git a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Generate.cs b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Generate.cs index 83a75254df..2ac8feea91 100644 --- a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Generate.cs +++ b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Generate.cs @@ -3,14 +3,14 @@ // 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 { + // REVIEW: Add async variant? + public static IAsyncEnumerable Generate(TState initialState, Func condition, Func iterate, Func resultSelector) { if (condition == null) @@ -20,7 +20,6 @@ public static IAsyncEnumerable Generate(TState initial if (resultSelector == null) throw Error.ArgumentNull(nameof(resultSelector)); -#if USE_ASYNC_ITERATOR return AsyncEnumerable.Create(Core); #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously @@ -34,81 +33,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously -#else - return new GenerateAsyncIterator(initialState, condition, iterate, resultSelector); -#endif - } - - // REVIEW: Add async variant? - -#if !USE_ASYNC_ITERATOR - private sealed class GenerateAsyncIterator : AsyncIterator - { - private readonly Func _condition; - private readonly TState _initialState; - private readonly Func _iterate; - private readonly Func _resultSelector; - - private TState _currentState; - - private bool _started; - - public GenerateAsyncIterator(TState initialState, Func condition, Func iterate, Func resultSelector) - { - Debug.Assert(condition != null); - Debug.Assert(iterate != null); - Debug.Assert(resultSelector != null); - - _initialState = initialState; - _condition = condition; - _iterate = iterate; - _resultSelector = resultSelector; - } - - public override AsyncIteratorBase Clone() - { - return new GenerateAsyncIterator(_initialState, _condition, _iterate, _resultSelector); - } - - public override async ValueTask DisposeAsync() - { - _currentState = default; - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask 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; - } } -#endif } } diff --git a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/IgnoreElements.cs b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/IgnoreElements.cs index 1821ba53a0..5e07037457 100644 --- a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/IgnoreElements.cs +++ b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/IgnoreElements.cs @@ -3,7 +3,6 @@ // 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; @@ -16,7 +15,6 @@ public static IAsyncEnumerable IgnoreElements(this IAsyncEnume if (source == null) throw Error.ArgumentNull(nameof(source)); -#if USE_ASYNC_ITERATOR return AsyncEnumerable.Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -27,61 +25,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) yield break; } -#else - return new IgnoreElementsAsyncIterator(source); -#endif } - -#if !USE_ASYNC_ITERATOR - private sealed class IgnoreElementsAsyncIterator : AsyncIterator - { - private readonly IAsyncEnumerable _source; - private IAsyncEnumerator _enumerator; - - public IgnoreElementsAsyncIterator(IAsyncEnumerable source) - { - Debug.Assert(source != null); - - _source = source; - } - - public override AsyncIteratorBase Clone() - { - return new IgnoreElementsAsyncIterator(_source); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - } - - break; // case - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } -#endif } } diff --git a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Merge.cs b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Merge.cs index 0aba29e83a..f51855ae20 100644 --- a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Merge.cs +++ b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Merge.cs @@ -3,7 +3,6 @@ // 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; @@ -170,7 +169,7 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#elif USE_ASYNC_ITERATOR +#else return AsyncEnumerable.Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -294,8 +293,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new MergeAsyncIterator(sources); #endif } @@ -338,107 +335,5 @@ public static IAsyncEnumerable Merge(this IAsyncEnumerable source); } - - private sealed class MergeAsyncIterator : AsyncIterator - { - private readonly IAsyncEnumerable[] _sources; - - private IAsyncEnumerator[] _enumerators; - private Task[] _moveNexts; - private int _active; - - public MergeAsyncIterator(IAsyncEnumerable[] sources) - { - Debug.Assert(sources != null); - - _sources = sources; - } - - public override AsyncIteratorBase Clone() - { - return new MergeAsyncIterator(_sources); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerators != null) - { - var n = _enumerators.Length; - - var disposes = new ValueTask[n]; - - for (var i = 0; i < n; i++) - { - var dispose = _enumerators[i].DisposeAsync(); - disposes[i] = dispose; - } - - await Task.WhenAll(disposes.Select(t => t.AsTask())).ConfigureAwait(false); - _enumerators = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - var n = _sources.Length; - - _enumerators = new IAsyncEnumerator[n]; - _moveNexts = new Task[n]; - _active = n; - - for (var i = 0; i < n; i++) - { - var enumerator = _sources[i].GetAsyncEnumerator(_cancellationToken); - _enumerators[i] = enumerator; - _moveNexts[i] = enumerator.MoveNextAsync().AsTask(); - } - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (_active > 0) - { - // - // REVIEW: This approach does have a bias towards giving sources on the left - // priority over sources on the right when yielding values. We may - // want to consider a "prefer fairness" option. - // - - var moveNext = await Task.WhenAny(_moveNexts).ConfigureAwait(false); - - // REVIEW: This seems wrong. AsTask can return the original Task (if the ValueTask - // is wrapping one) or return a singleton instance for true and false, at which point - // the use of IndexOf may pick an element closer to the start of the array because of - // reference equality checks and aliasing effects. See GetTaskForResult in the BCL. - - var index = Array.IndexOf(_moveNexts, moveNext); - - if (!await moveNext.ConfigureAwait(false)) - { - _moveNexts[index] = TaskExt.Never; - _active--; - } - else - { - var enumerator = _enumerators[index]; - _current = enumerator.Current; - _moveNexts[index] = enumerator.MoveNextAsync().AsTask(); - return true; - } - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } } } diff --git a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Repeat.cs b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Repeat.cs index e6d08eeb97..3c5c05c732 100644 --- a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Repeat.cs +++ b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Repeat.cs @@ -3,7 +3,6 @@ // 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; @@ -13,7 +12,6 @@ public static partial class AsyncEnumerableEx { public static IAsyncEnumerable Repeat(TResult element) { -#if USE_ASYNC_ITERATOR return AsyncEnumerable.Create(Core); #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously @@ -27,9 +25,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously -#else - return new RepeatElementAsyncIterator(element); -#endif } public static IAsyncEnumerable Repeat(this IAsyncEnumerable source) @@ -37,7 +32,6 @@ public static IAsyncEnumerable Repeat(this IAsyncEnumerable Core(CancellationToken cancellationToken) @@ -50,9 +44,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new RepeatSequenceAsyncIterator(source, -1); -#endif } public static IAsyncEnumerable Repeat(this IAsyncEnumerable source, int count) @@ -62,7 +53,6 @@ public static IAsyncEnumerable Repeat(this IAsyncEnumerable Core(CancellationToken cancellationToken) @@ -75,102 +65,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new RepeatSequenceAsyncIterator(source, count); -#endif } - -#if !USE_ASYNC_ITERATOR - private sealed class RepeatElementAsyncIterator : AsyncIterator - { - private readonly TResult _element; - - public RepeatElementAsyncIterator(TResult element) => _element = element; - - public override AsyncIteratorBase Clone() - { - return new RepeatElementAsyncIterator(_element); - } - - protected override ValueTask MoveNextCore() - { - _cancellationToken.ThrowIfCancellationRequested(); - - _current = _element; - return new ValueTask(true); - } - } - - private sealed class RepeatSequenceAsyncIterator : AsyncIterator - { - private readonly int _count; - private readonly bool _isInfinite; - private readonly IAsyncEnumerable _source; - - private int _currentCount; - private IAsyncEnumerator _enumerator; - - public RepeatSequenceAsyncIterator(IAsyncEnumerable source, int count) - { - Debug.Assert(source != null); - - _source = source; - _count = count; - _isInfinite = count < 0; - _currentCount = count; - } - - public override AsyncIteratorBase Clone() - { - return new RepeatSequenceAsyncIterator(_source, _count); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - if (!_isInfinite && _currentCount-- == 0) - break; - - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _state = AsyncIteratorState.Iterating; - - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - goto case AsyncIteratorState.Allocated; - } - - await DisposeAsync().ConfigureAwait(false); - - return false; - } - } -#endif } } diff --git a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Scan.cs b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Scan.cs index 4e6f9c9102..9951cb9938 100644 --- a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Scan.cs +++ b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Scan.cs @@ -3,7 +3,6 @@ // 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; @@ -22,7 +21,6 @@ public static IAsyncEnumerable Scan(this IAsyncEnumerable Core(CancellationToken cancellationToken) @@ -44,9 +42,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new ScanAsyncEnumerable(source, accumulator); -#endif } public static IAsyncEnumerable Scan(this IAsyncEnumerable source, TAccumulate seed, Func accumulator) @@ -56,7 +51,6 @@ public static IAsyncEnumerable Scan(this IAsy if (accumulator == null) throw Error.ArgumentNull(nameof(accumulator)); -#if USE_ASYNC_ITERATOR return AsyncEnumerable.Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -70,9 +64,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) yield return res; } } -#else - return new ScanAsyncEnumerable(source, seed, accumulator); -#endif } public static IAsyncEnumerable Scan(this IAsyncEnumerable source, Func> accumulator) @@ -82,7 +73,6 @@ public static IAsyncEnumerable Scan(this IAsyncEnumerable Core(CancellationToken cancellationToken) @@ -104,9 +94,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new ScanAsyncEnumerableWithTask(source, accumulator); -#endif } #if !NO_DEEP_CANCELLATION @@ -117,7 +104,6 @@ public static IAsyncEnumerable Scan(this IAsyncEnumerable Core(CancellationToken cancellationToken) @@ -139,9 +125,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new ScanAsyncEnumerableWithTaskAndCancellation(source, accumulator); -#endif } #endif @@ -152,7 +135,6 @@ public static IAsyncEnumerable Scan(this IAsy if (accumulator == null) throw Error.ArgumentNull(nameof(accumulator)); -#if USE_ASYNC_ITERATOR return AsyncEnumerable.Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -166,9 +148,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) yield return res; } } -#else - return new ScanAsyncEnumerableWithTask(source, seed, accumulator); -#endif } #if !NO_DEEP_CANCELLATION @@ -179,7 +158,6 @@ public static IAsyncEnumerable Scan(this IAsy if (accumulator == null) throw Error.ArgumentNull(nameof(accumulator)); -#if USE_ASYNC_ITERATOR return AsyncEnumerable.Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -193,435 +171,7 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) yield return res; } } -#else - return new ScanAsyncEnumerableWithTaskAndCancellation(source, seed, accumulator); -#endif - } -#endif - -#if !USE_ASYNC_ITERATOR - 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 AsyncIteratorBase 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() - { - 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 AsyncIteratorBase 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() - { - 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 AsyncIteratorBase 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() - { - 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; - } - } - -#if !NO_DEEP_CANCELLATION - private sealed class ScanAsyncEnumerableWithTaskAndCancellation : AsyncIterator - { - private readonly Func> _accumulator; - private readonly IAsyncEnumerable _source; - - private TSource _accumulated; - private IAsyncEnumerator _enumerator; - - private bool _hasSeed; - - public ScanAsyncEnumerableWithTaskAndCancellation(IAsyncEnumerable source, Func> accumulator) - { - Debug.Assert(source != null); - Debug.Assert(accumulator != null); - - _source = source; - _accumulator = accumulator; - } - - public override AsyncIteratorBase Clone() - { - return new ScanAsyncEnumerableWithTaskAndCancellation(_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() - { - 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, _cancellationToken).ConfigureAwait(false); - _current = _accumulated; - return true; - } - - break; // case - - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } -#endif - - 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 AsyncIteratorBase 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() - { - 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; - } - } - -#if !NO_DEEP_CANCELLATION - private sealed class ScanAsyncEnumerableWithTaskAndCancellation : AsyncIterator - { - private readonly Func> _accumulator; - private readonly TAccumulate _seed; - private readonly IAsyncEnumerable _source; - - private TAccumulate _accumulated; - private IAsyncEnumerator _enumerator; - - public ScanAsyncEnumerableWithTaskAndCancellation(IAsyncEnumerable source, TAccumulate seed, Func> accumulator) - { - Debug.Assert(source != null); - Debug.Assert(accumulator != null); - - _source = source; - _seed = seed; - _accumulator = accumulator; - } - - public override AsyncIteratorBase Clone() - { - return new ScanAsyncEnumerableWithTaskAndCancellation(_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() - { - 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, _cancellationToken).ConfigureAwait(false); - _current = _accumulated; - return true; - } - - break; - - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } -#endif #endif } } diff --git a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Using.cs b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Using.cs index 40e7c452ad..067ef91663 100644 --- a/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Using.cs +++ b/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Using.cs @@ -3,7 +3,6 @@ // 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; @@ -20,7 +19,6 @@ public static IAsyncEnumerable Using(Func Core(CancellationToken cancellationToken) @@ -33,9 +31,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new UsingAsyncIterator(resourceFactory, enumerableFactory); -#endif } public static IAsyncEnumerable Using(Func> resourceFactory, Func>> enumerableFactory) where TResource : IDisposable @@ -45,7 +40,6 @@ public static IAsyncEnumerable Using(Func Core(CancellationToken cancellationToken) @@ -58,9 +52,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new UsingAsyncIteratorWithTask(resourceFactory, enumerableFactory); -#endif } #if !NO_DEEP_CANCELLATION @@ -71,7 +62,6 @@ public static IAsyncEnumerable Using(Func Core(CancellationToken cancellationToken) @@ -84,216 +74,7 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new UsingAsyncIteratorWithTaskAndCancellation(resourceFactory, enumerableFactory); -#endif } -#endif - -#if !USE_ASYNC_ITERATOR - private sealed class UsingAsyncIterator : AsyncIterator where TResource : IDisposable - { - private readonly Func> _enumerableFactory; - private readonly Func _resourceFactory; - - private IAsyncEnumerator _enumerator; - private TResource _resource; - - public UsingAsyncIterator(Func resourceFactory, Func> enumerableFactory) - { - Debug.Assert(resourceFactory != null); - Debug.Assert(enumerableFactory != null); - - _resourceFactory = resourceFactory; - _enumerableFactory = enumerableFactory; - } - - public override AsyncIteratorBase Clone() - { - return new UsingAsyncIterator(_resourceFactory, _enumerableFactory); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - if (_resource != null) - { - _resource.Dispose(); - _resource = default; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - // NB: Earlier behavior of this operator was more eager, causing the resource factory to be called upon calling - // GetAsyncEnumerator. This is inconsistent with asynchronous "using" and with a C# 8.0 async iterator with - // a using statement inside, so this logic got moved to MoveNextAsync instead. - - switch (_state) - { - case AsyncIteratorState.Allocated: - _resource = _resourceFactory(); - _enumerator = _enumerableFactory(_resource).GetAsyncEnumerator(_cancellationToken); - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } - - private sealed class UsingAsyncIteratorWithTask : AsyncIterator where TResource : IDisposable - { - private readonly Func>> _enumerableFactory; - private readonly Func> _resourceFactory; - - private IAsyncEnumerable _enumerable; - private IAsyncEnumerator _enumerator; - private TResource _resource; - - public UsingAsyncIteratorWithTask(Func> resourceFactory, Func>> enumerableFactory) - { - Debug.Assert(resourceFactory != null); - Debug.Assert(enumerableFactory != null); - - _resourceFactory = resourceFactory; - _enumerableFactory = enumerableFactory; - } - - public override AsyncIteratorBase Clone() - { - return new UsingAsyncIteratorWithTask(_resourceFactory, _enumerableFactory); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - if (_resource != null) - { - _resource.Dispose(); - _resource = default; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _resource = await _resourceFactory().ConfigureAwait(false); - _enumerable = await _enumerableFactory(_resource).ConfigureAwait(false); - - _enumerator = _enumerable.GetAsyncEnumerator(_cancellationToken); - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } - -#if !NO_DEEP_CANCELLATION - private sealed class UsingAsyncIteratorWithTaskAndCancellation : AsyncIterator where TResource : IDisposable - { - private readonly Func>> _enumerableFactory; - private readonly Func> _resourceFactory; - - private IAsyncEnumerable _enumerable; - private IAsyncEnumerator _enumerator; - private TResource _resource; - - public UsingAsyncIteratorWithTaskAndCancellation(Func> resourceFactory, Func>> enumerableFactory) - { - Debug.Assert(resourceFactory != null); - Debug.Assert(enumerableFactory != null); - - _resourceFactory = resourceFactory; - _enumerableFactory = enumerableFactory; - } - - public override AsyncIteratorBase Clone() - { - return new UsingAsyncIteratorWithTaskAndCancellation(_resourceFactory, _enumerableFactory); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - if (_resource != null) - { - _resource.Dispose(); - _resource = default; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _resource = await _resourceFactory(_cancellationToken).ConfigureAwait(false); - _enumerable = await _enumerableFactory(_resource, _cancellationToken).ConfigureAwait(false); - - _enumerator = _enumerable.GetAsyncEnumerator(_cancellationToken); - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } -#endif #endif } } diff --git a/Ix.NET/Source/System.Linq.Async.Tests/System/Linq/Operators/SkipLast.cs b/Ix.NET/Source/System.Linq.Async.Tests/System/Linq/Operators/SkipLast.cs index ed27815e07..d0ea1d0bc5 100644 --- a/Ix.NET/Source/System.Linq.Async.Tests/System/Linq/Operators/SkipLast.cs +++ b/Ix.NET/Source/System.Linq.Async.Tests/System/Linq/Operators/SkipLast.cs @@ -63,7 +63,6 @@ public async Task SkipLast_Zero() await NoNextAsync(e); } -#if USE_ASYNC_ITERATOR [Fact] public void SkipLast_Zero_NoAlias() { @@ -78,6 +77,5 @@ private async IAsyncEnumerable Xs() await Task.Yield(); yield return 1; } -#endif } } diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Cast.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Cast.cs index 736a3aa119..986982da8b 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Cast.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Cast.cs @@ -23,7 +23,6 @@ public static IAsyncEnumerable Cast(this IAsyncEnumerable Core(CancellationToken cancellationToken) @@ -33,61 +32,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) yield return (TResult)obj; } } -#else - return new CastAsyncIterator(source); -#endif } - -#if !USE_ASYNC_ITERATOR - private sealed class CastAsyncIterator : AsyncIterator - { - private readonly IAsyncEnumerable _source; - private IAsyncEnumerator _enumerator; - - public CastAsyncIterator(IAsyncEnumerable source) - { - _source = source; - } - - public override AsyncIteratorBase Clone() - { - return new CastAsyncIterator(_source); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = (TResult)_enumerator.Current; - return true; - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } -#endif } } diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Except.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Except.cs index e006c481b3..934280e9ec 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Except.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Except.cs @@ -3,7 +3,6 @@ // 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; @@ -21,7 +20,6 @@ public static IAsyncEnumerable Except(this IAsyncEnumerable Core(CancellationToken cancellationToken) @@ -41,89 +39,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new ExceptAsyncIterator(first, second, comparer); -#endif } - -#if !USE_ASYNC_ITERATOR - private sealed class ExceptAsyncIterator : AsyncIterator - { - private readonly IEqualityComparer _comparer; - private readonly IAsyncEnumerable _first; - private readonly IAsyncEnumerable _second; - - private IAsyncEnumerator _firstEnumerator; - private Set _set; - - public ExceptAsyncIterator(IAsyncEnumerable first, IAsyncEnumerable second, IEqualityComparer comparer) - { - Debug.Assert(first != null); - Debug.Assert(second != null); - - _first = first; - _second = second; - _comparer = comparer; - } - - public override AsyncIteratorBase Clone() - { - return new ExceptAsyncIterator(_first, _second, _comparer); - } - - public override async ValueTask DisposeAsync() - { - if (_firstEnumerator != null) - { - await _firstEnumerator.DisposeAsync().ConfigureAwait(false); - _firstEnumerator = null; - } - - _set = null; - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - // NB: Earlier implementations of this operator constructed the set for the second source concurrently - // with the first MoveNextAsync call on the first source. This resulted in an unexpected source of - // concurrency, which isn't a great default behavior because it's very hard to suppress or control - // this behavior. - - switch (_state) - { - case AsyncIteratorState.Allocated: - _set = await AsyncEnumerableHelpers.ToSet(_second, _comparer, _cancellationToken).ConfigureAwait(false); - _firstEnumerator = _first.GetAsyncEnumerator(_cancellationToken); - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - bool moveNext; - do - { - moveNext = await _firstEnumerator.MoveNextAsync().ConfigureAwait(false); - - if (moveNext) - { - var item = _firstEnumerator.Current; - if (_set.Add(item)) - { - _current = item; - return true; - } - } - } while (moveNext); - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } -#endif } } diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/GroupJoin.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/GroupJoin.cs index d67038a2e6..05be11a3a6 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/GroupJoin.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/GroupJoin.cs @@ -26,7 +26,6 @@ public static IAsyncEnumerable GroupJoin if (resultSelector == null) throw Error.ArgumentNull(nameof(resultSelector)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -47,9 +46,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new GroupJoinAsyncEnumerable(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer); -#endif } internal static IAsyncEnumerable GroupJoinAwaitCore(this IAsyncEnumerable outer, IAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func, ValueTask> resultSelector) => @@ -68,7 +64,6 @@ internal static IAsyncEnumerable GroupJoinAwaitCore Core(CancellationToken cancellationToken) @@ -89,9 +84,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new GroupJoinAsyncEnumerableWithTask(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer); -#endif } #if !NO_DEEP_CANCELLATION @@ -111,7 +103,6 @@ internal static IAsyncEnumerable GroupJoinAwaitWithCancellationCore Core(CancellationToken cancellationToken) @@ -132,308 +123,7 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new GroupJoinAsyncEnumerableWithTaskAndCancellation(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer); -#endif - } -#endif - -#if !USE_ASYNC_ITERATOR - private sealed class GroupJoinAsyncEnumerable : IAsyncEnumerable - { - private readonly IEqualityComparer _comparer; - private readonly IAsyncEnumerable _inner; - private readonly Func _innerKeySelector; - private readonly IAsyncEnumerable _outer; - private readonly Func _outerKeySelector; - private readonly Func, TResult> _resultSelector; - - public GroupJoinAsyncEnumerable( - IAsyncEnumerable outer, - IAsyncEnumerable inner, - Func outerKeySelector, - Func innerKeySelector, - Func, TResult> resultSelector, - IEqualityComparer comparer) - { - _outer = outer; - _inner = inner; - _outerKeySelector = outerKeySelector; - _innerKeySelector = innerKeySelector; - _resultSelector = resultSelector; - _comparer = comparer; - } - - public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken) - { - cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior. - - return new GroupJoinAsyncEnumerator( - _outer.GetAsyncEnumerator(cancellationToken), - _inner, - _outerKeySelector, - _innerKeySelector, - _resultSelector, - _comparer, - cancellationToken); - } - - private sealed class GroupJoinAsyncEnumerator : IAsyncEnumerator - { - private readonly IEqualityComparer _comparer; - private readonly IAsyncEnumerable _inner; - private readonly Func _innerKeySelector; - private readonly IAsyncEnumerator _outer; - private readonly Func _outerKeySelector; - private readonly Func, TResult> _resultSelector; - private readonly CancellationToken _cancellationToken; - - private Internal.Lookup _lookup; - - public GroupJoinAsyncEnumerator( - IAsyncEnumerator outer, - IAsyncEnumerable inner, - Func outerKeySelector, - Func innerKeySelector, - Func, TResult> resultSelector, - IEqualityComparer comparer, - CancellationToken cancellationToken) - { - _outer = outer; - _inner = inner; - _outerKeySelector = outerKeySelector; - _innerKeySelector = innerKeySelector; - _resultSelector = resultSelector; - _comparer = comparer; - _cancellationToken = cancellationToken; - } - - public async ValueTask MoveNextAsync() - { - // nothing to do - if (!await _outer.MoveNextAsync().ConfigureAwait(false)) - { - return false; - } - - if (_lookup == null) - { - _lookup = await Internal.Lookup.CreateForJoinAsync(_inner, _innerKeySelector, _comparer, _cancellationToken).ConfigureAwait(false); - } - - var item = _outer.Current; - - var outerKey = _outerKeySelector(item); - var inner = _lookup[outerKey].ToAsyncEnumerable(); - - Current = _resultSelector(item, inner); - - return true; - } - - public TResult Current { get; private set; } - - public ValueTask DisposeAsync() => _outer.DisposeAsync(); - } } - - private sealed class GroupJoinAsyncEnumerableWithTask : IAsyncEnumerable - { - private readonly IEqualityComparer _comparer; - private readonly IAsyncEnumerable _inner; - private readonly Func> _innerKeySelector; - private readonly IAsyncEnumerable _outer; - private readonly Func> _outerKeySelector; - private readonly Func, ValueTask> _resultSelector; - - public GroupJoinAsyncEnumerableWithTask( - IAsyncEnumerable outer, - IAsyncEnumerable inner, - Func> outerKeySelector, - Func> innerKeySelector, - Func, ValueTask> resultSelector, - IEqualityComparer comparer) - { - _outer = outer; - _inner = inner; - _outerKeySelector = outerKeySelector; - _innerKeySelector = innerKeySelector; - _resultSelector = resultSelector; - _comparer = comparer; - } - - public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken) - { - cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior. - - return new GroupJoinAsyncEnumeratorWithTask( - _outer.GetAsyncEnumerator(cancellationToken), - _inner, - _outerKeySelector, - _innerKeySelector, - _resultSelector, - _comparer, - cancellationToken); - } - - private sealed class GroupJoinAsyncEnumeratorWithTask : IAsyncEnumerator - { - private readonly IEqualityComparer _comparer; - private readonly IAsyncEnumerable _inner; - private readonly Func> _innerKeySelector; - private readonly IAsyncEnumerator _outer; - private readonly Func> _outerKeySelector; - private readonly Func, ValueTask> _resultSelector; - private readonly CancellationToken _cancellationToken; - - private Internal.LookupWithTask _lookup; - - public GroupJoinAsyncEnumeratorWithTask( - IAsyncEnumerator outer, - IAsyncEnumerable inner, - Func> outerKeySelector, - Func> innerKeySelector, - Func, ValueTask> resultSelector, - IEqualityComparer comparer, - CancellationToken cancellationToken) - { - _outer = outer; - _inner = inner; - _outerKeySelector = outerKeySelector; - _innerKeySelector = innerKeySelector; - _resultSelector = resultSelector; - _comparer = comparer; - _cancellationToken = cancellationToken; - } - - public async ValueTask MoveNextAsync() - { - // nothing to do - if (!await _outer.MoveNextAsync().ConfigureAwait(false)) - { - return false; - } - - if (_lookup == null) - { - _lookup = await Internal.LookupWithTask.CreateForJoinAsync(_inner, _innerKeySelector, _comparer, _cancellationToken).ConfigureAwait(false); - } - - var item = _outer.Current; - - var outerKey = await _outerKeySelector(item).ConfigureAwait(false); - var inner = _lookup[outerKey].ToAsyncEnumerable(); - - Current = await _resultSelector(item, inner).ConfigureAwait(false); - - return true; - } - - public TResult Current { get; private set; } - - public ValueTask DisposeAsync() => _outer.DisposeAsync(); - } - } - -#if !NO_DEEP_CANCELLATION - private sealed class GroupJoinAsyncEnumerableWithTaskAndCancellation : IAsyncEnumerable - { - private readonly IEqualityComparer _comparer; - private readonly IAsyncEnumerable _inner; - private readonly Func> _innerKeySelector; - private readonly IAsyncEnumerable _outer; - private readonly Func> _outerKeySelector; - private readonly Func, CancellationToken, ValueTask> _resultSelector; - - public GroupJoinAsyncEnumerableWithTaskAndCancellation( - IAsyncEnumerable outer, - IAsyncEnumerable inner, - Func> outerKeySelector, - Func> innerKeySelector, - Func, CancellationToken, ValueTask> resultSelector, - IEqualityComparer comparer) - { - _outer = outer; - _inner = inner; - _outerKeySelector = outerKeySelector; - _innerKeySelector = innerKeySelector; - _resultSelector = resultSelector; - _comparer = comparer; - } - - public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken) - { - cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior. - - return new GroupJoinAsyncEnumeratorWithTask( - _outer.GetAsyncEnumerator(cancellationToken), - _inner, - _outerKeySelector, - _innerKeySelector, - _resultSelector, - _comparer, - cancellationToken); - } - - private sealed class GroupJoinAsyncEnumeratorWithTask : IAsyncEnumerator - { - private readonly IEqualityComparer _comparer; - private readonly IAsyncEnumerable _inner; - private readonly Func> _innerKeySelector; - private readonly IAsyncEnumerator _outer; - private readonly Func> _outerKeySelector; - private readonly Func, CancellationToken, ValueTask> _resultSelector; - private readonly CancellationToken _cancellationToken; - - private Internal.LookupWithTask _lookup; - - public GroupJoinAsyncEnumeratorWithTask( - IAsyncEnumerator outer, - IAsyncEnumerable inner, - Func> outerKeySelector, - Func> innerKeySelector, - Func, CancellationToken, ValueTask> resultSelector, - IEqualityComparer comparer, - CancellationToken cancellationToken) - { - _outer = outer; - _inner = inner; - _outerKeySelector = outerKeySelector; - _innerKeySelector = innerKeySelector; - _resultSelector = resultSelector; - _comparer = comparer; - _cancellationToken = cancellationToken; - } - - public async ValueTask MoveNextAsync() - { - // nothing to do - if (!await _outer.MoveNextAsync().ConfigureAwait(false)) - { - return false; - } - - if (_lookup == null) - { - _lookup = await Internal.LookupWithTask.CreateForJoinAsync(_inner, _innerKeySelector, _comparer, _cancellationToken).ConfigureAwait(false); - } - - var item = _outer.Current; - - var outerKey = await _outerKeySelector(item, _cancellationToken).ConfigureAwait(false); - var inner = _lookup[outerKey].ToAsyncEnumerable(); - - Current = await _resultSelector(item, inner, _cancellationToken).ConfigureAwait(false); - - return true; - } - - public TResult Current { get; private set; } - - public ValueTask DisposeAsync() => _outer.DisposeAsync(); - } - } -#endif #endif } } diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Intersect.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Intersect.cs index 2a2a6dd149..4606644f4d 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Intersect.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Intersect.cs @@ -21,7 +21,6 @@ public static IAsyncEnumerable Intersect(this IAsyncEnumerable if (second == null) throw Error.ArgumentNull(nameof(second)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -41,90 +40,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new IntersectAsyncIterator(first, second, comparer); -#endif } - -#if !USE_ASYNC_ITERATOR - private sealed class IntersectAsyncIterator : AsyncIterator - { - private readonly IEqualityComparer _comparer; - private readonly IAsyncEnumerable _first; - private readonly IAsyncEnumerable _second; - - private IAsyncEnumerator _firstEnumerator; - private Set _set; - - public IntersectAsyncIterator(IAsyncEnumerable first, IAsyncEnumerable second, IEqualityComparer comparer) - { - Debug.Assert(first != null); - Debug.Assert(second != null); - - _first = first; - _second = second; - _comparer = comparer; - } - - public override AsyncIteratorBase Clone() - { - return new IntersectAsyncIterator(_first, _second, _comparer); - } - - public override async ValueTask DisposeAsync() - { - if (_firstEnumerator != null) - { - await _firstEnumerator.DisposeAsync().ConfigureAwait(false); - _firstEnumerator = null; - } - - _set = null; - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - // NB: Earlier implementations of this operator constructed the set for the second source concurrently - // with the first MoveNextAsync call on the first source. This resulted in an unexpected source of - // concurrency, which isn't a great default behavior because it's very hard to suppress or control - // this behavior. - - switch (_state) - { - case AsyncIteratorState.Allocated: - _set = await AsyncEnumerableHelpers.ToSet(_second, _comparer, _cancellationToken).ConfigureAwait(false); - _firstEnumerator = _first.GetAsyncEnumerator(_cancellationToken); - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - - bool moveNext; - do - { - moveNext = await _firstEnumerator.MoveNextAsync().ConfigureAwait(false); - - if (moveNext) - { - var item = _firstEnumerator.Current; - if (_set.Remove(item)) - { - _current = item; - return true; - } - } - } while (moveNext); - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } -#endif } } diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Join.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Join.cs index c638c3d6b8..f1a5b6d568 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Join.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Join.cs @@ -27,7 +27,6 @@ public static IAsyncEnumerable Join(this if (resultSelector == null) throw Error.ArgumentNull(nameof(resultSelector)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -64,9 +63,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new JoinAsyncIterator(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer); -#endif } internal static IAsyncEnumerable JoinAwaitCore(this IAsyncEnumerable outer, IAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func> resultSelector) => @@ -85,7 +81,6 @@ internal static IAsyncEnumerable JoinAwaitCore Core(CancellationToken cancellationToken) @@ -122,9 +117,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new JoinAsyncIteratorWithTask(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer); -#endif } #if !NO_DEEP_CANCELLATION @@ -144,7 +136,6 @@ internal static IAsyncEnumerable JoinAwaitWithCancellationCore Core(CancellationToken cancellationToken) @@ -181,395 +172,7 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new JoinAsyncIteratorWithTaskAndCancellation(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer); -#endif - } -#endif - -#if !USE_ASYNC_ITERATOR - private sealed class JoinAsyncIterator : AsyncIterator - { - private readonly IAsyncEnumerable _outer; - private readonly IAsyncEnumerable _inner; - private readonly Func _outerKeySelector; - private readonly Func _innerKeySelector; - private readonly Func _resultSelector; - private readonly IEqualityComparer _comparer; - - private IAsyncEnumerator _outerEnumerator; - - public JoinAsyncIterator(IAsyncEnumerable outer, IAsyncEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func resultSelector, IEqualityComparer comparer) - { - Debug.Assert(outer != null); - Debug.Assert(inner != null); - Debug.Assert(outerKeySelector != null); - Debug.Assert(innerKeySelector != null); - Debug.Assert(resultSelector != null); - - _outer = outer; - _inner = inner; - _outerKeySelector = outerKeySelector; - _innerKeySelector = innerKeySelector; - _resultSelector = resultSelector; - _comparer = comparer; - } - - public override AsyncIteratorBase Clone() - { - return new JoinAsyncIterator(_outer, _inner, _outerKeySelector, _innerKeySelector, _resultSelector, _comparer); - } - - public override async ValueTask DisposeAsync() - { - if (_outerEnumerator != null) - { - await _outerEnumerator.DisposeAsync().ConfigureAwait(false); - _outerEnumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - // State machine vars - private Internal.Lookup _lookup; - private int _count; - private TInner[] _elements; - private int _index; - private TOuter _item; - private int _mode; - - private const int State_If = 1; - private const int State_DoLoop = 2; - private const int State_For = 3; - private const int State_While = 4; - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _outerEnumerator = _outer.GetAsyncEnumerator(_cancellationToken); - _mode = State_If; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - switch (_mode) - { - case State_If: - if (await _outerEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - _lookup = await Internal.Lookup.CreateForJoinAsync(_inner, _innerKeySelector, _comparer, _cancellationToken).ConfigureAwait(false); - - if (_lookup.Count != 0) - { - _mode = State_DoLoop; - goto case State_DoLoop; - } - } - - break; - - case State_DoLoop: - _item = _outerEnumerator.Current; - var g = _lookup.GetGrouping(_outerKeySelector(_item), create: false); - if (g != null) - { - _count = g._count; - _elements = g._elements; - _index = 0; - _mode = State_For; - goto case State_For; - } - - // advance to while - _mode = State_While; - goto case State_While; - - case State_For: - _current = _resultSelector(_item, _elements[_index]); - _index++; - if (_index == _count) - { - _mode = State_While; - } - - return true; - - case State_While: - var hasNext = await _outerEnumerator.MoveNextAsync().ConfigureAwait(false); - if (hasNext) - { - goto case State_DoLoop; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } - - private sealed class JoinAsyncIteratorWithTask : AsyncIterator - { - private readonly IAsyncEnumerable _outer; - private readonly IAsyncEnumerable _inner; - private readonly Func> _outerKeySelector; - private readonly Func> _innerKeySelector; - private readonly Func> _resultSelector; - private readonly IEqualityComparer _comparer; - - private IAsyncEnumerator _outerEnumerator; - - public JoinAsyncIteratorWithTask(IAsyncEnumerable outer, IAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func> resultSelector, IEqualityComparer comparer) - { - Debug.Assert(outer != null); - Debug.Assert(inner != null); - Debug.Assert(outerKeySelector != null); - Debug.Assert(innerKeySelector != null); - Debug.Assert(resultSelector != null); - - _outer = outer; - _inner = inner; - _outerKeySelector = outerKeySelector; - _innerKeySelector = innerKeySelector; - _resultSelector = resultSelector; - _comparer = comparer; - } - - public override AsyncIteratorBase Clone() - { - return new JoinAsyncIteratorWithTask(_outer, _inner, _outerKeySelector, _innerKeySelector, _resultSelector, _comparer); - } - - public override async ValueTask DisposeAsync() - { - if (_outerEnumerator != null) - { - await _outerEnumerator.DisposeAsync().ConfigureAwait(false); - _outerEnumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - // State machine vars - private Internal.LookupWithTask _lookup; - private int _count; - private TInner[] _elements; - private int _index; - private TOuter _item; - private int _mode; - - private const int State_If = 1; - private const int State_DoLoop = 2; - private const int State_For = 3; - private const int State_While = 4; - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _outerEnumerator = _outer.GetAsyncEnumerator(_cancellationToken); - _mode = State_If; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - switch (_mode) - { - case State_If: - if (await _outerEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - _lookup = await Internal.LookupWithTask.CreateForJoinAsync(_inner, _innerKeySelector, _comparer, _cancellationToken).ConfigureAwait(false); - - if (_lookup.Count != 0) - { - _mode = State_DoLoop; - goto case State_DoLoop; - } - } - - break; - - case State_DoLoop: - _item = _outerEnumerator.Current; - var g = _lookup.GetGrouping(await _outerKeySelector(_item).ConfigureAwait(false), create: false); - if (g != null) - { - _count = g._count; - _elements = g._elements; - _index = 0; - _mode = State_For; - goto case State_For; - } - - // advance to while - _mode = State_While; - goto case State_While; - - case State_For: - _current = await _resultSelector(_item, _elements[_index]).ConfigureAwait(false); - _index++; - if (_index == _count) - { - _mode = State_While; - } - - return true; - - case State_While: - var hasNext = await _outerEnumerator.MoveNextAsync().ConfigureAwait(false); - if (hasNext) - { - goto case State_DoLoop; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } } - -#if !NO_DEEP_CANCELLATION - private sealed class JoinAsyncIteratorWithTaskAndCancellation : AsyncIterator - { - private readonly IAsyncEnumerable _outer; - private readonly IAsyncEnumerable _inner; - private readonly Func> _outerKeySelector; - private readonly Func> _innerKeySelector; - private readonly Func> _resultSelector; - private readonly IEqualityComparer _comparer; - - private IAsyncEnumerator _outerEnumerator; - - public JoinAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable outer, IAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func> resultSelector, IEqualityComparer comparer) - { - Debug.Assert(outer != null); - Debug.Assert(inner != null); - Debug.Assert(outerKeySelector != null); - Debug.Assert(innerKeySelector != null); - Debug.Assert(resultSelector != null); - - _outer = outer; - _inner = inner; - _outerKeySelector = outerKeySelector; - _innerKeySelector = innerKeySelector; - _resultSelector = resultSelector; - _comparer = comparer; - } - - public override AsyncIteratorBase Clone() - { - return new JoinAsyncIteratorWithTaskAndCancellation(_outer, _inner, _outerKeySelector, _innerKeySelector, _resultSelector, _comparer); - } - - public override async ValueTask DisposeAsync() - { - if (_outerEnumerator != null) - { - await _outerEnumerator.DisposeAsync().ConfigureAwait(false); - _outerEnumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - // State machine vars - private Internal.LookupWithTask _lookup; - private int _count; - private TInner[] _elements; - private int _index; - private TOuter _item; - private int _mode; - - private const int State_If = 1; - private const int State_DoLoop = 2; - private const int State_For = 3; - private const int State_While = 4; - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _outerEnumerator = _outer.GetAsyncEnumerator(_cancellationToken); - _mode = State_If; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - switch (_mode) - { - case State_If: - if (await _outerEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - _lookup = await Internal.LookupWithTask.CreateForJoinAsync(_inner, _innerKeySelector, _comparer, _cancellationToken).ConfigureAwait(false); - - if (_lookup.Count != 0) - { - _mode = State_DoLoop; - goto case State_DoLoop; - } - } - - break; - - case State_DoLoop: - _item = _outerEnumerator.Current; - var g = _lookup.GetGrouping(await _outerKeySelector(_item, _cancellationToken).ConfigureAwait(false), create: false); - if (g != null) - { - _count = g._count; - _elements = g._elements; - _index = 0; - _mode = State_For; - goto case State_For; - } - - // advance to while - _mode = State_While; - goto case State_While; - - case State_For: - _current = await _resultSelector(_item, _elements[_index], _cancellationToken).ConfigureAwait(false); - _index++; - if (_index == _count) - { - _mode = State_While; - } - - return true; - - case State_While: - var hasNext = await _outerEnumerator.MoveNextAsync().ConfigureAwait(false); - if (hasNext) - { - goto case State_DoLoop; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } -#endif #endif } } diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/OfType.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/OfType.cs index 1220ed56fc..abae1e1d02 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/OfType.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/OfType.cs @@ -23,7 +23,6 @@ public static IAsyncEnumerable OfType(this IAsyncEnumerable Core(CancellationToken cancellationToken) @@ -36,65 +35,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new OfTypeAsyncIterator(source); -#endif } - -#if !USE_ASYNC_ITERATOR - private sealed class OfTypeAsyncIterator : AsyncIterator - { - private readonly IAsyncEnumerable _source; - private IAsyncEnumerator _enumerator; - - public OfTypeAsyncIterator(IAsyncEnumerable source) - { - _source = source; - } - - public override AsyncIteratorBase Clone() - { - return new OfTypeAsyncIterator(_source); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - if (item is TResult res) - { - _current = res; - return true; - } - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } -#endif } } diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Select.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Select.cs index c660988ee1..ab87e2b662 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Select.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Select.cs @@ -36,7 +36,6 @@ public static IAsyncEnumerable Select(this IAsyncEnum if (selector == null) throw Error.ArgumentNull(nameof(selector)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -53,9 +52,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) yield return selector(element, index); } } -#else - return new SelectEnumerableWithIndexAsyncIterator(source, selector); -#endif } internal static IAsyncEnumerable SelectAwaitCore(this IAsyncEnumerable source, Func> selector) @@ -103,7 +99,6 @@ internal static IAsyncEnumerable SelectAwaitCore(this if (selector == null) throw Error.ArgumentNull(nameof(selector)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -120,9 +115,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) yield return await selector(element, index).ConfigureAwait(false); } } -#else - return new SelectEnumerableWithIndexAsyncIteratorWithTask(source, selector); -#endif } #if !NO_DEEP_CANCELLATION @@ -133,7 +125,6 @@ internal static IAsyncEnumerable SelectAwaitWithCancellationCore Core(CancellationToken cancellationToken) @@ -150,9 +141,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) yield return await selector(element, index, cancellationToken).ConfigureAwait(false); } } -#else - return new SelectEnumerableWithIndexAsyncIteratorWithTaskAndCancellation(source, selector); -#endif } #endif @@ -217,72 +205,6 @@ protected override async ValueTask MoveNextCore() } } -#if !USE_ASYNC_ITERATOR - private sealed class SelectEnumerableWithIndexAsyncIterator : AsyncIterator - { - private readonly Func _selector; - private readonly IAsyncEnumerable _source; - private IAsyncEnumerator _enumerator; - private int _index; - - public SelectEnumerableWithIndexAsyncIterator(IAsyncEnumerable source, Func selector) - { - Debug.Assert(source != null); - Debug.Assert(selector != null); - - _source = source; - _selector = selector; - } - - public override AsyncIteratorBase Clone() - { - return new SelectEnumerableWithIndexAsyncIterator(_source, _selector); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - - checked - { - _index++; - } - - _current = _selector(item, _index); - return true; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } -#endif - internal sealed class SelectIListIterator : AsyncIterator, IAsyncIListProvider { private readonly Func _selector; @@ -523,138 +445,6 @@ protected override async ValueTask MoveNextCore() } #endif -#if !USE_ASYNC_ITERATOR - private sealed class SelectEnumerableWithIndexAsyncIteratorWithTask : AsyncIterator - { - private readonly Func> _selector; - private readonly IAsyncEnumerable _source; - private IAsyncEnumerator _enumerator; - private int _index; - - public SelectEnumerableWithIndexAsyncIteratorWithTask(IAsyncEnumerable source, Func> selector) - { - Debug.Assert(source != null); - Debug.Assert(selector != null); - - _source = source; - _selector = selector; - } - - public override AsyncIteratorBase Clone() - { - return new SelectEnumerableWithIndexAsyncIteratorWithTask(_source, _selector); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - - checked - { - _index++; - } - - _current = await _selector(item, _index).ConfigureAwait(false); - return true; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - -#if !NO_DEEP_CANCELLATION - private sealed class SelectEnumerableWithIndexAsyncIteratorWithTaskAndCancellation : AsyncIterator - { - private readonly Func> _selector; - private readonly IAsyncEnumerable _source; - private IAsyncEnumerator _enumerator; - private int _index; - - public SelectEnumerableWithIndexAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable source, Func> selector) - { - Debug.Assert(source != null); - Debug.Assert(selector != null); - - _source = source; - _selector = selector; - } - - public override AsyncIteratorBase Clone() - { - return new SelectEnumerableWithIndexAsyncIteratorWithTaskAndCancellation(_source, _selector); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - - checked - { - _index++; - } - - _current = await _selector(item, _index, _cancellationToken).ConfigureAwait(false); - return true; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } -#endif -#endif - // NB: LINQ to Objects implements IPartition for this. However, it seems incorrect to do so in a trivial // manner where e.g. TryGetLast simply indexes into the list without running the selector for the first n - 1 // elements in order to ensure side-effects. We should consider whether we want to follow this implementation diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SelectMany.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SelectMany.cs index 3df9eae4bc..5e6c9ecab7 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SelectMany.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SelectMany.cs @@ -52,7 +52,6 @@ public static IAsyncEnumerable SelectMany(this IAsync if (selector == null) throw Error.ArgumentNull(nameof(selector)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -74,9 +73,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new SelectManyWithIndexAsyncIterator(source, selector); -#endif } internal static IAsyncEnumerable SelectManyAwaitCore(this IAsyncEnumerable source, Func>> selector) @@ -86,7 +82,6 @@ internal static IAsyncEnumerable SelectManyAwaitCore( if (selector == null) throw Error.ArgumentNull(nameof(selector)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -108,9 +103,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new SelectManyWithIndexAsyncIteratorWithTask(source, selector); -#endif } #if !NO_DEEP_CANCELLATION @@ -121,7 +113,6 @@ internal static IAsyncEnumerable SelectManyAwaitWithCancellationCore Core(CancellationToken cancellationToken) @@ -143,9 +134,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new SelectManyWithIndexAsyncIteratorWithTaskAndCancellation(source, selector); -#endif } #endif @@ -158,7 +146,6 @@ public static IAsyncEnumerable SelectMany Core(CancellationToken cancellationToken) @@ -173,9 +160,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new SelectManyAsyncIterator(source, collectionSelector, resultSelector); -#endif } internal static IAsyncEnumerable SelectManyAwaitCore(this IAsyncEnumerable source, Func>> collectionSelector, Func> resultSelector) @@ -187,7 +171,6 @@ internal static IAsyncEnumerable SelectManyAwaitCore Core(CancellationToken cancellationToken) @@ -202,9 +185,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new SelectManyAsyncIteratorWithTask(source, collectionSelector, resultSelector); -#endif } #if !NO_DEEP_CANCELLATION @@ -217,7 +197,6 @@ internal static IAsyncEnumerable SelectManyAwaitWithCancellationCore Core(CancellationToken cancellationToken) @@ -232,9 +211,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new SelectManyAsyncIteratorWithTaskAndCancellation(source, collectionSelector, resultSelector); -#endif } #endif @@ -247,7 +223,6 @@ public static IAsyncEnumerable SelectMany Core(CancellationToken cancellationToken) @@ -269,9 +244,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new SelectManyWithIndexAsyncIterator(source, collectionSelector, resultSelector); -#endif } internal static IAsyncEnumerable SelectManyAwaitCore(this IAsyncEnumerable source, Func>> collectionSelector, Func> resultSelector) @@ -283,7 +255,6 @@ internal static IAsyncEnumerable SelectManyAwaitCore Core(CancellationToken cancellationToken) @@ -305,9 +276,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new SelectManyWithIndexAsyncIteratorWithTask(source, collectionSelector, resultSelector); -#endif } #if !NO_DEEP_CANCELLATION @@ -320,7 +288,6 @@ internal static IAsyncEnumerable SelectManyAwaitWithCancellationCore Core(CancellationToken cancellationToken) @@ -342,9 +309,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new SelectManyWithIndexAsyncIteratorWithTaskAndCancellation(source, collectionSelector, resultSelector); -#endif } #endif @@ -769,920 +733,6 @@ protected override async ValueTask MoveNextCore() return false; } } -#endif - -#if !USE_ASYNC_ITERATOR - private sealed class SelectManyAsyncIterator : AsyncIterator - { - private const int State_Source = 1; - private const int State_Result = 2; - - private readonly Func> _collectionSelector; - private readonly Func _resultSelector; - private readonly IAsyncEnumerable _source; - - private TSource _currentSource; - private int _mode; - private IAsyncEnumerator _resultEnumerator; - private IAsyncEnumerator _sourceEnumerator; - - public SelectManyAsyncIterator(IAsyncEnumerable source, Func> collectionSelector, Func resultSelector) - { - Debug.Assert(source != null); - Debug.Assert(collectionSelector != null); - Debug.Assert(resultSelector != null); - - _source = source; - _collectionSelector = collectionSelector; - _resultSelector = resultSelector; - } - - public override AsyncIteratorBase Clone() - { - return new SelectManyAsyncIterator(_source, _collectionSelector, _resultSelector); - } - - public override async ValueTask DisposeAsync() - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - _resultEnumerator = null; - } - - if (_sourceEnumerator != null) - { - await _sourceEnumerator.DisposeAsync().ConfigureAwait(false); - _sourceEnumerator = null; - } - - _currentSource = default; - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _sourceEnumerator = _source.GetAsyncEnumerator(_cancellationToken); - _mode = State_Source; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - switch (_mode) - { - case State_Source: - if (await _sourceEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - } - - _currentSource = _sourceEnumerator.Current; - var inner = _collectionSelector(_currentSource); - _resultEnumerator = inner.GetAsyncEnumerator(_cancellationToken); - - _mode = State_Result; - goto case State_Result; - } - break; - - case State_Result: - if (await _resultEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _resultSelector(_currentSource, _resultEnumerator.Current); - return true; - } - - _mode = State_Source; - goto case State_Source; // loop - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - - private sealed class SelectManyAsyncIteratorWithTask : AsyncIterator - { - private const int State_Source = 1; - private const int State_Result = 2; - - private readonly Func>> _collectionSelector; - private readonly Func> _resultSelector; - private readonly IAsyncEnumerable _source; - - private TSource _currentSource; - private int _mode; - private IAsyncEnumerator _resultEnumerator; - private IAsyncEnumerator _sourceEnumerator; - - public SelectManyAsyncIteratorWithTask(IAsyncEnumerable source, Func>> collectionSelector, Func> resultSelector) - { - Debug.Assert(source != null); - Debug.Assert(collectionSelector != null); - Debug.Assert(resultSelector != null); - - _source = source; - _collectionSelector = collectionSelector; - _resultSelector = resultSelector; - } - - public override AsyncIteratorBase Clone() - { - return new SelectManyAsyncIteratorWithTask(_source, _collectionSelector, _resultSelector); - } - - public override async ValueTask DisposeAsync() - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - _resultEnumerator = null; - } - - if (_sourceEnumerator != null) - { - await _sourceEnumerator.DisposeAsync().ConfigureAwait(false); - _sourceEnumerator = null; - } - - _currentSource = default; - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _sourceEnumerator = _source.GetAsyncEnumerator(_cancellationToken); - _mode = State_Source; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - switch (_mode) - { - case State_Source: - if (await _sourceEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - } - - _currentSource = _sourceEnumerator.Current; - var inner = await _collectionSelector(_currentSource).ConfigureAwait(false); - _resultEnumerator = inner.GetAsyncEnumerator(_cancellationToken); - - _mode = State_Result; - goto case State_Result; - } - break; - - case State_Result: - if (await _resultEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = await _resultSelector(_currentSource, _resultEnumerator.Current).ConfigureAwait(false); - return true; - } - - _mode = State_Source; - goto case State_Source; // loop - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - -#if !NO_DEEP_CANCELLATION - private sealed class SelectManyAsyncIteratorWithTaskAndCancellation : AsyncIterator - { - private const int State_Source = 1; - private const int State_Result = 2; - - private readonly Func>> _collectionSelector; - private readonly Func> _resultSelector; - private readonly IAsyncEnumerable _source; - - private TSource _currentSource; - private int _mode; - private IAsyncEnumerator _resultEnumerator; - private IAsyncEnumerator _sourceEnumerator; - - public SelectManyAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable source, Func>> collectionSelector, Func> resultSelector) - { - Debug.Assert(source != null); - Debug.Assert(collectionSelector != null); - Debug.Assert(resultSelector != null); - - _source = source; - _collectionSelector = collectionSelector; - _resultSelector = resultSelector; - } - - public override AsyncIteratorBase Clone() - { - return new SelectManyAsyncIteratorWithTaskAndCancellation(_source, _collectionSelector, _resultSelector); - } - - public override async ValueTask DisposeAsync() - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - _resultEnumerator = null; - } - - if (_sourceEnumerator != null) - { - await _sourceEnumerator.DisposeAsync().ConfigureAwait(false); - _sourceEnumerator = null; - } - - _currentSource = default; - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _sourceEnumerator = _source.GetAsyncEnumerator(_cancellationToken); - _mode = State_Source; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - switch (_mode) - { - case State_Source: - if (await _sourceEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - } - - _currentSource = _sourceEnumerator.Current; - var inner = await _collectionSelector(_currentSource, _cancellationToken).ConfigureAwait(false); - _resultEnumerator = inner.GetAsyncEnumerator(_cancellationToken); - - _mode = State_Result; - goto case State_Result; - } - break; - - case State_Result: - if (await _resultEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = await _resultSelector(_currentSource, _resultEnumerator.Current, _cancellationToken).ConfigureAwait(false); - return true; - } - - _mode = State_Source; - goto case State_Source; // loop - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } -#endif - - private sealed class SelectManyWithIndexAsyncIterator : AsyncIterator - { - private const int State_Source = 1; - private const int State_Result = 2; - - private readonly Func> _collectionSelector; - private readonly Func _resultSelector; - private readonly IAsyncEnumerable _source; - - private TSource _currentSource; - private int _index; - private int _mode; - private IAsyncEnumerator _resultEnumerator; - private IAsyncEnumerator _sourceEnumerator; - - public SelectManyWithIndexAsyncIterator(IAsyncEnumerable source, Func> collectionSelector, Func resultSelector) - { - Debug.Assert(source != null); - Debug.Assert(collectionSelector != null); - Debug.Assert(resultSelector != null); - - _source = source; - _collectionSelector = collectionSelector; - _resultSelector = resultSelector; - } - - public override AsyncIteratorBase Clone() - { - return new SelectManyWithIndexAsyncIterator(_source, _collectionSelector, _resultSelector); - } - - public override async ValueTask DisposeAsync() - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - _resultEnumerator = null; - } - - if (_sourceEnumerator != null) - { - await _sourceEnumerator.DisposeAsync().ConfigureAwait(false); - _sourceEnumerator = null; - } - - _currentSource = default; - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _sourceEnumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - _mode = State_Source; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - switch (_mode) - { - case State_Source: - if (await _sourceEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - } - - _currentSource = _sourceEnumerator.Current; - - checked - { - _index++; - } - - var inner = _collectionSelector(_currentSource, _index); - _resultEnumerator = inner.GetAsyncEnumerator(_cancellationToken); - - _mode = State_Result; - goto case State_Result; - } - break; - - case State_Result: - if (await _resultEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _resultSelector(_currentSource, _resultEnumerator.Current); - return true; - } - - _mode = State_Source; - goto case State_Source; // loop - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - - private sealed class SelectManyWithIndexAsyncIteratorWithTask : AsyncIterator - { - private const int State_Source = 1; - private const int State_Result = 2; - - private readonly Func>> _collectionSelector; - private readonly Func> _resultSelector; - private readonly IAsyncEnumerable _source; - - private TSource _currentSource; - private int _index; - private int _mode; - private IAsyncEnumerator _resultEnumerator; - private IAsyncEnumerator _sourceEnumerator; - - public SelectManyWithIndexAsyncIteratorWithTask(IAsyncEnumerable source, Func>> collectionSelector, Func> resultSelector) - { - Debug.Assert(source != null); - Debug.Assert(collectionSelector != null); - Debug.Assert(resultSelector != null); - - _source = source; - _collectionSelector = collectionSelector; - _resultSelector = resultSelector; - } - - public override AsyncIteratorBase Clone() - { - return new SelectManyWithIndexAsyncIteratorWithTask(_source, _collectionSelector, _resultSelector); - } - - public override async ValueTask DisposeAsync() - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - _resultEnumerator = null; - } - - if (_sourceEnumerator != null) - { - await _sourceEnumerator.DisposeAsync().ConfigureAwait(false); - _sourceEnumerator = null; - } - - _currentSource = default; - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _sourceEnumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - _mode = State_Source; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - switch (_mode) - { - case State_Source: - if (await _sourceEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - } - - _currentSource = _sourceEnumerator.Current; - - checked - { - _index++; - } - - var inner = await _collectionSelector(_currentSource, _index).ConfigureAwait(false); - _resultEnumerator = inner.GetAsyncEnumerator(_cancellationToken); - - _mode = State_Result; - goto case State_Result; - } - break; - - case State_Result: - if (await _resultEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = await _resultSelector(_currentSource, _resultEnumerator.Current).ConfigureAwait(false); - return true; - } - - _mode = State_Source; - goto case State_Source; // loop - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - -#if !NO_DEEP_CANCELLATION - private sealed class SelectManyWithIndexAsyncIteratorWithTaskAndCancellation : AsyncIterator - { - private const int State_Source = 1; - private const int State_Result = 2; - - private readonly Func>> _collectionSelector; - private readonly Func> _resultSelector; - private readonly IAsyncEnumerable _source; - - private TSource _currentSource; - private int _index; - private int _mode; - private IAsyncEnumerator _resultEnumerator; - private IAsyncEnumerator _sourceEnumerator; - - public SelectManyWithIndexAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable source, Func>> collectionSelector, Func> resultSelector) - { - Debug.Assert(source != null); - Debug.Assert(collectionSelector != null); - Debug.Assert(resultSelector != null); - - _source = source; - _collectionSelector = collectionSelector; - _resultSelector = resultSelector; - } - - public override AsyncIteratorBase Clone() - { - return new SelectManyWithIndexAsyncIteratorWithTaskAndCancellation(_source, _collectionSelector, _resultSelector); - } - - public override async ValueTask DisposeAsync() - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - _resultEnumerator = null; - } - - if (_sourceEnumerator != null) - { - await _sourceEnumerator.DisposeAsync().ConfigureAwait(false); - _sourceEnumerator = null; - } - - _currentSource = default; - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _sourceEnumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - _mode = State_Source; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - switch (_mode) - { - case State_Source: - if (await _sourceEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - } - - _currentSource = _sourceEnumerator.Current; - - checked - { - _index++; - } - - var inner = await _collectionSelector(_currentSource, _index, _cancellationToken).ConfigureAwait(false); - _resultEnumerator = inner.GetAsyncEnumerator(_cancellationToken); - - _mode = State_Result; - goto case State_Result; - } - break; - - case State_Result: - if (await _resultEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = await _resultSelector(_currentSource, _resultEnumerator.Current, _cancellationToken).ConfigureAwait(false); - return true; - } - - _mode = State_Source; - goto case State_Source; // loop - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } -#endif - - private sealed class SelectManyWithIndexAsyncIterator : AsyncIterator - { - private const int State_Source = 1; - private const int State_Result = 2; - - private readonly Func> _selector; - private readonly IAsyncEnumerable _source; - - private int _index; - private int _mode; - private IAsyncEnumerator _resultEnumerator; - private IAsyncEnumerator _sourceEnumerator; - - public SelectManyWithIndexAsyncIterator(IAsyncEnumerable source, Func> selector) - { - Debug.Assert(source != null); - Debug.Assert(selector != null); - - _source = source; - _selector = selector; - } - - public override AsyncIteratorBase Clone() - { - return new SelectManyWithIndexAsyncIterator(_source, _selector); - } - - public override async ValueTask DisposeAsync() - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - _resultEnumerator = null; - } - - if (_sourceEnumerator != null) - { - await _sourceEnumerator.DisposeAsync().ConfigureAwait(false); - _sourceEnumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _sourceEnumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - _mode = State_Source; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - switch (_mode) - { - case State_Source: - if (await _sourceEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - } - - checked - { - _index++; - } - - var inner = _selector(_sourceEnumerator.Current, _index); - _resultEnumerator = inner.GetAsyncEnumerator(_cancellationToken); - - _mode = State_Result; - goto case State_Result; - } - break; - - case State_Result: - if (await _resultEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _resultEnumerator.Current; - return true; - } - - _mode = State_Source; - goto case State_Source; // loop - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - - private sealed class SelectManyWithIndexAsyncIteratorWithTask : AsyncIterator - { - private const int State_Source = 1; - private const int State_Result = 2; - - private readonly Func>> _selector; - private readonly IAsyncEnumerable _source; - - private int _index; - private int _mode; - private IAsyncEnumerator _resultEnumerator; - private IAsyncEnumerator _sourceEnumerator; - - public SelectManyWithIndexAsyncIteratorWithTask(IAsyncEnumerable source, Func>> selector) - { - Debug.Assert(source != null); - Debug.Assert(selector != null); - - _source = source; - _selector = selector; - } - - public override AsyncIteratorBase Clone() - { - return new SelectManyWithIndexAsyncIteratorWithTask(_source, _selector); - } - - public override async ValueTask DisposeAsync() - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - _resultEnumerator = null; - } - - if (_sourceEnumerator != null) - { - await _sourceEnumerator.DisposeAsync().ConfigureAwait(false); - _sourceEnumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _sourceEnumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - _mode = State_Source; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - switch (_mode) - { - case State_Source: - if (await _sourceEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - } - - checked - { - _index++; - } - - var inner = await _selector(_sourceEnumerator.Current, _index).ConfigureAwait(false); - _resultEnumerator = inner.GetAsyncEnumerator(_cancellationToken); - - _mode = State_Result; - goto case State_Result; - } - break; - - case State_Result: - if (await _resultEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _resultEnumerator.Current; - return true; - } - - _mode = State_Source; - goto case State_Source; // loop - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - -#if !NO_DEEP_CANCELLATION - private sealed class SelectManyWithIndexAsyncIteratorWithTaskAndCancellation : AsyncIterator - { - private const int State_Source = 1; - private const int State_Result = 2; - - private readonly Func>> _selector; - private readonly IAsyncEnumerable _source; - - private int _index; - private int _mode; - private IAsyncEnumerator _resultEnumerator; - private IAsyncEnumerator _sourceEnumerator; - - public SelectManyWithIndexAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable source, Func>> selector) - { - Debug.Assert(source != null); - Debug.Assert(selector != null); - - _source = source; - _selector = selector; - } - - public override AsyncIteratorBase Clone() - { - return new SelectManyWithIndexAsyncIteratorWithTaskAndCancellation(_source, _selector); - } - - public override async ValueTask DisposeAsync() - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - _resultEnumerator = null; - } - - if (_sourceEnumerator != null) - { - await _sourceEnumerator.DisposeAsync().ConfigureAwait(false); - _sourceEnumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _sourceEnumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - _mode = State_Source; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - switch (_mode) - { - case State_Source: - if (await _sourceEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - if (_resultEnumerator != null) - { - await _resultEnumerator.DisposeAsync().ConfigureAwait(false); - } - - checked - { - _index++; - } - - var inner = await _selector(_sourceEnumerator.Current, _index, _cancellationToken).ConfigureAwait(false); - _resultEnumerator = inner.GetAsyncEnumerator(_cancellationToken); - - _mode = State_Result; - goto case State_Result; - } - break; - - case State_Result: - if (await _resultEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _resultEnumerator.Current; - return true; - } - - _mode = State_Source; - goto case State_Source; // loop - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } -#endif #endif } } diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SkipLast.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SkipLast.cs index 5fc364014c..b70389eeef 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SkipLast.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SkipLast.cs @@ -28,7 +28,6 @@ public static IAsyncEnumerable SkipLast(this IAsyncEnumerable< count = 0; } -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -56,79 +55,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new SkipLastAsyncIterator(source, count); -#endif } - -#if !USE_ASYNC_ITERATOR - private sealed class SkipLastAsyncIterator : AsyncIterator - { - private readonly int _count; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - private Queue _queue; - - public SkipLastAsyncIterator(IAsyncEnumerable source, int count) - { - Debug.Assert(source != null); - - _source = source; - _count = count; - } - - public override AsyncIteratorBase Clone() - { - return new SkipLastAsyncIterator(_source, _count); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - _queue = null; // release the memory - - await base.DisposeAsync().ConfigureAwait(false); - } - - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _queue = new Queue(); - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - - case AsyncIteratorState.Iterating: - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - _queue.Enqueue(item); - - if (_queue.Count > _count) - { - _current = _queue.Dequeue(); - return true; - } - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } -#endif } } diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SkipWhile.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SkipWhile.cs index 93190009c9..0a624aa1dd 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SkipWhile.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SkipWhile.cs @@ -18,7 +18,6 @@ public static IAsyncEnumerable SkipWhile(this IAsyncEnumerable if (predicate == null) throw Error.ArgumentNull(nameof(predicate)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -43,9 +42,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new SkipWhileAsyncIterator(source, predicate); -#endif } public static IAsyncEnumerable SkipWhile(this IAsyncEnumerable source, Func predicate) @@ -55,7 +51,6 @@ public static IAsyncEnumerable SkipWhile(this IAsyncEnumerable if (predicate == null) throw Error.ArgumentNull(nameof(predicate)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -87,9 +82,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new SkipWhileWithIndexAsyncIterator(source, predicate); -#endif } internal static IAsyncEnumerable SkipWhileAwaitCore(this IAsyncEnumerable source, Func> predicate) @@ -99,7 +91,6 @@ internal static IAsyncEnumerable SkipWhileAwaitCore(this IAsyn if (predicate == null) throw Error.ArgumentNull(nameof(predicate)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -124,9 +115,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new SkipWhileAsyncIteratorWithTask(source, predicate); -#endif } #if !NO_DEEP_CANCELLATION @@ -137,7 +125,6 @@ internal static IAsyncEnumerable SkipWhileAwaitWithCancellationCore Core(CancellationToken cancellationToken) @@ -162,9 +149,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new SkipWhileAsyncIteratorWithTaskAndCancellation(source, predicate); -#endif } #endif @@ -175,7 +159,6 @@ internal static IAsyncEnumerable SkipWhileAwaitCore(this IAsyn if (predicate == null) throw Error.ArgumentNull(nameof(predicate)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -207,9 +190,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new SkipWhileWithIndexAsyncIteratorWithTask(source, predicate); -#endif } #if !NO_DEEP_CANCELLATION @@ -220,7 +200,6 @@ internal static IAsyncEnumerable SkipWhileAwaitWithCancellationCore Core(CancellationToken cancellationToken) @@ -252,496 +231,7 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new SkipWhileWithIndexAsyncIteratorWithTaskAndCancellation(source, predicate); -#endif - } -#endif - -#if !USE_ASYNC_ITERATOR - private sealed class SkipWhileAsyncIterator : AsyncIterator - { - private readonly Func _predicate; - private readonly IAsyncEnumerable _source; - - private bool _doMoveNext; - private IAsyncEnumerator _enumerator; - - public SkipWhileAsyncIterator(IAsyncEnumerable source, Func predicate) - { - Debug.Assert(predicate != null); - Debug.Assert(source != null); - - _source = source; - _predicate = predicate; - } - - public override AsyncIteratorBase Clone() - { - return new SkipWhileAsyncIterator(_source, _predicate); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - - // skip elements as requested - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var element = _enumerator.Current; - if (!_predicate(element)) - { - _doMoveNext = false; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - } - } - - break; - - case AsyncIteratorState.Iterating: - if (_doMoveNext && await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - if (!_doMoveNext) - { - _current = _enumerator.Current; - _doMoveNext = true; - return true; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - - private sealed class SkipWhileWithIndexAsyncIterator : AsyncIterator - { - private readonly Func _predicate; - private readonly IAsyncEnumerable _source; - - private bool _doMoveNext; - private IAsyncEnumerator _enumerator; - private int _index; - - public SkipWhileWithIndexAsyncIterator(IAsyncEnumerable source, Func predicate) - { - Debug.Assert(predicate != null); - Debug.Assert(source != null); - - _source = source; - _predicate = predicate; - } - - public override AsyncIteratorBase Clone() - { - return new SkipWhileWithIndexAsyncIterator(_source, _predicate); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - - // skip elements as requested - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var element = _enumerator.Current; - - checked - { - _index++; - } - - if (!_predicate(element, _index)) - { - _doMoveNext = false; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - } - } - - break; - - case AsyncIteratorState.Iterating: - if (_doMoveNext && await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - if (!_doMoveNext) - { - _current = _enumerator.Current; - _doMoveNext = true; - return true; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - - private sealed class SkipWhileAsyncIteratorWithTask : AsyncIterator - { - private readonly Func> _predicate; - private readonly IAsyncEnumerable _source; - - private bool _doMoveNext; - private IAsyncEnumerator _enumerator; - - public SkipWhileAsyncIteratorWithTask(IAsyncEnumerable source, Func> predicate) - { - Debug.Assert(predicate != null); - Debug.Assert(source != null); - - _source = source; - _predicate = predicate; - } - - public override AsyncIteratorBase Clone() - { - return new SkipWhileAsyncIteratorWithTask(_source, _predicate); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - - // skip elements as requested - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var element = _enumerator.Current; - if (!await _predicate(element).ConfigureAwait(false)) - { - _doMoveNext = false; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - } - } - - break; - - case AsyncIteratorState.Iterating: - if (_doMoveNext && await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - if (!_doMoveNext) - { - _current = _enumerator.Current; - _doMoveNext = true; - return true; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } } - -#if !NO_DEEP_CANCELLATION - private sealed class SkipWhileAsyncIteratorWithTaskAndCancellation : AsyncIterator - { - private readonly Func> _predicate; - private readonly IAsyncEnumerable _source; - - private bool _doMoveNext; - private IAsyncEnumerator _enumerator; - - public SkipWhileAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable source, Func> predicate) - { - Debug.Assert(predicate != null); - Debug.Assert(source != null); - - _source = source; - _predicate = predicate; - } - - public override AsyncIteratorBase Clone() - { - return new SkipWhileAsyncIteratorWithTaskAndCancellation(_source, _predicate); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - - // skip elements as requested - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var element = _enumerator.Current; - if (!await _predicate(element, _cancellationToken).ConfigureAwait(false)) - { - _doMoveNext = false; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - } - } - - break; - - case AsyncIteratorState.Iterating: - if (_doMoveNext && await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - if (!_doMoveNext) - { - _current = _enumerator.Current; - _doMoveNext = true; - return true; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } -#endif - - private sealed class SkipWhileWithIndexAsyncIteratorWithTask : AsyncIterator - { - private readonly Func> _predicate; - private readonly IAsyncEnumerable _source; - - private bool _doMoveNext; - private IAsyncEnumerator _enumerator; - private int _index; - - public SkipWhileWithIndexAsyncIteratorWithTask(IAsyncEnumerable source, Func> predicate) - { - Debug.Assert(predicate != null); - Debug.Assert(source != null); - - _source = source; - _predicate = predicate; - } - - public override AsyncIteratorBase Clone() - { - return new SkipWhileWithIndexAsyncIteratorWithTask(_source, _predicate); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - - // skip elements as requested - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var element = _enumerator.Current; - - checked - { - _index++; - } - - if (!await _predicate(element, _index).ConfigureAwait(false)) - { - _doMoveNext = false; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - } - } - - break; - - case AsyncIteratorState.Iterating: - if (_doMoveNext && await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - if (!_doMoveNext) - { - _current = _enumerator.Current; - _doMoveNext = true; - return true; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - -#if !NO_DEEP_CANCELLATION - private sealed class SkipWhileWithIndexAsyncIteratorWithTaskAndCancellation : AsyncIterator - { - private readonly Func> _predicate; - private readonly IAsyncEnumerable _source; - - private bool _doMoveNext; - private IAsyncEnumerator _enumerator; - private int _index; - - public SkipWhileWithIndexAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable source, Func> predicate) - { - Debug.Assert(predicate != null); - Debug.Assert(source != null); - - _source = source; - _predicate = predicate; - } - - public override AsyncIteratorBase Clone() - { - return new SkipWhileWithIndexAsyncIteratorWithTaskAndCancellation(_source, _predicate); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - - // skip elements as requested - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var element = _enumerator.Current; - - checked - { - _index++; - } - - if (!await _predicate(element, _index, _cancellationToken).ConfigureAwait(false)) - { - _doMoveNext = false; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - } - } - - break; - - case AsyncIteratorState.Iterating: - if (_doMoveNext && await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _enumerator.Current; - return true; - } - - if (!_doMoveNext) - { - _current = _enumerator.Current; - _doMoveNext = true; - return true; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } -#endif #endif } } diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/TakeLast.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/TakeLast.cs index e5cc370394..28538f0e84 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/TakeLast.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/TakeLast.cs @@ -21,7 +21,6 @@ public static IAsyncEnumerable TakeLast(this IAsyncEnumerable< return Empty(); } -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -64,104 +63,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } while (queue.Count > 0); } -#else - return new TakeLastAsyncIterator(source, count); -#endif } - -#if !USE_ASYNC_ITERATOR - private sealed class TakeLastAsyncIterator : AsyncIterator - { - private readonly int _count; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - private bool _isDone; - private Queue _queue; - - public TakeLastAsyncIterator(IAsyncEnumerable source, int count) - { - Debug.Assert(source != null); - - _source = source; - _count = count; - } - - public override AsyncIteratorBase Clone() - { - return new TakeLastAsyncIterator(_source, _count); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - _queue = null; // release the memory - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _queue = new Queue(); - _isDone = false; - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - - case AsyncIteratorState.Iterating: - while (true) - { - if (!_isDone) - { - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - if (_count > 0) - { - var item = _enumerator.Current; - if (_queue.Count >= _count) - { - _queue.Dequeue(); - } - _queue.Enqueue(item); - } - } - else - { - _isDone = true; - // Dispose early here as we can - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - continue; // loop until queue is drained - } - - if (_queue.Count > 0) - { - _current = _queue.Dequeue(); - return true; - } - - break; // while - } - - break; // case - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } -#endif } } diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/TakeWhile.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/TakeWhile.cs index ee8a9a07ee..8c94b884e1 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/TakeWhile.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/TakeWhile.cs @@ -18,7 +18,6 @@ public static IAsyncEnumerable TakeWhile(this IAsyncEnumerable if (predicate == null) throw Error.ArgumentNull(nameof(predicate)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -33,9 +32,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) yield return element; } } -#else - return new TakeWhileAsyncIterator(source, predicate); -#endif } public static IAsyncEnumerable TakeWhile(this IAsyncEnumerable source, Func predicate) @@ -45,7 +41,6 @@ public static IAsyncEnumerable TakeWhile(this IAsyncEnumerable if (predicate == null) throw Error.ArgumentNull(nameof(predicate)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -67,9 +62,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) yield return element; } } -#else - return new TakeWhileWithIndexAsyncIterator(source, predicate); -#endif } internal static IAsyncEnumerable TakeWhileAwaitCore(this IAsyncEnumerable source, Func> predicate) @@ -79,7 +71,6 @@ internal static IAsyncEnumerable TakeWhileAwaitCore(this IAsyn if (predicate == null) throw Error.ArgumentNull(nameof(predicate)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -94,9 +85,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) yield return element; } } -#else - return new TakeWhileAsyncIteratorWithTask(source, predicate); -#endif } #if !NO_DEEP_CANCELLATION @@ -107,7 +95,6 @@ internal static IAsyncEnumerable TakeWhileAwaitWithCancellationCore Core(CancellationToken cancellationToken) @@ -122,9 +109,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) yield return element; } } -#else - return new TakeWhileAsyncIteratorWithTaskAndCancellation(source, predicate); -#endif } #endif @@ -135,7 +119,6 @@ internal static IAsyncEnumerable TakeWhileAwaitCore(this IAsyn if (predicate == null) throw Error.ArgumentNull(nameof(predicate)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -157,9 +140,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) yield return element; } } -#else - return new TakeWhileWithIndexAsyncIteratorWithTask(source, predicate); -#endif } #if !NO_DEEP_CANCELLATION @@ -170,7 +150,6 @@ internal static IAsyncEnumerable TakeWhileAwaitWithCancellationCore Core(CancellationToken cancellationToken) @@ -192,416 +171,7 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) yield return element; } } -#else - return new TakeWhileWithIndexAsyncIteratorWithTaskAndCancellation(source, predicate); -#endif - } -#endif - -#if !USE_ASYNC_ITERATOR - private sealed class TakeWhileAsyncIterator : AsyncIterator - { - private readonly Func _predicate; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - - public TakeWhileAsyncIterator(IAsyncEnumerable source, Func predicate) - { - Debug.Assert(predicate != null); - Debug.Assert(source != null); - - _source = source; - _predicate = predicate; - } - - public override AsyncIteratorBase Clone() - { - return new TakeWhileAsyncIterator(_source, _predicate); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - if (!_predicate(item)) - { - break; - } - - _current = item; - return true; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - - private sealed class TakeWhileWithIndexAsyncIterator : AsyncIterator - { - private readonly Func _predicate; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - private int _index; - - public TakeWhileWithIndexAsyncIterator(IAsyncEnumerable source, Func predicate) - { - Debug.Assert(predicate != null); - Debug.Assert(source != null); - - _source = source; - _predicate = predicate; - } - - public override AsyncIteratorBase Clone() - { - return new TakeWhileWithIndexAsyncIterator(_source, _predicate); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - - checked - { - _index++; - } - - if (!_predicate(item, _index)) - { - break; - } - - _current = item; - return true; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - - private sealed class TakeWhileAsyncIteratorWithTask : AsyncIterator - { - private readonly Func> _predicate; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - - public TakeWhileAsyncIteratorWithTask(IAsyncEnumerable source, Func> predicate) - { - Debug.Assert(predicate != null); - Debug.Assert(source != null); - - _source = source; - _predicate = predicate; - } - - public override AsyncIteratorBase Clone() - { - return new TakeWhileAsyncIteratorWithTask(_source, _predicate); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - if (!await _predicate(item).ConfigureAwait(false)) - { - break; - } - - _current = item; - return true; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } } - -#if !NO_DEEP_CANCELLATION - private sealed class TakeWhileAsyncIteratorWithTaskAndCancellation : AsyncIterator - { - private readonly Func> _predicate; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - - public TakeWhileAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable source, Func> predicate) - { - Debug.Assert(predicate != null); - Debug.Assert(source != null); - - _source = source; - _predicate = predicate; - } - - public override AsyncIteratorBase Clone() - { - return new TakeWhileAsyncIteratorWithTaskAndCancellation(_source, _predicate); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - - case AsyncIteratorState.Iterating: - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - if (!await _predicate(item, _cancellationToken).ConfigureAwait(false)) - { - break; - } - - _current = item; - return true; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } -#endif - - private sealed class TakeWhileWithIndexAsyncIteratorWithTask : AsyncIterator - { - private readonly Func> _predicate; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - private int _index; - - public TakeWhileWithIndexAsyncIteratorWithTask(IAsyncEnumerable source, Func> predicate) - { - Debug.Assert(predicate != null); - Debug.Assert(source != null); - - _source = source; - _predicate = predicate; - } - - public override AsyncIteratorBase Clone() - { - return new TakeWhileWithIndexAsyncIteratorWithTask(_source, _predicate); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - - checked - { - _index++; - } - - if (!await _predicate(item, _index).ConfigureAwait(false)) - { - break; - } - - _current = item; - return true; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } - -#if !NO_DEEP_CANCELLATION - private sealed class TakeWhileWithIndexAsyncIteratorWithTaskAndCancellation : AsyncIterator - { - private readonly Func> _predicate; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - private int _index; - - public TakeWhileWithIndexAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable source, Func> predicate) - { - Debug.Assert(predicate != null); - Debug.Assert(source != null); - - _source = source; - _predicate = predicate; - } - - public override AsyncIteratorBase Clone() - { - return new TakeWhileWithIndexAsyncIteratorWithTaskAndCancellation(_source, _predicate); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - if (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - - checked - { - _index++; - } - - if (!await _predicate(item, _index, _cancellationToken).ConfigureAwait(false)) - { - break; - } - - _current = item; - return true; - } - - break; - } - - await DisposeAsync().ConfigureAwait(false); - return false; - } - } -#endif #endif } } diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Where.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Where.cs index d4e057b754..e6ed892f83 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Where.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Where.cs @@ -34,7 +34,6 @@ public static IAsyncEnumerable Where(this IAsyncEnumerable Core(CancellationToken cancellationToken) @@ -54,9 +53,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new WhereEnumerableWithIndexAsyncIterator(source, predicate); -#endif } internal static IAsyncEnumerable WhereAwaitCore(this IAsyncEnumerable source, Func> predicate) @@ -100,7 +96,6 @@ internal static IAsyncEnumerable WhereAwaitCore(this IAsyncEnu if (predicate == null) throw Error.ArgumentNull(nameof(predicate)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -120,9 +115,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new WhereEnumerableWithIndexAsyncIteratorWithTask(source, predicate); -#endif } #if !NO_DEEP_CANCELLATION @@ -133,7 +125,6 @@ internal static IAsyncEnumerable WhereAwaitWithCancellationCore Core(CancellationToken cancellationToken) @@ -153,9 +144,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new WhereEnumerableWithIndexAsyncIteratorWithTaskAndCancellation(source, predicate); -#endif } #endif @@ -228,76 +216,6 @@ protected override async ValueTask MoveNextCore() } } -#if !USE_ASYNC_ITERATOR - private sealed class WhereEnumerableWithIndexAsyncIterator : AsyncIterator - { - private readonly Func _predicate; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - private int _index; - - public WhereEnumerableWithIndexAsyncIterator(IAsyncEnumerable source, Func predicate) - { - Debug.Assert(source != null); - Debug.Assert(predicate != null); - - _source = source; - _predicate = predicate; - } - - public override AsyncIteratorBase Clone() - { - return new WhereEnumerableWithIndexAsyncIterator(_source, _predicate); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - - checked - { - _index++; - } - - if (_predicate(item, _index)) - { - _current = item; - return true; - } - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } -#endif - internal sealed class WhereEnumerableAsyncIteratorWithTask : AsyncIterator { private readonly Func> _predicate; @@ -428,146 +346,6 @@ protected override async ValueTask MoveNextCore() } #endif -#if !USE_ASYNC_ITERATOR - private sealed class WhereEnumerableWithIndexAsyncIteratorWithTask : AsyncIterator - { - private readonly Func> _predicate; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - private int _index; - - public WhereEnumerableWithIndexAsyncIteratorWithTask(IAsyncEnumerable source, Func> predicate) - { - Debug.Assert(source != null); - Debug.Assert(predicate != null); - - _source = source; - _predicate = predicate; - } - - public override AsyncIteratorBase Clone() - { - return new WhereEnumerableWithIndexAsyncIteratorWithTask(_source, _predicate); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - - checked - { - _index++; - } - - if (await _predicate(item, _index).ConfigureAwait(false)) - { - _current = item; - return true; - } - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } - -#if !NO_DEEP_CANCELLATION - private sealed class WhereEnumerableWithIndexAsyncIteratorWithTaskAndCancellation : AsyncIterator - { - private readonly Func> _predicate; - private readonly IAsyncEnumerable _source; - - private IAsyncEnumerator _enumerator; - private int _index; - - public WhereEnumerableWithIndexAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable source, Func> predicate) - { - Debug.Assert(source != null); - Debug.Assert(predicate != null); - - _source = source; - _predicate = predicate; - } - - public override AsyncIteratorBase Clone() - { - return new WhereEnumerableWithIndexAsyncIteratorWithTaskAndCancellation(_source, _predicate); - } - - public override async ValueTask DisposeAsync() - { - if (_enumerator != null) - { - await _enumerator.DisposeAsync().ConfigureAwait(false); - _enumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - switch (_state) - { - case AsyncIteratorState.Allocated: - _enumerator = _source.GetAsyncEnumerator(_cancellationToken); - _index = -1; - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) - { - var item = _enumerator.Current; - - checked - { - _index++; - } - - if (await _predicate(item, _index, _cancellationToken).ConfigureAwait(false)) - { - _current = item; - return true; - } - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } -#endif -#endif - private sealed class WhereSelectEnumerableAsyncIterator : AsyncIterator { private readonly Func _predicate; diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Zip.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Zip.cs index 182ebae643..0f8c4219a4 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Zip.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Zip.cs @@ -19,7 +19,6 @@ public static partial class AsyncEnumerable if (second == null) throw Error.ArgumentNull(nameof(second)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator<(TFirst, TSecond)> Core(CancellationToken cancellationToken) @@ -35,9 +34,6 @@ public static partial class AsyncEnumerable } } } -#else - return new ZipAsyncIterator(first, second, (first, second) => (first, second)); -#endif } #endif @@ -50,7 +46,6 @@ public static IAsyncEnumerable Zip(this IAsyn if (selector == null) throw Error.ArgumentNull(nameof(selector)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -66,9 +61,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new ZipAsyncIterator(first, second, selector); -#endif } internal static IAsyncEnumerable ZipAwaitCore(this IAsyncEnumerable first, IAsyncEnumerable second, Func> selector) @@ -80,7 +72,6 @@ internal static IAsyncEnumerable ZipAwaitCore if (selector == null) throw Error.ArgumentNull(nameof(selector)); -#if USE_ASYNC_ITERATOR return Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) @@ -96,9 +87,6 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new ZipAsyncIteratorWithTask(first, second, selector); -#endif } #if !NO_DEEP_CANCELLATION @@ -111,7 +99,6 @@ internal static IAsyncEnumerable ZipAwaitWithCancellationCore Core(CancellationToken cancellationToken) @@ -127,230 +114,7 @@ async IAsyncEnumerator Core(CancellationToken cancellationToken) } } } -#else - return new ZipAsyncIteratorWithTaskAndCancellation(first, second, selector); -#endif - } -#endif - -#if !USE_ASYNC_ITERATOR - private sealed class ZipAsyncIterator : AsyncIterator - { - private readonly IAsyncEnumerable _first; - private readonly IAsyncEnumerable _second; - private readonly Func _selector; - - private IAsyncEnumerator _firstEnumerator; - private IAsyncEnumerator _secondEnumerator; - - public ZipAsyncIterator(IAsyncEnumerable first, IAsyncEnumerable second, Func selector) - { - Debug.Assert(first != null); - Debug.Assert(second != null); - Debug.Assert(selector != null); - - _first = first; - _second = second; - _selector = selector; - } - - public override AsyncIteratorBase Clone() - { - return new ZipAsyncIterator(_first, _second, _selector); - } - - public override async ValueTask DisposeAsync() - { - if (_secondEnumerator != null) - { - await _secondEnumerator.DisposeAsync().ConfigureAwait(false); - _secondEnumerator = null; - } - - if (_firstEnumerator != null) - { - await _firstEnumerator.DisposeAsync().ConfigureAwait(false); - _firstEnumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - // REVIEW: Earlier versions of this operator performed concurrent MoveNextAsync calls, which isn't a great default and - // results in an unexpected source of concurrency. However, a concurrent Zip may be a worthy addition to the - // API or System.Interactive.Async as a complementary implementation besides the conservative default. - - switch (_state) - { - case AsyncIteratorState.Allocated: - _firstEnumerator = _first.GetAsyncEnumerator(_cancellationToken); - _secondEnumerator = _second.GetAsyncEnumerator(_cancellationToken); - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - if (await _firstEnumerator.MoveNextAsync().ConfigureAwait(false) && await _secondEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = _selector(_firstEnumerator.Current, _secondEnumerator.Current); - return true; - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } } - - private sealed class ZipAsyncIteratorWithTask : AsyncIterator - { - private readonly IAsyncEnumerable _first; - private readonly IAsyncEnumerable _second; - private readonly Func> _selector; - - private IAsyncEnumerator _firstEnumerator; - private IAsyncEnumerator _secondEnumerator; - - public ZipAsyncIteratorWithTask(IAsyncEnumerable first, IAsyncEnumerable second, Func> selector) - { - Debug.Assert(first != null); - Debug.Assert(second != null); - Debug.Assert(selector != null); - - _first = first; - _second = second; - _selector = selector; - } - - public override AsyncIteratorBase Clone() - { - return new ZipAsyncIteratorWithTask(_first, _second, _selector); - } - - public override async ValueTask DisposeAsync() - { - if (_secondEnumerator != null) - { - await _secondEnumerator.DisposeAsync().ConfigureAwait(false); - _secondEnumerator = null; - } - - if (_firstEnumerator != null) - { - await _firstEnumerator.DisposeAsync().ConfigureAwait(false); - _firstEnumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - // REVIEW: Earlier versions of this operator performed concurrent MoveNextAsync calls, which isn't a great default and - // results in an unexpected source of concurrency. However, a concurrent Zip may be a worthy addition to the - // API or System.Interactive.Async as a complementary implementation besides the conservative default. - - switch (_state) - { - case AsyncIteratorState.Allocated: - _firstEnumerator = _first.GetAsyncEnumerator(_cancellationToken); - _secondEnumerator = _second.GetAsyncEnumerator(_cancellationToken); - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - if (await _firstEnumerator.MoveNextAsync().ConfigureAwait(false) && await _secondEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = await _selector(_firstEnumerator.Current, _secondEnumerator.Current).ConfigureAwait(false); - return true; - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } - -#if !NO_DEEP_CANCELLATION - private sealed class ZipAsyncIteratorWithTaskAndCancellation : AsyncIterator - { - private readonly IAsyncEnumerable _first; - private readonly IAsyncEnumerable _second; - private readonly Func> _selector; - - private IAsyncEnumerator _firstEnumerator; - private IAsyncEnumerator _secondEnumerator; - - public ZipAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable first, IAsyncEnumerable second, Func> selector) - { - Debug.Assert(first != null); - Debug.Assert(second != null); - Debug.Assert(selector != null); - - _first = first; - _second = second; - _selector = selector; - } - - public override AsyncIteratorBase Clone() - { - return new ZipAsyncIteratorWithTaskAndCancellation(_first, _second, _selector); - } - - public override async ValueTask DisposeAsync() - { - if (_secondEnumerator != null) - { - await _secondEnumerator.DisposeAsync().ConfigureAwait(false); - _secondEnumerator = null; - } - - if (_firstEnumerator != null) - { - await _firstEnumerator.DisposeAsync().ConfigureAwait(false); - _firstEnumerator = null; - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - protected override async ValueTask MoveNextCore() - { - // REVIEW: Earlier versions of this operator performed concurrent MoveNextAsync calls, which isn't a great default and - // results in an unexpected source of concurrency. However, a concurrent Zip may be a worthy addition to the - // API or System.Interactive.Async as a complementary implementation besides the conservative default. - - switch (_state) - { - case AsyncIteratorState.Allocated: - _firstEnumerator = _first.GetAsyncEnumerator(_cancellationToken); - _secondEnumerator = _second.GetAsyncEnumerator(_cancellationToken); - - _state = AsyncIteratorState.Iterating; - goto case AsyncIteratorState.Iterating; - - case AsyncIteratorState.Iterating: - if (await _firstEnumerator.MoveNextAsync().ConfigureAwait(false) && await _secondEnumerator.MoveNextAsync().ConfigureAwait(false)) - { - _current = await _selector(_firstEnumerator.Current, _secondEnumerator.Current, _cancellationToken).ConfigureAwait(false); - return true; - } - - await DisposeAsync().ConfigureAwait(false); - break; - } - - return false; - } - } -#endif #endif } }