// 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
{
///
/// Creates a dictionary from an async-enumerable sequence according to a specified key selector function.
///
/// The type of the elements in the source sequence.
/// The type of the dictionary key computed for each element in the source sequence.
/// An async-enumerable sequence to create a dictionary for.
/// A function to extract a key from each element.
/// The optional cancellation token to be used for cancelling the sequence at any time.
/// An async-enumerable sequence containing a single element with a dictionary mapping unique key values onto the corresponding source sequence's element.
/// 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> ToDictionaryAsync(this IAsyncEnumerable source, Func keySelector, CancellationToken cancellationToken = default) where TKey : notnull =>
ToDictionaryAsync(source, keySelector, comparer: null, cancellationToken);
///
/// Creates a dictionary from an async-enumerable sequence according to a specified key selector function, and a comparer.
///
/// The type of the elements in the source sequence.
/// The type of the dictionary key computed for each element in the source sequence.
/// An async-enumerable sequence to create a dictionary for.
/// A function to extract a key from each element.
/// An equality comparer to compare keys.
/// The optional cancellation token to be used for cancelling the sequence at any time.
/// An async-enumerable sequence containing a single element with a dictionary mapping unique key values onto the corresponding source sequence's element.
/// 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> ToDictionaryAsync(this IAsyncEnumerable source, Func keySelector, IEqualityComparer? comparer, CancellationToken cancellationToken = default) where TKey : notnull
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
if (keySelector == null)
throw Error.ArgumentNull(nameof(keySelector));
return Core(source, keySelector, comparer, cancellationToken);
static async ValueTask> Core(IAsyncEnumerable source, Func keySelector, IEqualityComparer? comparer, CancellationToken cancellationToken)
{
var d = new Dictionary(comparer);
await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
{
var key = keySelector(item);
d.Add(key, item);
}
return d;
}
}
///
/// Creates a dictionary from an async-enumerable sequence by invoking a key-selector function on each element and awaiting the result.
///
/// The type of the elements in the source sequence.
/// The type of the dictionary key computed for each element in the source sequence.
/// An async-enumerable sequence to create a dictionary for.
/// An asynchronous function to extract a key from each element.
/// The optional cancellation token to be used for cancelling the sequence at any time.
/// A ValueTask containing a dictionary mapping unique key values onto the corresponding source sequence's element.
/// or is null.
/// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.
[GenerateAsyncOverload]
private static ValueTask> ToDictionaryAwaitAsyncCore(this IAsyncEnumerable source, Func> keySelector, CancellationToken cancellationToken = default) where TKey : notnull =>
ToDictionaryAwaitAsyncCore(source, keySelector, comparer: null, cancellationToken);
///
/// Creates a dictionary from an async-enumerable sequence by invoking a key-selector function on each element and awaiting the result.
///
/// The type of the elements in the source sequence.
/// The type of the dictionary key computed for each element in the source sequence.
/// An async-enumerable sequence to create a dictionary for.
/// An asynchronous function to extract a key from each element.
/// An equality comparer to compare keys.
/// The optional cancellation token to be used for cancelling the sequence at any time.
/// A ValueTask containing a dictionary mapping unique key values onto the corresponding source sequence's element.
/// or or is null.
/// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.
[GenerateAsyncOverload]
private static ValueTask> ToDictionaryAwaitAsyncCore(this IAsyncEnumerable source, Func> keySelector, IEqualityComparer? comparer, CancellationToken cancellationToken = default) where TKey : notnull
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
if (keySelector == null)
throw Error.ArgumentNull(nameof(keySelector));
return Core(source, keySelector, comparer, cancellationToken);
static async ValueTask> Core(IAsyncEnumerable source, Func> keySelector, IEqualityComparer? comparer, CancellationToken cancellationToken)
{
var d = new Dictionary(comparer);
await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
{
var key = await keySelector(item).ConfigureAwait(false);
d.Add(key, item);
}
return d;
}
}
#if !NO_DEEP_CANCELLATION
[GenerateAsyncOverload]
private static ValueTask> ToDictionaryAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> keySelector, CancellationToken cancellationToken = default) where TKey : notnull =>
ToDictionaryAwaitWithCancellationAsyncCore(source, keySelector, comparer: null, cancellationToken);
[GenerateAsyncOverload]
private static ValueTask> ToDictionaryAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> keySelector, IEqualityComparer? comparer, CancellationToken cancellationToken = default) where TKey : notnull
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
if (keySelector == null)
throw Error.ArgumentNull(nameof(keySelector));
return Core(source, keySelector, comparer, cancellationToken);
static async ValueTask> Core(IAsyncEnumerable source, Func> keySelector, IEqualityComparer? comparer, CancellationToken cancellationToken)
{
var d = new Dictionary(comparer);
await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
{
var key = await keySelector(item, cancellationToken).ConfigureAwait(false);
d.Add(key, item);
}
return d;
}
}
#endif
///
/// Creates a dictionary from an async-enumerable sequence according to a specified key selector function, and an element selector function.
///
/// The type of the elements in the source sequence.
/// The type of the dictionary key computed for each element in the source sequence.
/// The type of the dictionary value computed for each element in the source sequence.
/// An async-enumerable sequence to create a dictionary for.
/// A function to extract a key from each element.
/// A transform function to produce a result element value from each element.
/// The optional cancellation token to be used for cancelling the sequence at any time.
/// An async-enumerable sequence containing a single element with a dictionary mapping unique key values onto the corresponding source sequence's element.
/// 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> ToDictionaryAsync(this IAsyncEnumerable source, Func keySelector, Func elementSelector, CancellationToken cancellationToken = default) where TKey : notnull =>
ToDictionaryAsync(source, keySelector, elementSelector, comparer: null, cancellationToken);
///
/// Creates a dictionary from an async-enumerable sequence according to a specified key selector function, a comparer, and an element selector function.
///
/// The type of the elements in the source sequence.
/// The type of the dictionary key computed for each element in the source sequence.
/// The type of the dictionary value computed for each element in the source sequence.
/// An async-enumerable sequence to create a dictionary for.
/// A function to extract a key from each element.
/// A transform function to produce a result element value from each element.
/// An equality comparer to compare keys.
/// The optional cancellation token to be used for cancelling the sequence at any time.
/// An async-enumerable sequence containing a single element with a dictionary mapping unique key values onto the corresponding source sequence's element.
/// or 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> ToDictionaryAsync(this IAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer? comparer, CancellationToken cancellationToken = default) where TKey : notnull
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
if (keySelector == null)
throw Error.ArgumentNull(nameof(keySelector));
if (elementSelector == null)
throw Error.ArgumentNull(nameof(elementSelector));
return Core(source, keySelector, elementSelector, comparer, cancellationToken);
static async ValueTask> Core(IAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer? comparer, CancellationToken cancellationToken)
{
var d = new Dictionary(comparer);
await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
{
var key = keySelector(item);
var value = elementSelector(item);
d.Add(key, value);
}
return d;
}
}
///
/// Creates a dictionary from an async-enumerable sequence using the specified asynchronous key and element selector functions.
///
/// The type of the elements in the source sequence.
/// The type of the dictionary key computed for each element in the source sequence.
/// The type of the dictionary value computed for each element in the source sequence.
/// An async-enumerable sequence to create a dictionary for.
/// An asynchronous function to extract a key from each element.
/// An asynchronous transform function to produce a result element value from each element.
/// The optional cancellation token to be used for cancelling the sequence at any time.
/// A ValueTask containing a dictionary mapping unique key values onto the corresponding source sequence's element.
/// or or is null.
/// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.
[GenerateAsyncOverload]
private static ValueTask> ToDictionaryAwaitAsyncCore(this IAsyncEnumerable source, Func> keySelector, Func> elementSelector, CancellationToken cancellationToken = default) where TKey : notnull =>
ToDictionaryAwaitAsyncCore(source, keySelector, elementSelector, comparer: null, cancellationToken);
///
/// Creates a dictionary from an async-enumerable sequence using the specified asynchronous key and element selector functions.
///
/// The type of the elements in the source sequence.
/// The type of the dictionary key computed for each element in the source sequence.
/// The type of the dictionary value computed for each element in the source sequence.
/// An async-enumerable sequence to create a dictionary for.
/// An asynchronous function to extract a key from each element.
/// An asynchronous transform function to produce a result element value from each element.
/// An equality comparer to compare keys.
/// The optional cancellation token to be used for cancelling the sequence at any time.
/// A ValueTask containing a dictionary mapping unique key values onto the corresponding source sequence's element.
/// or or or is null.
/// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.
[GenerateAsyncOverload]
private static ValueTask> ToDictionaryAwaitAsyncCore(this IAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer? comparer, CancellationToken cancellationToken = default) where TKey : notnull
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
if (keySelector == null)
throw Error.ArgumentNull(nameof(keySelector));
if (elementSelector == null)
throw Error.ArgumentNull(nameof(elementSelector));
return Core(source, keySelector, elementSelector, comparer, cancellationToken);
static async ValueTask> Core(IAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer? comparer, CancellationToken cancellationToken)
{
var d = new Dictionary(comparer);
await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
{
var key = await keySelector(item).ConfigureAwait(false);
var value = await elementSelector(item).ConfigureAwait(false);
d.Add(key, value);
}
return d;
}
}
#if !NO_DEEP_CANCELLATION
[GenerateAsyncOverload]
private static ValueTask> ToDictionaryAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> keySelector, Func> elementSelector, CancellationToken cancellationToken = default) where TKey : notnull =>
ToDictionaryAwaitWithCancellationAsyncCore(source, keySelector, elementSelector, comparer: null, cancellationToken);
[GenerateAsyncOverload]
private static ValueTask> ToDictionaryAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer? comparer, CancellationToken cancellationToken = default) where TKey : notnull
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
if (keySelector == null)
throw Error.ArgumentNull(nameof(keySelector));
if (elementSelector == null)
throw Error.ArgumentNull(nameof(elementSelector));
return Core(source, keySelector, elementSelector, comparer, cancellationToken);
static async ValueTask> Core(IAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer? comparer, CancellationToken cancellationToken)
{
var d = new Dictionary(comparer);
await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
{
var key = await keySelector(item, cancellationToken).ConfigureAwait(false);
var value = await elementSelector(item, cancellationToken).ConfigureAwait(false);
d.Add(key, value);
}
return d;
}
}
#endif
}
}