public async ValueTask <TElement[]> ToArrayAsync(CancellationToken cancellationToken) { var elements = await AsyncEnumerableHelpers.ToArrayWithLength(_source, cancellationToken).ConfigureAwait(false); var count = elements.Length; if (count == 0) { #if NO_ARRAY_EMPTY return(EmptyArray <TElement> .Value); #else return(Array.Empty <TElement>()); #endif } var array = elements.Array; var map = await SortedMap(array, count, cancellationToken).ConfigureAwait(false); var result = new TElement[count]; for (var i = 0; i < result.Length; i++) { result[i] = array[map[i]]; } return(result); }
public ValueTask <Maybe <TElement> > TryGetElementAtAsync(int index, CancellationToken cancellationToken) { if (index == 0) { return(TryGetFirstAsync(cancellationToken)); } if (index > 0) { return(Core()); async ValueTask <Maybe <TElement> > Core() { var elements = await AsyncEnumerableHelpers.ToArrayWithLength(_source, cancellationToken).ConfigureAwait(false); var count = elements.Length; if (index < count) { var sorter = GetAsyncEnumerableSorter(cancellationToken); var element = await sorter.ElementAt(elements.Array, count, index).ConfigureAwait(false); return(new Maybe <TElement>(element)); } return(new Maybe <TElement>()); } } return(new ValueTask <Maybe <TElement> >(new Maybe <TElement>())); }
internal async ValueTask <Maybe <TElement> > TryGetLastAsync(int minIndexInclusive, int maxIndexInclusive, CancellationToken cancellationToken) { var elements = await AsyncEnumerableHelpers.ToArrayWithLength(_source, cancellationToken).ConfigureAwait(false); var count = elements.Length; if (minIndexInclusive >= count) { return(new Maybe <TElement>()); } var array = elements.Array; TElement last; if (maxIndexInclusive < count - 1) { var sorter = GetAsyncEnumerableSorter(cancellationToken); last = await sorter.ElementAt(array, count, maxIndexInclusive).ConfigureAwait(false); } else { last = await Last(array, count, cancellationToken).ConfigureAwait(false); } return(new Maybe <TElement>(last)); }
internal async ValueTask <TElement[]> ToArrayAsync(int minIndexInclusive, int maxIndexInclusive, CancellationToken cancellationToken) { var elements = await AsyncEnumerableHelpers.ToArrayWithLength(_source, cancellationToken).ConfigureAwait(false); var count = elements.Length; if (count <= minIndexInclusive) { #if NO_ARRAY_EMPTY return(EmptyArray <TElement> .Value); #else return(Array.Empty <TElement>()); #endif } if (count <= maxIndexInclusive) { maxIndexInclusive = count - 1; } var array = elements.Array; if (minIndexInclusive == maxIndexInclusive) { var sorter = GetAsyncEnumerableSorter(cancellationToken); var element = await sorter.ElementAt(array, count, minIndexInclusive).ConfigureAwait(false); return(new TElement[] { element }); } var map = await SortedMap(array, count, minIndexInclusive, maxIndexInclusive, cancellationToken).ConfigureAwait(false); var result = new TElement[maxIndexInclusive - minIndexInclusive + 1]; for (var i = 0; minIndexInclusive <= maxIndexInclusive; i++) { result[i] = array[map[minIndexInclusive++]]; } return(result); }
internal async ValueTask <List <TElement> > ToListAsync(int minIndexInclusive, int maxIndexInclusive, CancellationToken cancellationToken) { var elements = await AsyncEnumerableHelpers.ToArrayWithLength(_source, cancellationToken).ConfigureAwait(false); var count = elements.Length; if (count <= minIndexInclusive) { return(new List <TElement>(0)); } if (count <= maxIndexInclusive) { maxIndexInclusive = count - 1; } var array = elements.Array; if (minIndexInclusive == maxIndexInclusive) { var sorter = GetAsyncEnumerableSorter(cancellationToken); var element = await sorter.ElementAt(array, count, minIndexInclusive).ConfigureAwait(false); return(new List <TElement>(1) { element }); } var map = await SortedMap(array, count, minIndexInclusive, maxIndexInclusive, cancellationToken).ConfigureAwait(false); var list = new List <TElement>(maxIndexInclusive - minIndexInclusive + 1); while (minIndexInclusive <= maxIndexInclusive) { list.Add(array[map[minIndexInclusive++]]); } return(list); }
public async ValueTask <List <TElement> > ToListAsync(CancellationToken cancellationToken) { var elements = await AsyncEnumerableHelpers.ToArrayWithLength(_source, cancellationToken).ConfigureAwait(false); var count = elements.Length; if (count == 0) { return(new List <TElement>(capacity: 0)); } var array = elements.Array; var map = await SortedMap(array, count, cancellationToken).ConfigureAwait(false); var result = new List <TElement>(count); for (var i = 0; i < count; i++) { result.Add(array[map[i]]); } return(result); }