ToDictionary.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. /// <summary>
  55. /// Creates a dictionary from an async-enumerable sequence by invoking a key-selector function on each element and awaiting the result.
  56. /// </summary>
  57. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  58. /// <typeparam name="TKey">The type of the dictionary key computed for each element in the source sequence.</typeparam>
  59. /// <param name="source">An async-enumerable sequence to create a dictionary for.</param>
  60. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  61. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  62. /// <returns>A ValueTask containing a dictionary mapping unique key values onto the corresponding source sequence's element.</returns>
  63. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  64. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  65. [GenerateAsyncOverload]
  66. private static ValueTask<Dictionary<TKey, TSource>> ToDictionaryAwaitAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, CancellationToken cancellationToken = default) where TKey : notnull =>
  67. ToDictionaryAwaitAsyncCore<TSource, TKey>(source, keySelector, comparer: null, cancellationToken);
  68. /// <summary>
  69. /// Creates a dictionary from an async-enumerable sequence by invoking a key-selector function on each element and awaiting the result.
  70. /// </summary>
  71. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  72. /// <typeparam name="TKey">The type of the dictionary key computed for each element in the source sequence.</typeparam>
  73. /// <param name="source">An async-enumerable sequence to create a dictionary for.</param>
  74. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  75. /// <param name="comparer">An equality comparer to compare keys.</param>
  76. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  77. /// <returns>A ValueTask containing a dictionary mapping unique key values onto the corresponding source sequence's element.</returns>
  78. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="comparer"/> is null.</exception>
  79. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  80. [GenerateAsyncOverload]
  81. private 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
  82. {
  83. if (source == null)
  84. throw Error.ArgumentNull(nameof(source));
  85. if (keySelector == null)
  86. throw Error.ArgumentNull(nameof(keySelector));
  87. return Core(source, keySelector, comparer, cancellationToken);
  88. static async ValueTask<Dictionary<TKey, TSource>> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
  89. {
  90. var d = new Dictionary<TKey, TSource>(comparer);
  91. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  92. {
  93. var key = await keySelector(item).ConfigureAwait(false);
  94. d.Add(key, item);
  95. }
  96. return d;
  97. }
  98. }
  99. #if !NO_DEEP_CANCELLATION
  100. [GenerateAsyncOverload]
  101. private static ValueTask<Dictionary<TKey, TSource>> ToDictionaryAwaitWithCancellationAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, CancellationToken cancellationToken = default) where TKey : notnull =>
  102. ToDictionaryAwaitWithCancellationAsyncCore(source, keySelector, comparer: null, cancellationToken);
  103. [GenerateAsyncOverload]
  104. private 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
  105. {
  106. if (source == null)
  107. throw Error.ArgumentNull(nameof(source));
  108. if (keySelector == null)
  109. throw Error.ArgumentNull(nameof(keySelector));
  110. return Core(source, keySelector, comparer, cancellationToken);
  111. static async ValueTask<Dictionary<TKey, TSource>> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
  112. {
  113. var d = new Dictionary<TKey, TSource>(comparer);
  114. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  115. {
  116. var key = await keySelector(item, cancellationToken).ConfigureAwait(false);
  117. d.Add(key, item);
  118. }
  119. return d;
  120. }
  121. }
  122. #endif
  123. /// <summary>
  124. /// Creates a dictionary from an async-enumerable sequence according to a specified key selector function, and an element selector function.
  125. /// </summary>
  126. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  127. /// <typeparam name="TKey">The type of the dictionary key computed for each element in the source sequence.</typeparam>
  128. /// <typeparam name="TElement">The type of the dictionary value computed for each element in the source sequence.</typeparam>
  129. /// <param name="source">An async-enumerable sequence to create a dictionary for.</param>
  130. /// <param name="keySelector">A function to extract a key from each element.</param>
  131. /// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
  132. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  133. /// <returns>An async-enumerable sequence containing a single element with a dictionary mapping unique key values onto the corresponding source sequence's element.</returns>
  134. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> is null.</exception>
  135. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  136. 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 =>
  137. ToDictionaryAsync(source, keySelector, elementSelector, comparer: null, cancellationToken);
  138. /// <summary>
  139. /// Creates a dictionary from an async-enumerable sequence according to a specified key selector function, a comparer, and an element selector function.
  140. /// </summary>
  141. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  142. /// <typeparam name="TKey">The type of the dictionary key computed for each element in the source sequence.</typeparam>
  143. /// <typeparam name="TElement">The type of the dictionary value computed for each element in the source sequence.</typeparam>
  144. /// <param name="source">An async-enumerable sequence to create a dictionary for.</param>
  145. /// <param name="keySelector">A function to extract a key from each element.</param>
  146. /// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
  147. /// <param name="comparer">An equality comparer to compare keys.</param>
  148. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  149. /// <returns>An async-enumerable sequence containing a single element with a dictionary mapping unique key values onto the corresponding source sequence's element.</returns>
  150. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> or <paramref name="comparer"/> is null.</exception>
  151. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  152. 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
  153. {
  154. if (source == null)
  155. throw Error.ArgumentNull(nameof(source));
  156. if (keySelector == null)
  157. throw Error.ArgumentNull(nameof(keySelector));
  158. if (elementSelector == null)
  159. throw Error.ArgumentNull(nameof(elementSelector));
  160. return Core(source, keySelector, elementSelector, comparer, cancellationToken);
  161. static async ValueTask<Dictionary<TKey, TElement>> Core(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
  162. {
  163. var d = new Dictionary<TKey, TElement>(comparer);
  164. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  165. {
  166. var key = keySelector(item);
  167. var value = elementSelector(item);
  168. d.Add(key, value);
  169. }
  170. return d;
  171. }
  172. }
  173. /// <summary>
  174. /// Creates a dictionary from an async-enumerable sequence using the specified asynchronous key and element selector functions.
  175. /// </summary>
  176. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  177. /// <typeparam name="TKey">The type of the dictionary key computed for each element in the source sequence.</typeparam>
  178. /// <typeparam name="TElement">The type of the dictionary value computed for each element in the source sequence.</typeparam>
  179. /// <param name="source">An async-enumerable sequence to create a dictionary for.</param>
  180. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  181. /// <param name="elementSelector">An asynchronous transform function to produce a result element value from each element.</param>
  182. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  183. /// <returns>A ValueTask containing a dictionary mapping unique key values onto the corresponding source sequence's element.</returns>
  184. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> is null.</exception>
  185. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  186. [GenerateAsyncOverload]
  187. private 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 =>
  188. ToDictionaryAwaitAsyncCore<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer: null, cancellationToken);
  189. /// <summary>
  190. /// Creates a dictionary from an async-enumerable sequence using the specified asynchronous key and element selector functions.
  191. /// </summary>
  192. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  193. /// <typeparam name="TKey">The type of the dictionary key computed for each element in the source sequence.</typeparam>
  194. /// <typeparam name="TElement">The type of the dictionary value computed for each element in the source sequence.</typeparam>
  195. /// <param name="source">An async-enumerable sequence to create a dictionary for.</param>
  196. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  197. /// <param name="elementSelector">An asynchronous transform function to produce a result element value from each element.</param>
  198. /// <param name="comparer">An equality comparer to compare keys.</param>
  199. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  200. /// <returns>A ValueTask containing a dictionary mapping unique key values onto the corresponding source sequence's element.</returns>
  201. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> or <paramref name="comparer"/> is null.</exception>
  202. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  203. [GenerateAsyncOverload]
  204. private 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
  205. {
  206. if (source == null)
  207. throw Error.ArgumentNull(nameof(source));
  208. if (keySelector == null)
  209. throw Error.ArgumentNull(nameof(keySelector));
  210. if (elementSelector == null)
  211. throw Error.ArgumentNull(nameof(elementSelector));
  212. return Core(source, keySelector, elementSelector, comparer, cancellationToken);
  213. 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)
  214. {
  215. var d = new Dictionary<TKey, TElement>(comparer);
  216. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  217. {
  218. var key = await keySelector(item).ConfigureAwait(false);
  219. var value = await elementSelector(item).ConfigureAwait(false);
  220. d.Add(key, value);
  221. }
  222. return d;
  223. }
  224. }
  225. #if !NO_DEEP_CANCELLATION
  226. [GenerateAsyncOverload]
  227. private 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 =>
  228. ToDictionaryAwaitWithCancellationAsyncCore(source, keySelector, elementSelector, comparer: null, cancellationToken);
  229. [GenerateAsyncOverload]
  230. private 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
  231. {
  232. if (source == null)
  233. throw Error.ArgumentNull(nameof(source));
  234. if (keySelector == null)
  235. throw Error.ArgumentNull(nameof(keySelector));
  236. if (elementSelector == null)
  237. throw Error.ArgumentNull(nameof(elementSelector));
  238. return Core(source, keySelector, elementSelector, comparer, cancellationToken);
  239. 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)
  240. {
  241. var d = new Dictionary<TKey, TElement>(comparer);
  242. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  243. {
  244. var key = await keySelector(item, cancellationToken).ConfigureAwait(false);
  245. var value = await elementSelector(item, cancellationToken).ConfigureAwait(false);
  246. d.Add(key, value);
  247. }
  248. return d;
  249. }
  250. }
  251. #endif
  252. }
  253. }