ToLookup.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 lookup 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 lookup key computed for each element in the source sequence.</typeparam>
  16. /// <param name="source">An async-enumerable sequence to create a lookup 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 lookup mapping unique key values onto the corresponding source sequence's elements.</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<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken = default) =>
  23. ToLookupAsync(source, keySelector, comparer: null, cancellationToken);
  24. /// <summary>
  25. /// Creates a lookup 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 lookup key computed for each element in the source sequence.</typeparam>
  29. /// <param name="source">An async-enumerable sequence to create a lookup 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 lookup mapping unique key values onto the corresponding source sequence's elements.</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<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken = default)
  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<ILookup<TKey, TSource>> Core(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
  44. {
  45. return await Internal.Lookup<TKey, TSource>.CreateAsync(source, keySelector, comparer, cancellationToken).ConfigureAwait(false);
  46. }
  47. }
  48. /// <summary>
  49. /// Creates a lookup from an async-enumerable sequence by invoking a key-selector function on each element and awaiting the result.
  50. /// </summary>
  51. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  52. /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
  53. /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
  54. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  55. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  56. /// <returns>A ValueTask containing a lookup mapping unique key values onto the corresponding source sequence's elements.</returns>
  57. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  58. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  59. [GenerateAsyncOverload]
  60. private static ValueTask<ILookup<TKey, TSource>> ToLookupAwaitAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, CancellationToken cancellationToken = default) =>
  61. ToLookupAwaitAsyncCore<TSource, TKey>(source, keySelector, comparer:null, cancellationToken);
  62. /// <summary>
  63. /// Creates a lookup from an async-enumerable sequence by invoking a key-selector function on each element and awaiting the result.
  64. /// </summary>
  65. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  66. /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
  67. /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
  68. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  69. /// <param name="comparer">An equality comparer to compare keys.</param>
  70. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  71. /// <returns>A ValueTask containing a lookup mapping unique key values onto the corresponding source sequence's elements.</returns>
  72. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="comparer"/> is null.</exception>
  73. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  74. [GenerateAsyncOverload]
  75. private static ValueTask<ILookup<TKey, TSource>> ToLookupAwaitAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken = default)
  76. {
  77. if (source == null)
  78. throw Error.ArgumentNull(nameof(source));
  79. if (keySelector == null)
  80. throw Error.ArgumentNull(nameof(keySelector));
  81. return Core(source, keySelector, comparer, cancellationToken);
  82. static async ValueTask<ILookup<TKey, TSource>> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
  83. {
  84. return await Internal.LookupWithTask<TKey, TSource>.CreateAsync(source, keySelector, comparer, cancellationToken).ConfigureAwait(false);
  85. }
  86. }
  87. #if !NO_DEEP_CANCELLATION
  88. [GenerateAsyncOverload]
  89. private static ValueTask<ILookup<TKey, TSource>> ToLookupAwaitWithCancellationAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, CancellationToken cancellationToken = default) =>
  90. ToLookupAwaitWithCancellationAsyncCore(source, keySelector, comparer: null, cancellationToken);
  91. [GenerateAsyncOverload]
  92. private static ValueTask<ILookup<TKey, TSource>> ToLookupAwaitWithCancellationAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken = default)
  93. {
  94. if (source == null)
  95. throw Error.ArgumentNull(nameof(source));
  96. if (keySelector == null)
  97. throw Error.ArgumentNull(nameof(keySelector));
  98. return Core(source, keySelector, comparer, cancellationToken);
  99. static async ValueTask<ILookup<TKey, TSource>> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
  100. {
  101. return await Internal.LookupWithTask<TKey, TSource>.CreateAsync(source, keySelector, comparer, cancellationToken).ConfigureAwait(false);
  102. }
  103. }
  104. #endif
  105. /// <summary>
  106. /// Creates a lookup from an async-enumerable sequence according to a specified key selector function, and an element selector function.
  107. /// </summary>
  108. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  109. /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
  110. /// <typeparam name="TElement">The type of the lookup value computed for each element in the source sequence.</typeparam>
  111. /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
  112. /// <param name="keySelector">A function to extract a key from each element.</param>
  113. /// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
  114. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  115. /// <returns>An async-enumerable sequence containing a single element with a lookup mapping unique key values onto the corresponding source sequence's elements.</returns>
  116. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> is null.</exception>
  117. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  118. public static ValueTask<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken = default) =>
  119. ToLookupAsync(source, keySelector, elementSelector, comparer: null, cancellationToken);
  120. /// <summary>
  121. /// Creates a lookup from an async-enumerable sequence according to a specified key selector function, a comparer, and an element selector function.
  122. /// </summary>
  123. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  124. /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
  125. /// <typeparam name="TElement">The type of the lookup value computed for each element in the source sequence.</typeparam>
  126. /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
  127. /// <param name="keySelector">A function to extract a key from each element.</param>
  128. /// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
  129. /// <param name="comparer">An equality comparer to compare keys.</param>
  130. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  131. /// <returns>An async-enumerable sequence containing a single element with a lookup mapping unique key values onto the corresponding source sequence's elements.</returns>
  132. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> or <paramref name="comparer"/> is null.</exception>
  133. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  134. public static ValueTask<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken = default)
  135. {
  136. if (source == null)
  137. throw Error.ArgumentNull(nameof(source));
  138. if (keySelector == null)
  139. throw Error.ArgumentNull(nameof(keySelector));
  140. if (elementSelector == null)
  141. throw Error.ArgumentNull(nameof(elementSelector));
  142. return Core(source, keySelector, elementSelector, comparer, cancellationToken);
  143. static async ValueTask<ILookup<TKey, TElement>> Core(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
  144. {
  145. return await Internal.Lookup<TKey, TElement>.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false);
  146. }
  147. }
  148. /// <summary>
  149. /// Creates a lookup from an async-enumerable sequence by invoking key and element selector functions on each source element and awaiting the results.
  150. /// </summary>
  151. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  152. /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
  153. /// <typeparam name="TElement">The type of the lookup value computed for each element in the source sequence.</typeparam>
  154. /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
  155. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  156. /// <param name="elementSelector">An asynchronous transform function to produce a result element value from each element.</param>
  157. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  158. /// <returns>An async-enumerable sequence containing a single element with a lookup mapping unique key values onto the corresponding source sequence's elements.</returns>
  159. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> is null.</exception>
  160. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  161. [GenerateAsyncOverload]
  162. private static ValueTask<ILookup<TKey, TElement>> ToLookupAwaitAsyncCore<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, CancellationToken cancellationToken = default) =>
  163. ToLookupAwaitAsyncCore<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer: null, cancellationToken);
  164. /// <summary>
  165. /// Creates a lookup from an async-enumerable sequence by invoking key and element selector functions on each source element and awaiting the results.
  166. /// </summary>
  167. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  168. /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
  169. /// <typeparam name="TElement">The type of the lookup value computed for each element in the source sequence.</typeparam>
  170. /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
  171. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  172. /// <param name="elementSelector">An asynchronous transform function to produce a result element value from each source element.</param>
  173. /// <param name="comparer">An equality comparer to compare keys.</param>
  174. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  175. /// <returns>A ValueTask containing a lookup mapping unique key values onto the corresponding source sequence's elements.</returns>
  176. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> or <paramref name="comparer"/> is null.</exception>
  177. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  178. [GenerateAsyncOverload]
  179. private static ValueTask<ILookup<TKey, TElement>> ToLookupAwaitAsyncCore<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken = default)
  180. {
  181. if (source == null)
  182. throw Error.ArgumentNull(nameof(source));
  183. if (keySelector == null)
  184. throw Error.ArgumentNull(nameof(keySelector));
  185. if (elementSelector == null)
  186. throw Error.ArgumentNull(nameof(elementSelector));
  187. return Core(source, keySelector, elementSelector, comparer, cancellationToken);
  188. static async ValueTask<ILookup<TKey, TElement>> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
  189. {
  190. return await Internal.LookupWithTask<TKey, TElement>.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false);
  191. }
  192. }
  193. #if !NO_DEEP_CANCELLATION
  194. [GenerateAsyncOverload]
  195. private static ValueTask<ILookup<TKey, TElement>> ToLookupAwaitWithCancellationAsyncCore<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, CancellationToken cancellationToken = default) =>
  196. ToLookupAwaitWithCancellationAsyncCore(source, keySelector, elementSelector, comparer: null, cancellationToken);
  197. [GenerateAsyncOverload]
  198. private static ValueTask<ILookup<TKey, TElement>> ToLookupAwaitWithCancellationAsyncCore<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)
  199. {
  200. if (source == null)
  201. throw Error.ArgumentNull(nameof(source));
  202. if (keySelector == null)
  203. throw Error.ArgumentNull(nameof(keySelector));
  204. if (elementSelector == null)
  205. throw Error.ArgumentNull(nameof(elementSelector));
  206. return Core(source, keySelector, elementSelector, comparer, cancellationToken);
  207. static async ValueTask<ILookup<TKey, TElement>> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
  208. {
  209. return await Internal.LookupWithTask<TKey, TElement>.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false);
  210. }
  211. }
  212. #endif
  213. }
  214. }