ToDictionary.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. /// <summary>
  12. /// Creates a dictionary from an async-enumerable sequence according to a specified key selector function.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <typeparam name="TKey">The type of the dictionary key computed for each element in the source sequence.</typeparam>
  16. /// <param name="source">An async-enumerable sequence to create a dictionary for.</param>
  17. /// <param name="keySelector">A function to extract a key from each element.</param>
  18. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  19. /// <returns>An async-enumerable sequence containing a single element with a dictionary mapping unique key values onto the corresponding source sequence's element.</returns>
  20. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  21. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  22. public static ValueTask<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken = default) where TKey : notnull =>
  23. ToDictionaryAsync(source, keySelector, comparer: null, cancellationToken);
  24. /// <summary>
  25. /// Creates a dictionary from an async-enumerable sequence according to a specified key selector function, and a comparer.
  26. /// </summary>
  27. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  28. /// <typeparam name="TKey">The type of the dictionary key computed for each element in the source sequence.</typeparam>
  29. /// <param name="source">An async-enumerable sequence to create a dictionary for.</param>
  30. /// <param name="keySelector">A function to extract a key from each element.</param>
  31. /// <param name="comparer">An equality comparer to compare keys.</param>
  32. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  33. /// <returns>An async-enumerable sequence containing a single element with a dictionary mapping unique key values onto the corresponding source sequence's element.</returns>
  34. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="comparer"/> is null.</exception>
  35. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  36. 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
  37. {
  38. if (source == null)
  39. throw Error.ArgumentNull(nameof(source));
  40. if (keySelector == null)
  41. throw Error.ArgumentNull(nameof(keySelector));
  42. return Core(source, keySelector, comparer, cancellationToken);
  43. static async ValueTask<Dictionary<TKey, TSource>> Core(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
  44. {
  45. var d = new Dictionary<TKey, TSource>(comparer);
  46. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  47. {
  48. var key = keySelector(item);
  49. d.Add(key, item);
  50. }
  51. return d;
  52. }
  53. }
  54. internal static ValueTask<Dictionary<TKey, TSource>> ToDictionaryAwaitAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, CancellationToken cancellationToken = default) where TKey : notnull =>
  55. ToDictionaryAwaitAsyncCore<TSource, TKey>(source, keySelector, comparer: null, cancellationToken);
  56. 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
  57. {
  58. if (source == null)
  59. throw Error.ArgumentNull(nameof(source));
  60. if (keySelector == null)
  61. throw Error.ArgumentNull(nameof(keySelector));
  62. return Core(source, keySelector, comparer, cancellationToken);
  63. static async ValueTask<Dictionary<TKey, TSource>> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
  64. {
  65. var d = new Dictionary<TKey, TSource>(comparer);
  66. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  67. {
  68. var key = await keySelector(item).ConfigureAwait(false);
  69. d.Add(key, item);
  70. }
  71. return d;
  72. }
  73. }
  74. #if !NO_DEEP_CANCELLATION
  75. 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 =>
  76. ToDictionaryAwaitWithCancellationAsyncCore(source, keySelector, comparer: null, cancellationToken);
  77. 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
  78. {
  79. if (source == null)
  80. throw Error.ArgumentNull(nameof(source));
  81. if (keySelector == null)
  82. throw Error.ArgumentNull(nameof(keySelector));
  83. return Core(source, keySelector, comparer, cancellationToken);
  84. static async ValueTask<Dictionary<TKey, TSource>> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
  85. {
  86. var d = new Dictionary<TKey, TSource>(comparer);
  87. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  88. {
  89. var key = await keySelector(item, cancellationToken).ConfigureAwait(false);
  90. d.Add(key, item);
  91. }
  92. return d;
  93. }
  94. }
  95. #endif
  96. /// <summary>
  97. /// Creates a dictionary from an async-enumerable sequence according to a specified key selector function, and an element selector function.
  98. /// </summary>
  99. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  100. /// <typeparam name="TKey">The type of the dictionary key computed for each element in the source sequence.</typeparam>
  101. /// <typeparam name="TElement">The type of the dictionary value computed for each element in the source sequence.</typeparam>
  102. /// <param name="source">An async-enumerable sequence to create a dictionary for.</param>
  103. /// <param name="keySelector">A function to extract a key from each element.</param>
  104. /// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
  105. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  106. /// <returns>An async-enumerable sequence containing a single element with a dictionary mapping unique key values onto the corresponding source sequence's element.</returns>
  107. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> is null.</exception>
  108. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  109. 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 =>
  110. ToDictionaryAsync(source, keySelector, elementSelector, comparer: null, cancellationToken);
  111. /// <summary>
  112. /// Creates a dictionary from an async-enumerable sequence according to a specified key selector function, a comparer, and an element selector function.
  113. /// </summary>
  114. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  115. /// <typeparam name="TKey">The type of the dictionary key computed for each element in the source sequence.</typeparam>
  116. /// <typeparam name="TElement">The type of the dictionary value computed for each element in the source sequence.</typeparam>
  117. /// <param name="source">An async-enumerable sequence to create a dictionary for.</param>
  118. /// <param name="keySelector">A function to extract a key from each element.</param>
  119. /// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
  120. /// <param name="comparer">An equality comparer to compare keys.</param>
  121. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  122. /// <returns>An async-enumerable sequence containing a single element with a dictionary mapping unique key values onto the corresponding source sequence's element.</returns>
  123. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> or <paramref name="comparer"/> is null.</exception>
  124. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  125. 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
  126. {
  127. if (source == null)
  128. throw Error.ArgumentNull(nameof(source));
  129. if (keySelector == null)
  130. throw Error.ArgumentNull(nameof(keySelector));
  131. if (elementSelector == null)
  132. throw Error.ArgumentNull(nameof(elementSelector));
  133. return Core(source, keySelector, elementSelector, comparer, cancellationToken);
  134. static async ValueTask<Dictionary<TKey, TElement>> Core(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
  135. {
  136. var d = new Dictionary<TKey, TElement>(comparer);
  137. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  138. {
  139. var key = keySelector(item);
  140. var value = elementSelector(item);
  141. d.Add(key, value);
  142. }
  143. return d;
  144. }
  145. }
  146. 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 =>
  147. ToDictionaryAwaitAsyncCore<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer: null, cancellationToken);
  148. 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
  149. {
  150. if (source == null)
  151. throw Error.ArgumentNull(nameof(source));
  152. if (keySelector == null)
  153. throw Error.ArgumentNull(nameof(keySelector));
  154. if (elementSelector == null)
  155. throw Error.ArgumentNull(nameof(elementSelector));
  156. return Core(source, keySelector, elementSelector, comparer, cancellationToken);
  157. 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)
  158. {
  159. var d = new Dictionary<TKey, TElement>(comparer);
  160. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  161. {
  162. var key = await keySelector(item).ConfigureAwait(false);
  163. var value = await elementSelector(item).ConfigureAwait(false);
  164. d.Add(key, value);
  165. }
  166. return d;
  167. }
  168. }
  169. #if !NO_DEEP_CANCELLATION
  170. 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 =>
  171. ToDictionaryAwaitWithCancellationAsyncCore(source, keySelector, elementSelector, comparer: null, cancellationToken);
  172. 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
  173. {
  174. if (source == null)
  175. throw Error.ArgumentNull(nameof(source));
  176. if (keySelector == null)
  177. throw Error.ArgumentNull(nameof(keySelector));
  178. if (elementSelector == null)
  179. throw Error.ArgumentNull(nameof(elementSelector));
  180. return Core(source, keySelector, elementSelector, comparer, cancellationToken);
  181. 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)
  182. {
  183. var d = new Dictionary<TKey, TElement>(comparer);
  184. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  185. {
  186. var key = await keySelector(item, cancellationToken).ConfigureAwait(false);
  187. var value = await elementSelector(item, cancellationToken).ConfigureAwait(false);
  188. d.Add(key, value);
  189. }
  190. return d;
  191. }
  192. }
  193. #endif
  194. }
  195. }