// 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 { public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func keySelector) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (keySelector == null) throw Error.ArgumentNull(nameof(keySelector)); return ToDictionaryCore(source, keySelector, x => x, EqualityComparer.Default, CancellationToken.None); } public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func keySelector, CancellationToken cancellationToken) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (keySelector == null) throw Error.ArgumentNull(nameof(keySelector)); return ToDictionaryCore(source, keySelector, x => x, EqualityComparer.Default, cancellationToken); } public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func> keySelector) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (keySelector == null) throw Error.ArgumentNull(nameof(keySelector)); return ToDictionaryCore(source, keySelector, x => new ValueTask(x), EqualityComparer.Default, CancellationToken.None); } public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func> keySelector, CancellationToken cancellationToken) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (keySelector == null) throw Error.ArgumentNull(nameof(keySelector)); return ToDictionaryCore(source, keySelector, x => new ValueTask(x), EqualityComparer.Default, cancellationToken); } #if !NO_DEEP_CANCELLATION public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func> keySelector, CancellationToken cancellationToken) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (keySelector == null) throw Error.ArgumentNull(nameof(keySelector)); return ToDictionaryCore(source, x => keySelector(x, cancellationToken), x => new ValueTask(x), EqualityComparer.Default, cancellationToken); } #endif public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (keySelector == null) throw Error.ArgumentNull(nameof(keySelector)); return ToDictionaryCore(source, keySelector, x => x, comparer, CancellationToken.None); } public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (keySelector == null) throw Error.ArgumentNull(nameof(keySelector)); return ToDictionaryCore(source, keySelector, x => x, comparer, cancellationToken); } public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (keySelector == null) throw Error.ArgumentNull(nameof(keySelector)); return ToDictionaryCore(source, keySelector, x => new ValueTask(x), comparer, CancellationToken.None); } public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (keySelector == null) throw Error.ArgumentNull(nameof(keySelector)); return ToDictionaryCore(source, keySelector, x => new ValueTask(x), comparer, cancellationToken); } #if !NO_DEEP_CANCELLATION public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (keySelector == null) throw Error.ArgumentNull(nameof(keySelector)); return ToDictionaryCore(source, x => keySelector(x, cancellationToken), x => new ValueTask(x), comparer, cancellationToken); } #endif public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func keySelector, Func elementSelector) { 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 ToDictionaryCore(source, keySelector, elementSelector, EqualityComparer.Default, CancellationToken.None); } public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func keySelector, Func elementSelector, CancellationToken cancellationToken) { 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 ToDictionaryCore(source, keySelector, elementSelector, EqualityComparer.Default, cancellationToken); } public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func> keySelector, Func> elementSelector) { 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 ToDictionaryCore(source, keySelector, elementSelector, EqualityComparer.Default, CancellationToken.None); } public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func> keySelector, Func> elementSelector, CancellationToken cancellationToken) { 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 ToDictionaryCore(source, keySelector, elementSelector, EqualityComparer.Default, cancellationToken); } #if !NO_DEEP_CANCELLATION public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func> keySelector, Func> elementSelector, CancellationToken cancellationToken) { 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 ToDictionaryCore(source, keySelector, elementSelector, EqualityComparer.Default, cancellationToken); } #endif public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer) { 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 ToDictionaryCore(source, keySelector, elementSelector, comparer, CancellationToken.None); } public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) { 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 ToDictionaryCore(source, keySelector, elementSelector, comparer, cancellationToken); } public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer) { 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 ToDictionaryCore(source, keySelector, elementSelector, comparer, CancellationToken.None); } public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) { 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 ToDictionaryCore(source, keySelector, elementSelector, comparer, cancellationToken); } #if !NO_DEEP_CANCELLATION public static Task> ToDictionaryAsync(this IAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) { 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 ToDictionaryCore(source, keySelector, elementSelector, comparer, cancellationToken); } #endif private static async Task> ToDictionaryCore(IAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) { IAsyncEnumerator e = source.GetAsyncEnumerator(cancellationToken); try { var d = new Dictionary(comparer); while (await e.MoveNextAsync().ConfigureAwait(false)) { TSource x = e.Current; TKey key = keySelector(x); TElement value = elementSelector(x); d.Add(key, value); } return d; } finally { await e.DisposeAsync().ConfigureAwait(false); } } private static async Task> ToDictionaryCore(IAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) { IAsyncEnumerator e = source.GetAsyncEnumerator(cancellationToken); try { var d = new Dictionary(comparer); while (await e.MoveNextAsync().ConfigureAwait(false)) { TSource x = e.Current; TKey key = await keySelector(x).ConfigureAwait(false); TElement value = await elementSelector(x).ConfigureAwait(false); d.Add(key, value); } return d; } finally { await e.DisposeAsync().ConfigureAwait(false); } } #if !NO_DEEP_CANCELLATION private static async Task> ToDictionaryCore(IAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) { IAsyncEnumerator e = source.GetAsyncEnumerator(cancellationToken); try { var d = new Dictionary(comparer); while (await e.MoveNextAsync().ConfigureAwait(false)) { TSource x = e.Current; TKey key = await keySelector(x, cancellationToken).ConfigureAwait(false); TElement value = await elementSelector(x, cancellationToken).ConfigureAwait(false); d.Add(key, value); } return d; } finally { await e.DisposeAsync().ConfigureAwait(false); } } #endif } }