Esempio n. 1
0
        protected override async ValueTask <bool> MoveNextCore()
        {
            switch (_state)
            {
            case AsyncIteratorState.Allocated:
                _buffer = await _source.ToArrayAsync(_cancellationToken).ConfigureAwait(false);     // TODO: Use buffer.

                _minIndexIterator = _minIndexInclusive;
                _maxIndexIterator = _maxIndexInclusive;

                var count = _buffer.Length;

                if (count > _minIndexIterator)
                {
                    if (count <= _maxIndexIterator)
                    {
                        _maxIndexIterator = count - 1;
                    }

                    var sorter = _source.GetAsyncEnumerableSorter(_cancellationToken);

                    if (_minIndexIterator == _maxIndexIterator)
                    {
                        _current = await sorter.ElementAt(_buffer, _buffer.Length, _minIndexIterator).ConfigureAwait(false);

                        _minIndexIterator = int.MaxValue;
                        _maxIndexIterator = int.MinValue;

                        _state = AsyncIteratorState.Iterating;
                        return(true);
                    }
                    else
                    {
                        _indexes = await sorter.Sort(_buffer, _buffer.Length, _minIndexIterator, _maxIndexIterator).ConfigureAwait(false);
                    }

                    _state = AsyncIteratorState.Iterating;
                    goto case AsyncIteratorState.Iterating;
                }

                await DisposeAsync();

                break;

            case AsyncIteratorState.Iterating:
                if (_minIndexIterator <= _maxIndexIterator)
                {
                    _current = _buffer[_indexes[_minIndexIterator++]];
                    return(true);
                }

                await DisposeAsync().ConfigureAwait(false);

                break;
            }

            return(false);
        }
Esempio n. 2
0
        internal override AsyncEnumerableSorter <TElement> GetAsyncEnumerableSorter(AsyncEnumerableSorter <TElement> next, CancellationToken cancellationToken)
        {
            var sorter = new AsyncKeySelectorAsyncEnumerableSorterWithCancellation <TElement, TKey>(_keySelector, _comparer, _descending, next, cancellationToken);

            if (_parent != null)
            {
                return(_parent.GetAsyncEnumerableSorter(sorter, cancellationToken));
            }

            return(sorter);
        }