123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- // 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>
- /// Creates a dictionary from an async-enumerable sequence according to a specified key selector function.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
- /// <typeparam name="TKey">The type of the dictionary key computed for each element in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to create a dictionary for.</param>
- /// <param name="keySelector">A function to extract a key from each element.</param>
- /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
- /// <returns>An async-enumerable sequence containing a single element with a dictionary mapping unique key values onto the corresponding source sequence's element.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> 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<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken = default) where TKey : notnull =>
- ToDictionaryAsync(source, keySelector, comparer: null, cancellationToken);
- /// <summary>
- /// Creates a dictionary from an async-enumerable sequence according to a specified key selector function, and a comparer.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
- /// <typeparam name="TKey">The type of the dictionary key computed for each element in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to create a dictionary for.</param>
- /// <param name="keySelector">A function to extract a key from each element.</param>
- /// <param name="comparer">An equality comparer to compare keys.</param>
- /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
- /// <returns>An async-enumerable sequence containing a single element with a dictionary mapping unique key values onto the corresponding source sequence's element.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="comparer"/> 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<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? 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<Dictionary<TKey, TSource>> Core(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
- {
- var d = new Dictionary<TKey, TSource>(comparer);
- await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
- {
- var key = keySelector(item);
- d.Add(key, item);
- }
- return d;
- }
- }
- internal static ValueTask<Dictionary<TKey, TSource>> ToDictionaryAwaitAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, CancellationToken cancellationToken = default) where TKey : notnull =>
- ToDictionaryAwaitAsyncCore<TSource, TKey>(source, keySelector, comparer: null, cancellationToken);
- internal static ValueTask<Dictionary<TKey, TSource>> ToDictionaryAwaitAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? 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<Dictionary<TKey, TSource>> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
- {
- var d = new Dictionary<TKey, TSource>(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
- internal static ValueTask<Dictionary<TKey, TSource>> ToDictionaryAwaitWithCancellationAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, CancellationToken cancellationToken = default) where TKey : notnull =>
- ToDictionaryAwaitWithCancellationAsyncCore(source, keySelector, comparer: null, cancellationToken);
- internal static ValueTask<Dictionary<TKey, TSource>> ToDictionaryAwaitWithCancellationAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? 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<Dictionary<TKey, TSource>> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
- {
- var d = new Dictionary<TKey, TSource>(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
- /// <summary>
- /// Creates a dictionary from an async-enumerable sequence according to a specified key selector function, and an element selector function.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
- /// <typeparam name="TKey">The type of the dictionary key computed for each element in the source sequence.</typeparam>
- /// <typeparam name="TElement">The type of the dictionary value computed for each element in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to create a dictionary for.</param>
- /// <param name="keySelector">A function to extract a key from each element.</param>
- /// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
- /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
- /// <returns>An async-enumerable sequence containing a single element with a dictionary mapping unique key values onto the corresponding source sequence's element.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> 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<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken = default) where TKey : notnull =>
- ToDictionaryAsync(source, keySelector, elementSelector, comparer: null, cancellationToken);
- /// <summary>
- /// Creates a dictionary from an async-enumerable sequence according to a specified key selector function, a comparer, and an element selector function.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
- /// <typeparam name="TKey">The type of the dictionary key computed for each element in the source sequence.</typeparam>
- /// <typeparam name="TElement">The type of the dictionary value computed for each element in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to create a dictionary for.</param>
- /// <param name="keySelector">A function to extract a key from each element.</param>
- /// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
- /// <param name="comparer">An equality comparer to compare keys.</param>
- /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
- /// <returns>An async-enumerable sequence containing a single element with a dictionary mapping unique key values onto the corresponding source sequence's element.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> or <paramref name="comparer"/> 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<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? 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<Dictionary<TKey, TElement>> Core(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
- {
- var d = new Dictionary<TKey, TElement>(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;
- }
- }
- internal static ValueTask<Dictionary<TKey, TElement>> ToDictionaryAwaitAsyncCore<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, CancellationToken cancellationToken = default) where TKey : notnull =>
- ToDictionaryAwaitAsyncCore<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer: null, cancellationToken);
- internal static ValueTask<Dictionary<TKey, TElement>> ToDictionaryAwaitAsyncCore<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey>? 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<Dictionary<TKey, TElement>> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
- {
- var d = new Dictionary<TKey, TElement>(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
- internal static ValueTask<Dictionary<TKey, TElement>> ToDictionaryAwaitWithCancellationAsyncCore<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, CancellationToken cancellationToken = default) where TKey : notnull =>
- ToDictionaryAwaitWithCancellationAsyncCore(source, keySelector, elementSelector, comparer: null, cancellationToken);
- internal static ValueTask<Dictionary<TKey, TElement>> ToDictionaryAwaitWithCancellationAsyncCore<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey>? 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<Dictionary<TKey, TElement>> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
- {
- var d = new Dictionary<TKey, TElement>(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
- }
- }
|