// 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 AsyncEnumerableEx
{
///
/// Returns the elements in an async-enumerable sequence with the maximum key value.
///
/// The type of the elements in the source sequence.
/// The type of the key computed for each element in the source sequence.
/// An async-enumerable sequence to get the maximum elements for.
/// Key selector function.
/// The optional cancellation token to be used for cancelling the sequence at any time.
/// A ValueTask containing a list of zero or more elements that have a maximum key value.
/// or is null.
/// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.
public static ValueTask> MaxByAsync(this IAsyncEnumerable source, Func keySelector, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
if (keySelector == null)
throw Error.ArgumentNull(nameof(keySelector));
return MaxByCore(source, keySelector, comparer: null, cancellationToken);
}
///
/// Returns the elements in an async-enumerable sequence with the maximum key value according to the specified comparer.
///
/// The type of the elements in the source sequence.
/// The type of the key computed for each element in the source sequence.
/// An async-enumerable sequence to get the maximum elements for.
/// Key selector function.
/// Comparer used to compare key values.
/// The optional cancellation token to be used for cancelling the sequence at any time.
/// A ValueTask containing a list of zero or more elements that have a maximum key value.
/// or or is null.
/// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.
public static ValueTask> MaxByAsync(this IAsyncEnumerable source, Func keySelector, IComparer? comparer, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
if (keySelector == null)
throw Error.ArgumentNull(nameof(keySelector));
return MaxByCore(source, keySelector, comparer, cancellationToken);
}
///
/// Returns the elements in an async-enumerable sequence with the maximum key value.
///
/// The type of the elements in the source sequence.
/// The type of the key computed for each element in the source sequence.
/// An async-enumerable sequence to get the maximum elements for.
/// Key selector function returning a key possibly asynchronously.
/// The optional cancellation token to be used for cancelling the sequence at any time.
/// A ValueTask containing a list of zero or more elements that have a maximum key value.
/// or is null.
/// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.
public static ValueTask> MaxByAsync(this IAsyncEnumerable source, Func> keySelector, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
if (keySelector == null)
throw Error.ArgumentNull(nameof(keySelector));
return MaxByCore(source, keySelector, comparer: null, cancellationToken);
}
#if !NO_DEEP_CANCELLATION
///
/// Returns the elements in an async-enumerable sequence with the maximum key value.
///
/// The type of the elements in the source sequence.
/// The type of the key computed for each element in the source sequence.
/// An async-enumerable sequence to get the maximum elements for.
/// Key selector function returning a key possibly asynchronously and supporting cancellation.
/// The optional cancellation token to be used for cancelling the sequence at any time.
/// A ValueTask containing a list of zero or more elements that have a maximum key value.
/// or is null.
/// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.
public static ValueTask> MaxByAsync(this IAsyncEnumerable source, Func> keySelector, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
if (keySelector == null)
throw Error.ArgumentNull(nameof(keySelector));
return MaxByCore(source, keySelector, comparer: null, cancellationToken);
}
#endif
///
/// Returns the elements in an async-enumerable sequence with the maximum key value according to the specified comparer.
///
/// The type of the elements in the source sequence.
/// The type of the key computed for each element in the source sequence.
/// An async-enumerable sequence to get the maximum elements for.
/// Key selector function returning a key possibly asynchronously.
/// Comparer used to compare key values.
/// The optional cancellation token to be used for cancelling the sequence at any time.
/// A ValueTask containing a list of zero or more elements that have a maximum key value.
/// or or is null.
/// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.
public static ValueTask> MaxByAsync(this IAsyncEnumerable source, Func> keySelector, IComparer? comparer, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
if (keySelector == null)
throw Error.ArgumentNull(nameof(keySelector));
return MaxByCore(source, keySelector, comparer, cancellationToken);
}
#if !NO_DEEP_CANCELLATION
///
/// Returns the elements in an async-enumerable sequence with the maximum key value according to the specified comparer.
///
/// The type of the elements in the source sequence.
/// The type of the key computed for each element in the source sequence.
/// An async-enumerable sequence to get the maximum elements for.
/// Key selector function returning a key possibly asynchronously and supporting cancellation.
/// Comparer used to compare key values.
/// The optional cancellation token to be used for cancelling the sequence at any time.
/// A ValueTask containing a list of zero or more elements that have a maximum key value.
/// or or is null.
/// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.
public static ValueTask> MaxByAsync(this IAsyncEnumerable source, Func> keySelector, IComparer? comparer, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
if (keySelector == null)
throw Error.ArgumentNull(nameof(keySelector));
return MaxByCore(source, keySelector, comparer, cancellationToken);
}
#endif
private static ValueTask> MaxByCore(IAsyncEnumerable source, Func keySelector, IComparer? comparer, CancellationToken cancellationToken)
{
comparer ??= Comparer.Default;
return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue), cancellationToken);
}
private static ValueTask> MaxByCore(IAsyncEnumerable source, Func> keySelector, IComparer? comparer, CancellationToken cancellationToken)
{
comparer ??= Comparer.Default;
return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue), cancellationToken);
}
#if !NO_DEEP_CANCELLATION
private static ValueTask> MaxByCore(IAsyncEnumerable source, Func> keySelector, IComparer? comparer, CancellationToken cancellationToken)
{
comparer ??= Comparer.Default;
return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue), cancellationToken);
}
#endif
}
}