123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- // Copied from https://github.com/dotnet/corefx/blob/5f1dd8298e4355b63bb760d88d437a91b3ca808c/src/System.Linq/src/System/Linq/Partition.cs
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Linq
- {
- /// <summary>
- /// An iterator that yields the items of part of an <see cref="IAsyncEnumerable{TSource}"/>.
- /// </summary>
- /// <typeparam name="TSource">The type of the source enumerable.</typeparam>
- internal sealed class AsyncEnumerablePartition<TSource> : AsyncIterator<TSource>, IAsyncPartition<TSource>
- {
- private readonly IAsyncEnumerable<TSource> _source;
- private readonly int _minIndexInclusive;
- private readonly int _maxIndexInclusive; // -1 if we want everything past _minIndexInclusive.
- // If this is -1, it's impossible to set a limit on the count.
- private IAsyncEnumerator<TSource> _enumerator;
- internal AsyncEnumerablePartition(IAsyncEnumerable<TSource> source, int minIndexInclusive, int maxIndexInclusive)
- {
- Debug.Assert(source != null);
- Debug.Assert(!(source is IList<TSource>), $"The caller needs to check for {nameof(IList<TSource>)}.");
- Debug.Assert(minIndexInclusive >= 0);
- Debug.Assert(maxIndexInclusive >= -1);
- // Note that although maxIndexInclusive can't grow, it can still be int.MaxValue.
- // We support partitioning enumerables with > 2B elements. For example, e.Skip(1).Take(int.MaxValue) should work.
- // But if it is int.MaxValue, then minIndexInclusive must != 0. Otherwise, our count may overflow.
- Debug.Assert(maxIndexInclusive == -1 || (maxIndexInclusive - minIndexInclusive < int.MaxValue), $"{nameof(Limit)} will overflow!");
- Debug.Assert(maxIndexInclusive == -1 || minIndexInclusive <= maxIndexInclusive);
- _source = source;
- _minIndexInclusive = minIndexInclusive;
- _maxIndexInclusive = maxIndexInclusive;
- }
- // If this is true (e.g. at least one Take call was made), then we have an upper bound
- // on how many elements we can have.
- private bool HasLimit => _maxIndexInclusive != -1;
- private int Limit => (_maxIndexInclusive + 1) - _minIndexInclusive; // This is that upper bound.
- public override AsyncIteratorBase<TSource> Clone()
- {
- return new AsyncEnumerablePartition<TSource>(_source, _minIndexInclusive, _maxIndexInclusive);
- }
- public override async ValueTask DisposeAsync()
- {
- if (_enumerator != null)
- {
- await _enumerator.DisposeAsync().ConfigureAwait(false);
- _enumerator = null;
- }
- await base.DisposeAsync().ConfigureAwait(false);
- }
- public Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
- {
- if (onlyIfCheap)
- {
- return TaskExt.MinusOne;
- }
- return Core();
- async Task<int> Core()
- {
- if (!HasLimit)
- {
- // If HasLimit is false, we contain everything past _minIndexInclusive.
- // Therefore, we have to iterate the whole enumerable.
- return Math.Max(await _source.CountAsync(cancellationToken).ConfigureAwait(false) - _minIndexInclusive, 0);
- }
- var en = _source.GetAsyncEnumerator(cancellationToken);
- try
- {
- // We only want to iterate up to _maxIndexInclusive + 1.
- // Past that, we know the enumerable will be able to fit this partition,
- // so the count will just be _maxIndexInclusive + 1 - _minIndexInclusive.
- // Note that it is possible for _maxIndexInclusive to be int.MaxValue here,
- // so + 1 may result in signed integer overflow. We need to handle this.
- // At the same time, however, we are guaranteed that our max count can fit
- // in an int because if that is true, then _minIndexInclusive must > 0.
- var count = await SkipAndCountAsync((uint)_maxIndexInclusive + 1, en).ConfigureAwait(false);
- Debug.Assert(count != (uint)int.MaxValue + 1 || _minIndexInclusive > 0, "Our return value will be incorrect.");
- return Math.Max((int)count - _minIndexInclusive, 0);
- }
- finally
- {
- await en.DisposeAsync().ConfigureAwait(false);
- }
- }
- }
- private bool _hasSkipped;
- private int _taken;
- protected override async ValueTask<bool> MoveNextCore()
- {
- switch (_state)
- {
- case AsyncIteratorState.Allocated:
- _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
- _hasSkipped = false;
- _taken = 0;
- _state = AsyncIteratorState.Iterating;
- goto case AsyncIteratorState.Iterating;
- case AsyncIteratorState.Iterating:
- if (!_hasSkipped)
- {
- if (!await SkipBeforeFirstAsync(_enumerator).ConfigureAwait(false))
- {
- // Reached the end before we finished skipping.
- break;
- }
- _hasSkipped = true;
- }
- if ((!HasLimit || _taken < Limit) && await _enumerator.MoveNextAsync().ConfigureAwait(false))
- {
- if (HasLimit)
- {
- // If we are taking an unknown number of elements, it's important not to increment _state.
- // _state - 3 may eventually end up overflowing & we'll hit the Dispose branch even though
- // we haven't finished enumerating.
- _taken++;
- }
- _current = _enumerator.Current;
- return true;
- }
- break;
- }
- await DisposeAsync().ConfigureAwait(false);
- return false;
- }
- #if NOTYET
- public override IAsyncEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector)
- {
- return new SelectIPartitionIterator<TSource, TResult>(this, selector);
- }
- public override IAsyncEnumerable<TResult> Select<TResult>(Func<TSource, ValueTask<TResult>> selector)
- {
- return new SelectIPartitionIterator<TSource, TResult>(this, selector);
- }
- #endif
- public IAsyncPartition<TSource> Skip(int count)
- {
- var minIndex = _minIndexInclusive + count;
- if (!HasLimit)
- {
- if (minIndex < 0)
- {
- // If we don't know our max count and minIndex can no longer fit in a positive int,
- // then we will need to wrap ourselves in another iterator.
- // This can happen, for example, during e.Skip(int.MaxValue).Skip(int.MaxValue).
- return new AsyncEnumerablePartition<TSource>(this, count, -1);
- }
- }
- else if ((uint)minIndex > (uint)_maxIndexInclusive)
- {
- // If minIndex overflows and we have an upper bound, we will go down this branch.
- // We know our upper bound must be smaller than minIndex, since our upper bound fits in an int.
- // This branch should not be taken if we don't have a bound.
- return AsyncEnumerable.EmptyAsyncIterator<TSource>.Instance;
- }
- Debug.Assert(minIndex >= 0, $"We should have taken care of all cases when {nameof(minIndex)} overflows.");
- return new AsyncEnumerablePartition<TSource>(_source, minIndex, _maxIndexInclusive);
- }
- public IAsyncPartition<TSource> Take(int count)
- {
- var maxIndex = _minIndexInclusive + count - 1;
- if (!HasLimit)
- {
- if (maxIndex < 0)
- {
- // If we don't know our max count and maxIndex can no longer fit in a positive int,
- // then we will need to wrap ourselves in another iterator.
- // Note that although maxIndex may be too large, the difference between it and
- // _minIndexInclusive (which is count - 1) must fit in an int.
- // Example: e.Skip(50).Take(int.MaxValue).
- return new AsyncEnumerablePartition<TSource>(this, 0, count - 1);
- }
- }
- else if ((uint)maxIndex >= (uint)_maxIndexInclusive)
- {
- // If we don't know our max count, we can't go down this branch.
- // It's always possible for us to contain more than count items, as the rest
- // of the enumerable past _minIndexInclusive can be arbitrarily long.
- return this;
- }
- Debug.Assert(maxIndex >= 0, $"We should have taken care of all cases when {nameof(maxIndex)} overflows.");
- return new AsyncEnumerablePartition<TSource>(_source, _minIndexInclusive, maxIndex);
- }
- public async Task<Maybe<TSource>> TryGetElementAsync(int index, CancellationToken cancellationToken)
- {
- // If the index is negative or >= our max count, return early.
- if (index >= 0 && (!HasLimit || index < Limit))
- {
- var en = _source.GetAsyncEnumerator(cancellationToken);
- try
- {
- Debug.Assert(_minIndexInclusive + index >= 0, $"Adding {nameof(index)} caused {nameof(_minIndexInclusive)} to overflow.");
- if (await SkipBeforeAsync(_minIndexInclusive + index, en).ConfigureAwait(false) && await en.MoveNextAsync().ConfigureAwait(false))
- {
- return new Maybe<TSource>(en.Current);
- }
- }
- finally
- {
- await en.DisposeAsync().ConfigureAwait(false);
- }
- }
- return new Maybe<TSource>();
- }
- public async Task<Maybe<TSource>> TryGetFirstAsync(CancellationToken cancellationToken)
- {
- var en = _source.GetAsyncEnumerator(cancellationToken);
- try
- {
- if (await SkipBeforeFirstAsync(en).ConfigureAwait(false) && await en.MoveNextAsync().ConfigureAwait(false))
- {
- return new Maybe<TSource>(en.Current);
- }
- }
- finally
- {
- await en.DisposeAsync().ConfigureAwait(false);
- }
- return new Maybe<TSource>();
- }
- public async Task<Maybe<TSource>> TryGetLastAsync(CancellationToken cancellationToken)
- {
- var en = _source.GetAsyncEnumerator(cancellationToken);
- try
- {
- if (await SkipBeforeFirstAsync(en).ConfigureAwait(false) && await en.MoveNextAsync().ConfigureAwait(false))
- {
- var remaining = Limit - 1; // Max number of items left, not counting the current element.
- var comparand = HasLimit ? 0 : int.MinValue; // If we don't have an upper bound, have the comparison always return true.
- TSource result;
- do
- {
- remaining--;
- result = en.Current;
- }
- while (remaining >= comparand && await en.MoveNextAsync().ConfigureAwait(false));
- return new Maybe<TSource>(result);
- }
- }
- finally
- {
- await en.DisposeAsync().ConfigureAwait(false);
- }
- return new Maybe<TSource>();
- }
- public async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
- {
- var en = _source.GetAsyncEnumerator(cancellationToken);
- try
- {
- if (await SkipBeforeFirstAsync(en).ConfigureAwait(false) && await en.MoveNextAsync().ConfigureAwait(false))
- {
- var remaining = Limit - 1; // Max number of items left, not counting the current element.
- var comparand = HasLimit ? 0 : int.MinValue; // If we don't have an upper bound, have the comparison always return true.
- var maxCapacity = HasLimit ? Limit : int.MaxValue;
- var builder = new List<TSource>(maxCapacity);
- do
- {
- remaining--;
- builder.Add(en.Current);
- }
- while (remaining >= comparand && await en.MoveNextAsync().ConfigureAwait(false));
- return builder.ToArray();
- }
- }
- finally
- {
- await en.DisposeAsync().ConfigureAwait(false);
- }
- #if NO_ARRAY_EMPTY
- return EmptyArray<TSource>.Value;
- #else
- return Array.Empty<TSource>();
- #endif
- }
- public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
- {
- var list = new List<TSource>();
- var en = _source.GetAsyncEnumerator(cancellationToken);
- try
- {
- if (await SkipBeforeFirstAsync(en).ConfigureAwait(false) && await en.MoveNextAsync().ConfigureAwait(false))
- {
- var remaining = Limit - 1; // Max number of items left, not counting the current element.
- var comparand = HasLimit ? 0 : int.MinValue; // If we don't have an upper bound, have the comparison always return true.
- do
- {
- remaining--;
- list.Add(en.Current);
- }
- while (remaining >= comparand && await en.MoveNextAsync().ConfigureAwait(false));
- }
- }
- finally
- {
- await en.DisposeAsync().ConfigureAwait(false);
- }
- return list;
- }
- private Task<bool> SkipBeforeFirstAsync(IAsyncEnumerator<TSource> en) => SkipBeforeAsync(_minIndexInclusive, en);
- private static async Task<bool> SkipBeforeAsync(int index, IAsyncEnumerator<TSource> en)
- {
- var n = await SkipAndCountAsync(index, en).ConfigureAwait(false);
- return n == index;
- }
- private static async Task<int> SkipAndCountAsync(int index, IAsyncEnumerator<TSource> en)
- {
- Debug.Assert(index >= 0);
- return (int)await SkipAndCountAsync((uint)index, en).ConfigureAwait(false);
- }
- private static async Task<uint> SkipAndCountAsync(uint index, IAsyncEnumerator<TSource> en)
- {
- Debug.Assert(en != null);
- for (uint i = 0; i < index; i++)
- {
- if (!await en.MoveNextAsync().ConfigureAwait(false))
- {
- return i;
- }
- }
- return index;
- }
- }
- }
|