123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Linq
- {
- public static partial class AsyncEnumerable
- {
- /// <summary>
- /// Sorts the elements of a sequence in ascending order according to a key.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements of source.</typeparam>
- /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
- /// <param name="source">An async-enumerable sequence of values to order.</param>
- /// <param name="keySelector">A function to extract a key from an element.</param>
- /// <returns>An ordered async-enumerable sequence whose elements are sorted according to a key.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
- public static IOrderedAsyncEnumerable<TSource> OrderBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector) =>
- new OrderedAsyncEnumerable<TSource, TKey>(source, keySelector, comparer: null, descending: false, parent: null);
- internal static IOrderedAsyncEnumerable<TSource> OrderByAwaitCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector) =>
- new OrderedAsyncEnumerableWithTask<TSource, TKey>(source, keySelector, comparer: null, descending: false, parent: null);
- #if !NO_DEEP_CANCELLATION
- internal static IOrderedAsyncEnumerable<TSource> OrderByAwaitWithCancellationCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector) =>
- new OrderedAsyncEnumerableWithTaskAndCancellation<TSource, TKey>(source, keySelector, comparer: null, descending: false, parent: null);
- #endif
- /// <summary>
- /// Sorts the elements of a sequence in ascending order by using a specified comparer.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements of source.</typeparam>
- /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
- /// <param name="source">An async-enumerable sequence of values to order.</param>
- /// <param name="keySelector">A function to extract a key from an element.</param>
- /// <param name="comparer">A comparer to compare keys.</param>
- /// <returns>An ordered async-enumerable sequence whose elements are sorted according to a key.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
- public static IOrderedAsyncEnumerable<TSource> OrderBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer) =>
- new OrderedAsyncEnumerable<TSource, TKey>(source, keySelector, comparer, descending: false, parent: null);
- internal static IOrderedAsyncEnumerable<TSource> OrderByAwaitCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey> comparer) =>
- new OrderedAsyncEnumerableWithTask<TSource, TKey>(source, keySelector, comparer, descending: false, parent: null);
- #if !NO_DEEP_CANCELLATION
- internal static IOrderedAsyncEnumerable<TSource> OrderByAwaitWithCancellationCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer) =>
- new OrderedAsyncEnumerableWithTaskAndCancellation<TSource, TKey>(source, keySelector, comparer, descending: false, parent: null);
- #endif
- /// <summary>
- /// Sorts the elements of a sequence in descending order according to a key.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements of source.</typeparam>
- /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
- /// <param name="source">An async-enumerable sequence of values to order.</param>
- /// <param name="keySelector">A function to extract a key from an element.</param>
- /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
- public static IOrderedAsyncEnumerable<TSource> OrderByDescending<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector) =>
- new OrderedAsyncEnumerable<TSource, TKey>(source, keySelector, comparer: null, descending: true, parent: null);
- internal static IOrderedAsyncEnumerable<TSource> OrderByDescendingAwaitCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector) =>
- new OrderedAsyncEnumerableWithTask<TSource, TKey>(source, keySelector, comparer: null, descending: true, parent: null);
- #if !NO_DEEP_CANCELLATION
- internal static IOrderedAsyncEnumerable<TSource> OrderByDescendingAwaitWithCancellationCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector) =>
- new OrderedAsyncEnumerableWithTaskAndCancellation<TSource, TKey>(source, keySelector, comparer: null, descending: true, parent: null);
- #endif
- /// <summary>
- /// Sorts the elements of a sequence in descending order by using a specified comparer.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements of source.</typeparam>
- /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
- /// <param name="source">An async-enumerable sequence of values to order.</param>
- /// <param name="keySelector">A function to extract a key from an element.</param>
- /// <param name="comparer">A comparer to compare keys.</param>
- /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
- public static IOrderedAsyncEnumerable<TSource> OrderByDescending<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer) =>
- new OrderedAsyncEnumerable<TSource, TKey>(source, keySelector, comparer, descending: true, parent: null);
- internal static IOrderedAsyncEnumerable<TSource> OrderByDescendingAwaitCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey> comparer) =>
- new OrderedAsyncEnumerableWithTask<TSource, TKey>(source, keySelector, comparer, descending: true, parent: null);
- #if !NO_DEEP_CANCELLATION
- internal static IOrderedAsyncEnumerable<TSource> OrderByDescendingAwaitWithCancellationCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer) =>
- new OrderedAsyncEnumerableWithTaskAndCancellation<TSource, TKey>(source, keySelector, comparer, descending: true, parent: null);
- #endif
- /// <summary>
- /// Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements of source.</typeparam>
- /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
- /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
- /// <param name="keySelector">A function to extract a key from each element.</param>
- /// <returns>An ordered async-enumerable whose elements are sorted according to a key.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
- public static IOrderedAsyncEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- return source.CreateOrderedEnumerable(keySelector, comparer: null, descending: false);
- }
- internal static IOrderedAsyncEnumerable<TSource> ThenByAwaitCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- return source.CreateOrderedEnumerable(keySelector, comparer: default(IComparer<TKey>), descending: false);
- }
- #if !NO_DEEP_CANCELLATION
- internal static IOrderedAsyncEnumerable<TSource> ThenByAwaitWithCancellationCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- return source.CreateOrderedEnumerable(keySelector, comparer: null, descending: false);
- }
- #endif
- /// <summary>
- /// Performs a subsequent ordering of the elements in a sequence in ascending order by using a specified comparer.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements of source.</typeparam>
- /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
- /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
- /// <param name="keySelector">A function to extract a key from each element.</param>
- /// <param name="comparer">A comparer to compare keys.</param>
- /// <returns>An ordered async-enumerable whose elements are sorted according to a key.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
- public static IOrderedAsyncEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- return source.CreateOrderedEnumerable(keySelector, comparer, descending: false);
- }
- internal static IOrderedAsyncEnumerable<TSource> ThenByAwaitCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey> comparer)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- return source.CreateOrderedEnumerable(keySelector, comparer, descending: false);
- }
- #if !NO_DEEP_CANCELLATION
- internal static IOrderedAsyncEnumerable<TSource> ThenByAwaitWithCancellationCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- return source.CreateOrderedEnumerable(keySelector, comparer, descending: false);
- }
- #endif
- /// <summary>
- /// Performs a subsequent ordering of the elements in a sequence in descending order, according to a key.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements of source.</typeparam>
- /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
- /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
- /// <param name="keySelector">A function to extract a key from each element.</param>
- /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
- public static IOrderedAsyncEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- return source.CreateOrderedEnumerable(keySelector, comparer: null, descending: true);
- }
- internal static IOrderedAsyncEnumerable<TSource> ThenByDescendingAwaitCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- return source.CreateOrderedEnumerable(keySelector, comparer: default(IComparer<TKey>), descending: true);
- }
- #if !NO_DEEP_CANCELLATION
- internal static IOrderedAsyncEnumerable<TSource> ThenByDescendingAwaitWithCancellationCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- return source.CreateOrderedEnumerable(keySelector, comparer: null, descending: true);
- }
- #endif
- /// <summary>
- /// Performs a subsequent ordering of the elements in a sequence in descending order by using a specified comparer.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements of source.</typeparam>
- /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
- /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
- /// <param name="keySelector">A function to extract a key from each element.</param>
- /// <param name="comparer">A comparer to compare keys.</param>
- /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
- public static IOrderedAsyncEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- return source.CreateOrderedEnumerable(keySelector, comparer, descending: true);
- }
- internal static IOrderedAsyncEnumerable<TSource> ThenByDescendingAwaitCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey> comparer)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- return source.CreateOrderedEnumerable(keySelector, comparer, descending: true);
- }
- #if !NO_DEEP_CANCELLATION
- internal static IOrderedAsyncEnumerable<TSource> ThenByDescendingAwaitWithCancellationCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- return source.CreateOrderedEnumerable(keySelector, comparer, descending: true);
- }
- #endif
- }
- }
|