// 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 minimum 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 minimum 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 minimum 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> MinByAsync(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 MinByCore(source, keySelector, comparer: null, cancellationToken);
}
///
/// Returns the elements in an async-enumerable sequence with the minimum 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 minimum 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 minimum 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> MinByAsync(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 MinByCore(source, keySelector, comparer, cancellationToken);
}
///
/// Returns the elements in an async-enumerable sequence with the minimum 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 minimum 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 minimum 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> MinByAsync(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 MinByCore(source, keySelector, comparer: null, cancellationToken);
}
#if !NO_DEEP_CANCELLATION
///
/// Returns the elements in an async-enumerable sequence with the minimum 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 minimum 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 minimum 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> MinByAsync(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 MinByCore(source, keySelector, comparer: null, cancellationToken);
}
#endif
///
/// Returns the elements in an async-enumerable sequence with the minimum 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 minimum 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 minimum 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> MinByAsync(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 MinByCore(source, keySelector, comparer, cancellationToken);
}
#if !NO_DEEP_CANCELLATION
///
/// Returns the elements in an async-enumerable sequence with the minimum 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 minimum 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 minimum 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> MinByAsync(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 MinByCore(source, keySelector, comparer, cancellationToken);
}
#endif
private static ValueTask> MinByCore(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> MinByCore(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> MinByCore(IAsyncEnumerable source, Func> keySelector, IComparer? comparer, CancellationToken cancellationToken)
{
comparer ??= Comparer.Default;
return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
}
#endif
private static async ValueTask> ExtremaBy(IAsyncEnumerable source, Func keySelector, Func compare, CancellationToken cancellationToken)
{
var result = new List();
await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
{
if (!await e.MoveNextAsync())
throw Error.NoElements();
var current = e.Current;
var resKey = keySelector(current);
result.Add(current);
while (await e.MoveNextAsync())
{
var cur = e.Current;
var key = keySelector(cur);
var cmp = compare(key, resKey);
if (cmp == 0)
{
result.Add(cur);
}
else if (cmp > 0)
{
result = new List { cur };
resKey = key;
}
}
}
return result;
}
private static async ValueTask> ExtremaBy(IAsyncEnumerable source, Func> keySelector, Func compare, CancellationToken cancellationToken)
{
var result = new List();
await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
{
if (!await e.MoveNextAsync())
throw Error.NoElements();
var current = e.Current;
var resKey = await keySelector(current).ConfigureAwait(false);
result.Add(current);
while (await e.MoveNextAsync())
{
var cur = e.Current;
var key = await keySelector(cur).ConfigureAwait(false);
var cmp = compare(key, resKey);
if (cmp == 0)
{
result.Add(cur);
}
else if (cmp > 0)
{
result = new List { cur };
resKey = key;
}
}
}
return result;
}
#if !NO_DEEP_CANCELLATION
private static async ValueTask> ExtremaBy(IAsyncEnumerable source, Func> keySelector, Func compare, CancellationToken cancellationToken)
{
var result = new List();
await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
{
if (!await e.MoveNextAsync())
throw Error.NoElements();
var current = e.Current;
var resKey = await keySelector(current, cancellationToken).ConfigureAwait(false);
result.Add(current);
while (await e.MoveNextAsync())
{
var cur = e.Current;
var key = await keySelector(cur, cancellationToken).ConfigureAwait(false);
var cmp = compare(key, resKey);
if (cmp == 0)
{
result.Add(cur);
}
else if (cmp > 0)
{
result = new List { cur };
resKey = key;
}
}
}
return result;
}
#endif
}
}