ToDictionary.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. public static Task<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken = default) =>
  12. ToDictionaryAsync<TSource, TKey>(source, keySelector, comparer: null, cancellationToken);
  13. public static Task<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
  14. {
  15. if (source == null)
  16. throw Error.ArgumentNull(nameof(source));
  17. if (keySelector == null)
  18. throw Error.ArgumentNull(nameof(keySelector));
  19. return ToDictionaryCore(source, keySelector, x => x, comparer, cancellationToken);
  20. }
  21. public static Task<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, CancellationToken cancellationToken = default) =>
  22. ToDictionaryAsync<TSource, TKey>(source, keySelector, comparer: null, cancellationToken);
  23. public static Task<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
  24. {
  25. if (source == null)
  26. throw Error.ArgumentNull(nameof(source));
  27. if (keySelector == null)
  28. throw Error.ArgumentNull(nameof(keySelector));
  29. return ToDictionaryCore(source, keySelector, x => new ValueTask<TSource>(x), comparer, cancellationToken);
  30. }
  31. #if !NO_DEEP_CANCELLATION
  32. public static Task<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, CancellationToken cancellationToken = default) =>
  33. ToDictionaryAsync<TSource, TKey>(source, keySelector, comparer: null, cancellationToken);
  34. public static Task<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
  35. {
  36. if (source == null)
  37. throw Error.ArgumentNull(nameof(source));
  38. if (keySelector == null)
  39. throw Error.ArgumentNull(nameof(keySelector));
  40. return ToDictionaryCore(source, x => keySelector(x, cancellationToken), x => new ValueTask<TSource>(x), comparer, cancellationToken);
  41. }
  42. #endif
  43. public static Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken = default) =>
  44. ToDictionaryAsync<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer: null, cancellationToken);
  45. public static Task<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)
  46. {
  47. if (source == null)
  48. throw Error.ArgumentNull(nameof(source));
  49. if (keySelector == null)
  50. throw Error.ArgumentNull(nameof(keySelector));
  51. if (elementSelector == null)
  52. throw Error.ArgumentNull(nameof(elementSelector));
  53. return ToDictionaryCore(source, keySelector, elementSelector, comparer, cancellationToken);
  54. }
  55. public static Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, CancellationToken cancellationToken = default) =>
  56. ToDictionaryAsync<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer: null, cancellationToken);
  57. public static Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
  58. {
  59. if (source == null)
  60. throw Error.ArgumentNull(nameof(source));
  61. if (keySelector == null)
  62. throw Error.ArgumentNull(nameof(keySelector));
  63. if (elementSelector == null)
  64. throw Error.ArgumentNull(nameof(elementSelector));
  65. return ToDictionaryCore(source, keySelector, elementSelector, comparer, cancellationToken);
  66. }
  67. #if !NO_DEEP_CANCELLATION
  68. public static Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, CancellationToken cancellationToken = default) =>
  69. ToDictionaryAsync(source, keySelector, elementSelector, comparer: null, cancellationToken);
  70. public static Task<Dictionary<TKey, TElement>> ToDictionaryAsync<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)
  71. {
  72. if (source == null)
  73. throw Error.ArgumentNull(nameof(source));
  74. if (keySelector == null)
  75. throw Error.ArgumentNull(nameof(keySelector));
  76. if (elementSelector == null)
  77. throw Error.ArgumentNull(nameof(elementSelector));
  78. return ToDictionaryCore(source, keySelector, elementSelector, comparer, cancellationToken);
  79. }
  80. #endif
  81. private static async Task<Dictionary<TKey, TElement>> ToDictionaryCore<TSource, TKey, TElement>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  82. {
  83. var d = new Dictionary<TKey, TElement>(comparer);
  84. #if CSHARP8 && AETOR_HAS_CT // CS0656 Missing compiler required member 'System.Collections.Generic.IAsyncEnumerable`1.GetAsyncEnumerator'
  85. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  86. {
  87. var key = keySelector(item);
  88. var value = elementSelector(item);
  89. d.Add(key, value);
  90. }
  91. #else
  92. var e = source.GetAsyncEnumerator(cancellationToken);
  93. try
  94. {
  95. while (await e.MoveNextAsync().ConfigureAwait(false))
  96. {
  97. var x = e.Current;
  98. var key = keySelector(x);
  99. var value = elementSelector(x);
  100. d.Add(key, value);
  101. }
  102. }
  103. finally
  104. {
  105. await e.DisposeAsync().ConfigureAwait(false);
  106. }
  107. #endif
  108. return d;
  109. }
  110. private static async Task<Dictionary<TKey, TElement>> ToDictionaryCore<TSource, TKey, TElement>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  111. {
  112. var d = new Dictionary<TKey, TElement>(comparer);
  113. #if CSHARP8 && AETOR_HAS_CT // CS0656 Missing compiler required member 'System.Collections.Generic.IAsyncEnumerable`1.GetAsyncEnumerator'
  114. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  115. {
  116. var key = await keySelector(item).ConfigureAwait(false);
  117. var value = await elementSelector(item).ConfigureAwait(false);
  118. d.Add(key, value);
  119. }
  120. #else
  121. var e = source.GetAsyncEnumerator(cancellationToken);
  122. try
  123. {
  124. while (await e.MoveNextAsync().ConfigureAwait(false))
  125. {
  126. var x = e.Current;
  127. var key = await keySelector(x).ConfigureAwait(false);
  128. var value = await elementSelector(x).ConfigureAwait(false);
  129. d.Add(key, value);
  130. }
  131. }
  132. finally
  133. {
  134. await e.DisposeAsync().ConfigureAwait(false);
  135. }
  136. #endif
  137. return d;
  138. }
  139. #if !NO_DEEP_CANCELLATION
  140. private static async Task<Dictionary<TKey, TElement>> ToDictionaryCore<TSource, TKey, TElement>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  141. {
  142. var d = new Dictionary<TKey, TElement>(comparer);
  143. #if CSHARP8 && AETOR_HAS_CT // CS0656 Missing compiler required member 'System.Collections.Generic.IAsyncEnumerable`1.GetAsyncEnumerator'
  144. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  145. {
  146. var key = await keySelector(item, cancellationToken).ConfigureAwait(false);
  147. var value = await elementSelector(item, cancellationToken).ConfigureAwait(false);
  148. d.Add(key, value);
  149. }
  150. #else
  151. var e = source.GetAsyncEnumerator(cancellationToken);
  152. try
  153. {
  154. while (await e.MoveNextAsync().ConfigureAwait(false))
  155. {
  156. var x = e.Current;
  157. var key = await keySelector(x, cancellationToken).ConfigureAwait(false);
  158. var value = await elementSelector(x, cancellationToken).ConfigureAwait(false);
  159. d.Add(key, value);
  160. }
  161. }
  162. finally
  163. {
  164. await e.DisposeAsync().ConfigureAwait(false);
  165. }
  166. #endif
  167. return d;
  168. }
  169. #endif
  170. }
  171. }