123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT 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
- {
- #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
- // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.maxasync?view=net-9.0-pp
- // The method above has a slightly different signature: it takes a comparer. In cases where the cancellationToken
- // has not been supplied, or has been passed by name, that method is source-compatible with this one.
- // However, anyone calling this method with an unnamed cancellationToken argument will get an error because
- // the comparer argument comes before the cancellationToken argument. There's not much we can do about
- // this because if we continue to offer this method, it will result in ambiguous matches. The least bad
- // option seems to be to hide this method, and anyone who was relying on it taking a positional
- // cancellation token argument will need to deal with the compilation error.
- /// <summary>
- /// Returns the maximum element in an async-enumerable sequence.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to determine the maximum element of.</param>
- /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
- /// <returns>A ValueTask containing a single element with the maximum element in the source sequence.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
- /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
- public static ValueTask<TSource> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (default(TSource)! == null) // NB: Null value is desired; JIT-time check.
- {
- return Core(source, cancellationToken);
- static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
- {
- var comparer = Comparer<TSource>.Default;
- TSource value;
- await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
- {
- do
- {
- if (!await e.MoveNextAsync())
- {
- return default!;
- }
- value = e.Current;
- }
- while (value == null);
- while (await e.MoveNextAsync())
- {
- var x = e.Current;
- if (x != null && comparer.Compare(x, value) > 0)
- {
- value = x;
- }
- }
- }
- return value;
- }
- }
- else
- {
- return Core(source, cancellationToken);
- static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
- {
- var comparer = Comparer<TSource>.Default;
- TSource value;
- await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
- {
- if (!await e.MoveNextAsync())
- {
- throw Error.NoElements();
- }
- value = e.Current;
- while (await e.MoveNextAsync())
- {
- var x = e.Current;
- if (comparer.Compare(x, value) > 0)
- {
- value = x;
- }
- }
- }
- return value;
- }
- }
- }
- #endif
- /// <summary>
- /// Invokes a transform function on each element of a sequence and returns the maximum value.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
- /// <typeparam name="TResult">The type of the objects derived from the elements in the source sequence to determine the maximum of.</typeparam>
- /// <param name="source">An async-enumerable sequence to determine the minimum element of.</param>
- /// <param name="selector">A transform function to apply to each element.</param>
- /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
- /// <returns>A ValueTask containing a single element with the value that corresponds to the maximum element in the source sequence.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
- /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
- [Obsolete("Use MaxByAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the functionality previously provided by the MaxAsync overload that took a selector callback now exists as an overload of MaxByAsync.")]
- public static ValueTask<TResult> MaxAsync<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken = default)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (selector == null)
- throw Error.ArgumentNull(nameof(selector));
- if (default(TResult)! == null) // NB: Null value is desired; JIT-time check.
- {
- return Core(source, selector, cancellationToken);
- static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
- {
- var comparer = Comparer<TResult>.Default;
- TResult value;
- await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
- {
- do
- {
- if (!await e.MoveNextAsync())
- {
- return default!;
- }
- value = selector(e.Current);
- }
- while (value == null);
- while (await e.MoveNextAsync())
- {
- var x = selector(e.Current);
- if (x != null && comparer.Compare(x, value) > 0)
- {
- value = x;
- }
- }
- }
- return value;
- }
- }
- else
- {
- return Core(source, selector, cancellationToken);
- static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
- {
- var comparer = Comparer<TResult>.Default;
- TResult value;
- await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
- {
- if (!await e.MoveNextAsync())
- {
- throw Error.NoElements();
- }
- value = selector(e.Current);
- while (await e.MoveNextAsync())
- {
- var x = selector(e.Current);
- if (comparer.Compare(x, value) > 0)
- {
- value = x;
- }
- }
- }
- return value;
- }
- }
- }
- /// <summary>
- /// Invokes and awaits a transform function on each element of a sequence and returns the maximum value.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
- /// <typeparam name="TResult">The type of the objects derived from the elements in the source sequence to determine the maximum of.</typeparam>
- /// <param name="source">An async-enumerable sequence to determine the minimum element of.</param>
- /// <param name="selector">An asynchronous transform function to invoke and await on each element.</param>
- /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
- /// <returns>A ValueTask containing a single element with the value that corresponds to the maximum element in the source sequence.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
- /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
- [GenerateAsyncOverload]
- [Obsolete("Use MaxByAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the functionality previously provided by MaxAwaitAsync now exists as an overload of MaxByAsync.")]
- private static ValueTask<TResult> MaxAwaitAsyncCore<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TResult>> selector, CancellationToken cancellationToken = default)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (selector == null)
- throw Error.ArgumentNull(nameof(selector));
- if (default(TResult)! == null) // NB: Null value is desired; JIT-time check.
- {
- return Core(source, selector, cancellationToken);
- static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TResult>> selector, CancellationToken cancellationToken)
- {
- var comparer = Comparer<TResult>.Default;
- TResult value;
- await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
- {
- do
- {
- if (!await e.MoveNextAsync())
- {
- return default!;
- }
- value = await selector(e.Current).ConfigureAwait(false);
- }
- while (value == null);
- while (await e.MoveNextAsync())
- {
- var x = await selector(e.Current).ConfigureAwait(false);
- if (x != null && comparer.Compare(x, value) > 0)
- {
- value = x;
- }
- }
- }
- return value;
- }
- }
- else
- {
- return Core(source, selector, cancellationToken);
- static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TResult>> selector, CancellationToken cancellationToken)
- {
- var comparer = Comparer<TResult>.Default;
- TResult value;
- await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
- {
- if (!await e.MoveNextAsync())
- {
- throw Error.NoElements();
- }
- value = await selector(e.Current).ConfigureAwait(false);
- while (await e.MoveNextAsync())
- {
- var x = await selector(e.Current).ConfigureAwait(false);
- if (comparer.Compare(x, value) > 0)
- {
- value = x;
- }
- }
- }
- return value;
- }
- }
- }
- #if !NO_DEEP_CANCELLATION
- [GenerateAsyncOverload]
- [Obsolete("Use MaxByAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the functionality previously provided by MaxAwaitWithCancellationAsync now exists as an overload of MaxByAsync.")]
- private static ValueTask<TResult> MaxAwaitWithCancellationAsyncCore<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TResult>> selector, CancellationToken cancellationToken = default)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (selector == null)
- throw Error.ArgumentNull(nameof(selector));
- if (default(TResult)! == null) // NB: Null value is desired; JIT-time check.
- {
- return Core(source, selector, cancellationToken);
- static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TResult>> selector, CancellationToken cancellationToken)
- {
- var comparer = Comparer<TResult>.Default;
- TResult value;
- await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
- {
- do
- {
- if (!await e.MoveNextAsync())
- {
- return default!;
- }
- value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
- }
- while (value == null);
- while (await e.MoveNextAsync())
- {
- var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
- if (x != null && comparer.Compare(x, value) > 0)
- {
- value = x;
- }
- }
- }
- return value;
- }
- }
- else
- {
- return Core(source, selector, cancellationToken);
- static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TResult>> selector, CancellationToken cancellationToken)
- {
- var comparer = Comparer<TResult>.Default;
- TResult value;
- await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
- {
- if (!await e.MoveNextAsync())
- {
- throw Error.NoElements();
- }
- value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
- while (await e.MoveNextAsync())
- {
- var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
- if (comparer.Compare(x, value) > 0)
- {
- value = x;
- }
- }
- }
- return value;
- }
- }
- }
- #endif
- }
- }
|