123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- // 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.tolookupasync?view=net-9.0-pp#system-linq-asyncenumerable-tolookupasync-2(system-collections-generic-iasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-iequalitycomparer((-1))-system-threading-cancellationtoken)
- // That one overload covers the next two methods, because it supplieds a default comparer.
- /// <summary>
- /// Creates a lookup 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 lookup key computed for each element in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to create a lookup 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 lookup mapping unique key values onto the corresponding source sequence's elements.</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<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken = default) =>
- ToLookupAsync(source, keySelector, comparer: null, cancellationToken);
- /// <summary>
- /// Creates a lookup 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 lookup key computed for each element in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to create a lookup 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 lookup mapping unique key values onto the corresponding source sequence's elements.</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<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken = default)
- {
- 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<ILookup<TKey, TSource>> Core(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
- {
- return await Internal.Lookup<TKey, TSource>.CreateAsync(source, keySelector, comparer, cancellationToken).ConfigureAwait(false);
- }
- }
- #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
- /// <summary>
- /// Creates a lookup from an async-enumerable sequence by invoking a key-selector function on each element and awaiting the result.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
- /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
- /// <param name="keySelector">An asynchronous 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>A ValueTask containing a lookup mapping unique key values onto the corresponding source sequence's elements.</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>
- [GenerateAsyncOverload]
- [Obsolete("Use ToLookupAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ToLookupAwaitAsync functionality now exists as overloads of ToLookupAsync.")]
- private static ValueTask<ILookup<TKey, TSource>> ToLookupAwaitAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, CancellationToken cancellationToken = default) =>
- ToLookupAwaitAsyncCore<TSource, TKey>(source, keySelector, comparer:null, cancellationToken);
- /// <summary>
- /// Creates a lookup from an async-enumerable sequence by invoking a key-selector function on each element and awaiting the result.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
- /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
- /// <param name="keySelector">An asynchronous 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>A ValueTask containing a lookup mapping unique key values onto the corresponding source sequence's elements.</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>
- [GenerateAsyncOverload]
- [Obsolete("Use ToLookupAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ToLookupAwaitAsync functionality now exists as overloads of ToLookupAsync.")]
- private static ValueTask<ILookup<TKey, TSource>> ToLookupAwaitAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken = default)
- {
- 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<ILookup<TKey, TSource>> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
- {
- return await Internal.LookupWithTask<TKey, TSource>.CreateAsync(source, keySelector, comparer, cancellationToken).ConfigureAwait(false);
- }
- }
- #if !NO_DEEP_CANCELLATION
- [GenerateAsyncOverload]
- [Obsolete("Use ToLookupAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ToLookupAwaitWithCancellationAsync functionality now exists as overloads of ToLookupAsync.")]
- private static ValueTask<ILookup<TKey, TSource>> ToLookupAwaitWithCancellationAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, CancellationToken cancellationToken = default) =>
- ToLookupAwaitWithCancellationAsyncCore(source, keySelector, comparer: null, cancellationToken);
- [GenerateAsyncOverload]
- [Obsolete("Use ToLookupAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ToLookupAwaitWithCancellationAsync functionality now exists as overloads of ToLookupAsync.")]
- private static ValueTask<ILookup<TKey, TSource>> ToLookupAwaitWithCancellationAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken = default)
- {
- 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<ILookup<TKey, TSource>> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
- {
- return await Internal.LookupWithTask<TKey, TSource>.CreateAsync(source, keySelector, comparer, cancellationToken).ConfigureAwait(false);
- }
- }
- #endif
- #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
- // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.tolookupasync?view=net-9.0-pp#system-linq-asyncenumerable-tolookupasync-3(system-collections-generic-iasyncenumerable((-0))-system-func((-0-1))-system-func((-0-2))-system-collections-generic-iequalitycomparer((-1))-system-threading-cancellationtoken)
- // That one overload covers the next two methods, because it supplieds a default comparer.
- /// <summary>
- /// Creates a lookup 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 lookup key computed for each element in the source sequence.</typeparam>
- /// <typeparam name="TElement">The type of the lookup value computed for each element in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to create a lookup 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 lookup mapping unique key values onto the corresponding source sequence's elements.</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<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken = default) =>
- ToLookupAsync(source, keySelector, elementSelector, comparer: null, cancellationToken);
- /// <summary>
- /// Creates a lookup 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 lookup key computed for each element in the source sequence.</typeparam>
- /// <typeparam name="TElement">The type of the lookup value computed for each element in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to create a lookup 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 lookup mapping unique key values onto the corresponding source sequence's elements.</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<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken = default)
- {
- 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<ILookup<TKey, TElement>> Core(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
- {
- return await Internal.Lookup<TKey, TElement>.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false);
- }
- }
- #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
- /// <summary>
- /// Creates a lookup from an async-enumerable sequence by invoking key and element selector functions on each source element and awaiting the results.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
- /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
- /// <typeparam name="TElement">The type of the lookup value computed for each element in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
- /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
- /// <param name="elementSelector">An asynchronous 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 lookup mapping unique key values onto the corresponding source sequence's elements.</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>
- [GenerateAsyncOverload]
- [Obsolete("Use ToLookupAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ToLookupAwaitAsync functionality now exists as overloads of ToLookupAsync.")]
- private static ValueTask<ILookup<TKey, TElement>> ToLookupAwaitAsyncCore<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, CancellationToken cancellationToken = default) =>
- ToLookupAwaitAsyncCore<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer: null, cancellationToken);
- /// <summary>
- /// Creates a lookup from an async-enumerable sequence by invoking key and element selector functions on each source element and awaiting the results.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
- /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
- /// <typeparam name="TElement">The type of the lookup value computed for each element in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
- /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
- /// <param name="elementSelector">An asynchronous transform function to produce a result element value from each source 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>A ValueTask containing a lookup mapping unique key values onto the corresponding source sequence's elements.</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>
- [GenerateAsyncOverload]
- [Obsolete("Use ToLookupAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ToLookupAwaitAsync functionality now exists as overloads of ToLookupAsync.")]
- private static ValueTask<ILookup<TKey, TElement>> ToLookupAwaitAsyncCore<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken = default)
- {
- 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<ILookup<TKey, TElement>> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
- {
- return await Internal.LookupWithTask<TKey, TElement>.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false);
- }
- }
- #if !NO_DEEP_CANCELLATION
- [GenerateAsyncOverload]
- [Obsolete("Use ToLookupAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ToLookupAwaitWithCancellationAsync functionality now exists as overloads of ToLookupAsync.")]
- private static ValueTask<ILookup<TKey, TElement>> ToLookupAwaitWithCancellationAsyncCore<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, CancellationToken cancellationToken = default) =>
- ToLookupAwaitWithCancellationAsyncCore(source, keySelector, elementSelector, comparer: null, cancellationToken);
- [GenerateAsyncOverload]
- [Obsolete("Use ToLookupAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ToLookupAwaitWithCancellationAsync functionality now exists as overloads of ToLookupAsync.")]
- private static ValueTask<ILookup<TKey, TElement>> ToLookupAwaitWithCancellationAsyncCore<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)
- {
- 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<ILookup<TKey, TElement>> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
- {
- return await Internal.LookupWithTask<TKey, TElement>.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false);
- }
- }
- #endif
- }
- }
|