ToLookup.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  12. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.tolookupasync?view=net-9.0-pp#system-linq-asyncenumerable-tolookupasync-2(system-collections-generic-iasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-iequalitycomparer((-1))-system-threading-cancellationtoken)
  13. // That one overload covers the next two methods, because it supplieds a default comparer.
  14. /// <summary>
  15. /// Creates a lookup from an async-enumerable sequence according to a specified key selector function.
  16. /// </summary>
  17. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  18. /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
  19. /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
  20. /// <param name="keySelector">A function to extract a key from each element.</param>
  21. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  22. /// <returns>An async-enumerable sequence containing a single element with a lookup mapping unique key values onto the corresponding source sequence's elements.</returns>
  23. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  24. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  25. public static ValueTask<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken = default) =>
  26. ToLookupAsync(source, keySelector, comparer: null, cancellationToken);
  27. /// <summary>
  28. /// Creates a lookup from an async-enumerable sequence according to a specified key selector function, and a comparer.
  29. /// </summary>
  30. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  31. /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
  32. /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
  33. /// <param name="keySelector">A function to extract a key from each element.</param>
  34. /// <param name="comparer">An equality comparer to compare keys.</param>
  35. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  36. /// <returns>An async-enumerable sequence containing a single element with a lookup mapping unique key values onto the corresponding source sequence's elements.</returns>
  37. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="comparer"/> is null.</exception>
  38. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  39. public static ValueTask<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken = default)
  40. {
  41. if (source == null)
  42. throw Error.ArgumentNull(nameof(source));
  43. if (keySelector == null)
  44. throw Error.ArgumentNull(nameof(keySelector));
  45. return Core(source, keySelector, comparer, cancellationToken);
  46. static async ValueTask<ILookup<TKey, TSource>> Core(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
  47. {
  48. return await Internal.Lookup<TKey, TSource>.CreateAsync(source, keySelector, comparer, cancellationToken).ConfigureAwait(false);
  49. }
  50. }
  51. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  52. /// <summary>
  53. /// Creates a lookup from an async-enumerable sequence by invoking a key-selector function on each element and awaiting the result.
  54. /// </summary>
  55. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  56. /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
  57. /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
  58. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  59. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  60. /// <returns>A ValueTask containing a lookup mapping unique key values onto the corresponding source sequence's elements.</returns>
  61. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  62. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  63. [GenerateAsyncOverload]
  64. [Obsolete("Use ToLookupAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ToLookupAwaitAsync functionality now exists as overloads of ToLookupAsync.")]
  65. private static ValueTask<ILookup<TKey, TSource>> ToLookupAwaitAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, CancellationToken cancellationToken = default) =>
  66. ToLookupAwaitAsyncCore<TSource, TKey>(source, keySelector, comparer:null, cancellationToken);
  67. /// <summary>
  68. /// Creates a lookup from an async-enumerable sequence by invoking a key-selector function on each element and awaiting the result.
  69. /// </summary>
  70. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  71. /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
  72. /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
  73. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  74. /// <param name="comparer">An equality comparer to compare keys.</param>
  75. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  76. /// <returns>A ValueTask containing a lookup mapping unique key values onto the corresponding source sequence's elements.</returns>
  77. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="comparer"/> is null.</exception>
  78. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  79. [GenerateAsyncOverload]
  80. [Obsolete("Use ToLookupAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ToLookupAwaitAsync functionality now exists as overloads of ToLookupAsync.")]
  81. private static ValueTask<ILookup<TKey, TSource>> ToLookupAwaitAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken = default)
  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<ILookup<TKey, TSource>> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
  89. {
  90. return await Internal.LookupWithTask<TKey, TSource>.CreateAsync(source, keySelector, comparer, cancellationToken).ConfigureAwait(false);
  91. }
  92. }
  93. #if !NO_DEEP_CANCELLATION
  94. [GenerateAsyncOverload]
  95. [Obsolete("Use ToLookupAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ToLookupAwaitWithCancellationAsync functionality now exists as overloads of ToLookupAsync.")]
  96. private static ValueTask<ILookup<TKey, TSource>> ToLookupAwaitWithCancellationAsyncCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, CancellationToken cancellationToken = default) =>
  97. ToLookupAwaitWithCancellationAsyncCore(source, keySelector, comparer: null, cancellationToken);
  98. [GenerateAsyncOverload]
  99. [Obsolete("Use ToLookupAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ToLookupAwaitWithCancellationAsync functionality now exists as overloads of ToLookupAsync.")]
  100. 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)
  101. {
  102. if (source == null)
  103. throw Error.ArgumentNull(nameof(source));
  104. if (keySelector == null)
  105. throw Error.ArgumentNull(nameof(keySelector));
  106. return Core(source, keySelector, comparer, cancellationToken);
  107. static async ValueTask<ILookup<TKey, TSource>> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
  108. {
  109. return await Internal.LookupWithTask<TKey, TSource>.CreateAsync(source, keySelector, comparer, cancellationToken).ConfigureAwait(false);
  110. }
  111. }
  112. #endif
  113. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  114. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.tolookupasync?view=net-9.0-pp#system-linq-asyncenumerable-tolookupasync-3(system-collections-generic-iasyncenumerable((-0))-system-func((-0-1))-system-func((-0-2))-system-collections-generic-iequalitycomparer((-1))-system-threading-cancellationtoken)
  115. // That one overload covers the next two methods, because it supplieds a default comparer.
  116. /// <summary>
  117. /// Creates a lookup from an async-enumerable sequence according to a specified key selector function, and an element selector function.
  118. /// </summary>
  119. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  120. /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
  121. /// <typeparam name="TElement">The type of the lookup value computed for each element in the source sequence.</typeparam>
  122. /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
  123. /// <param name="keySelector">A function to extract a key from each element.</param>
  124. /// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
  125. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  126. /// <returns>An async-enumerable sequence containing a single element with a lookup mapping unique key values onto the corresponding source sequence's elements.</returns>
  127. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> is null.</exception>
  128. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  129. 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) =>
  130. ToLookupAsync(source, keySelector, elementSelector, comparer: null, cancellationToken);
  131. /// <summary>
  132. /// Creates a lookup from an async-enumerable sequence according to a specified key selector function, a comparer, and an element selector function.
  133. /// </summary>
  134. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  135. /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
  136. /// <typeparam name="TElement">The type of the lookup value computed for each element in the source sequence.</typeparam>
  137. /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
  138. /// <param name="keySelector">A function to extract a key from each element.</param>
  139. /// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
  140. /// <param name="comparer">An equality comparer to compare keys.</param>
  141. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  142. /// <returns>An async-enumerable sequence containing a single element with a lookup mapping unique key values onto the corresponding source sequence's elements.</returns>
  143. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> or <paramref name="comparer"/> is null.</exception>
  144. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  145. 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)
  146. {
  147. if (source == null)
  148. throw Error.ArgumentNull(nameof(source));
  149. if (keySelector == null)
  150. throw Error.ArgumentNull(nameof(keySelector));
  151. if (elementSelector == null)
  152. throw Error.ArgumentNull(nameof(elementSelector));
  153. return Core(source, keySelector, elementSelector, comparer, cancellationToken);
  154. static async ValueTask<ILookup<TKey, TElement>> Core(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken)
  155. {
  156. return await Internal.Lookup<TKey, TElement>.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false);
  157. }
  158. }
  159. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  160. /// <summary>
  161. /// Creates a lookup from an async-enumerable sequence by invoking key and element selector functions on each source element and awaiting the results.
  162. /// </summary>
  163. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  164. /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
  165. /// <typeparam name="TElement">The type of the lookup value computed for each element in the source sequence.</typeparam>
  166. /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
  167. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  168. /// <param name="elementSelector">An asynchronous transform function to produce a result element value from each element.</param>
  169. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  170. /// <returns>An async-enumerable sequence containing a single element with a lookup mapping unique key values onto the corresponding source sequence's elements.</returns>
  171. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> is null.</exception>
  172. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  173. [GenerateAsyncOverload]
  174. [Obsolete("Use ToLookupAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ToLookupAwaitAsync functionality now exists as overloads of ToLookupAsync.")]
  175. 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) =>
  176. ToLookupAwaitAsyncCore<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer: null, cancellationToken);
  177. /// <summary>
  178. /// Creates a lookup from an async-enumerable sequence by invoking key and element selector functions on each source element and awaiting the results.
  179. /// </summary>
  180. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  181. /// <typeparam name="TKey">The type of the lookup key computed for each element in the source sequence.</typeparam>
  182. /// <typeparam name="TElement">The type of the lookup value computed for each element in the source sequence.</typeparam>
  183. /// <param name="source">An async-enumerable sequence to create a lookup for.</param>
  184. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  185. /// <param name="elementSelector">An asynchronous transform function to produce a result element value from each source element.</param>
  186. /// <param name="comparer">An equality comparer to compare keys.</param>
  187. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  188. /// <returns>A ValueTask containing a lookup mapping unique key values onto the corresponding source sequence's elements.</returns>
  189. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> or <paramref name="comparer"/> is null.</exception>
  190. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  191. [GenerateAsyncOverload]
  192. [Obsolete("Use ToLookupAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ToLookupAwaitAsync functionality now exists as overloads of ToLookupAsync.")]
  193. 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)
  194. {
  195. if (source == null)
  196. throw Error.ArgumentNull(nameof(source));
  197. if (keySelector == null)
  198. throw Error.ArgumentNull(nameof(keySelector));
  199. if (elementSelector == null)
  200. throw Error.ArgumentNull(nameof(elementSelector));
  201. return Core(source, keySelector, elementSelector, comparer, cancellationToken);
  202. 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)
  203. {
  204. return await Internal.LookupWithTask<TKey, TElement>.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false);
  205. }
  206. }
  207. #if !NO_DEEP_CANCELLATION
  208. [GenerateAsyncOverload]
  209. [Obsolete("Use ToLookupAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ToLookupAwaitWithCancellationAsync functionality now exists as overloads of ToLookupAsync.")]
  210. 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) =>
  211. ToLookupAwaitWithCancellationAsyncCore(source, keySelector, elementSelector, comparer: null, cancellationToken);
  212. [GenerateAsyncOverload]
  213. [Obsolete("Use ToLookupAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ToLookupAwaitWithCancellationAsync functionality now exists as overloads of ToLookupAsync.")]
  214. 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)
  215. {
  216. if (source == null)
  217. throw Error.ArgumentNull(nameof(source));
  218. if (keySelector == null)
  219. throw Error.ArgumentNull(nameof(keySelector));
  220. if (elementSelector == null)
  221. throw Error.ArgumentNull(nameof(elementSelector));
  222. return Core(source, keySelector, elementSelector, comparer, cancellationToken);
  223. 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)
  224. {
  225. return await Internal.LookupWithTask<TKey, TElement>.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false);
  226. }
  227. }
  228. #endif
  229. }
  230. }