// 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 lookup 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 lookup key computed for each element in the source sequence. /// An async-enumerable sequence to create a lookup 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 lookup mapping unique key values onto the corresponding source sequence's elements. /// 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> ToLookupAsync(this IAsyncEnumerable source, Func keySelector, CancellationToken cancellationToken = default) => ToLookupAsync(source, keySelector, comparer: null, cancellationToken); /// /// Creates a lookup 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 lookup key computed for each element in the source sequence. /// An async-enumerable sequence to create a lookup 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 lookup mapping unique key values onto the corresponding source sequence's elements. /// 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> ToLookupAsync(this IAsyncEnumerable source, Func keySelector, IEqualityComparer? 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> Core(IAsyncEnumerable source, Func keySelector, IEqualityComparer? comparer, CancellationToken cancellationToken) { return await Internal.Lookup.CreateAsync(source, keySelector, comparer, cancellationToken).ConfigureAwait(false); } } internal static ValueTask> ToLookupAwaitAsyncCore(this IAsyncEnumerable source, Func> keySelector, CancellationToken cancellationToken = default) => ToLookupAwaitAsyncCore(source, keySelector, comparer:null, cancellationToken); internal static ValueTask> ToLookupAwaitAsyncCore(this IAsyncEnumerable source, Func> keySelector, IEqualityComparer? 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> Core(IAsyncEnumerable source, Func> keySelector, IEqualityComparer? comparer, CancellationToken cancellationToken) { return await Internal.LookupWithTask.CreateAsync(source, keySelector, comparer, cancellationToken).ConfigureAwait(false); } } #if !NO_DEEP_CANCELLATION internal static ValueTask> ToLookupAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> keySelector, CancellationToken cancellationToken = default) => ToLookupAwaitWithCancellationAsyncCore(source, keySelector, comparer: null, cancellationToken); internal static ValueTask> ToLookupAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> keySelector, IEqualityComparer? 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> Core(IAsyncEnumerable source, Func> keySelector, IEqualityComparer? comparer, CancellationToken cancellationToken) { return await Internal.LookupWithTask.CreateAsync(source, keySelector, comparer, cancellationToken).ConfigureAwait(false); } } #endif /// /// Creates a lookup 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 lookup key computed for each element in the source sequence. /// The type of the lookup value computed for each element in the source sequence. /// An async-enumerable sequence to create a lookup 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 lookup mapping unique key values onto the corresponding source sequence's elements. /// 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> ToLookupAsync(this IAsyncEnumerable source, Func keySelector, Func elementSelector, CancellationToken cancellationToken = default) => ToLookupAsync(source, keySelector, elementSelector, comparer: null, cancellationToken); /// /// Creates a lookup 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 lookup key computed for each element in the source sequence. /// The type of the lookup value computed for each element in the source sequence. /// An async-enumerable sequence to create a lookup 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 lookup mapping unique key values onto the corresponding source sequence's elements. /// 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> ToLookupAsync(this IAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer? 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> Core(IAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer? comparer, CancellationToken cancellationToken) { return await Internal.Lookup.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false); } } internal static ValueTask> ToLookupAwaitAsyncCore(this IAsyncEnumerable source, Func> keySelector, Func> elementSelector, CancellationToken cancellationToken = default) => ToLookupAwaitAsyncCore(source, keySelector, elementSelector, comparer: null, cancellationToken); internal static ValueTask> ToLookupAwaitAsyncCore(this IAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer? 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> Core(IAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer? comparer, CancellationToken cancellationToken) { return await Internal.LookupWithTask.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false); } } #if !NO_DEEP_CANCELLATION internal static ValueTask> ToLookupAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> keySelector, Func> elementSelector, CancellationToken cancellationToken = default) => ToLookupAwaitWithCancellationAsyncCore(source, keySelector, elementSelector, comparer: null, cancellationToken); internal static ValueTask> ToLookupAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer? 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> Core(IAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer? comparer, CancellationToken cancellationToken) { return await Internal.LookupWithTask.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false); } } #endif } }