ToLookup.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  12. {
  13. if (source == null)
  14. throw Error.ArgumentNull(nameof(source));
  15. if (keySelector == null)
  16. throw Error.ArgumentNull(nameof(keySelector));
  17. return ToLookupCore(source, keySelector, x => x, comparer: null, CancellationToken.None);
  18. }
  19. public static Task<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
  20. {
  21. if (source == null)
  22. throw Error.ArgumentNull(nameof(source));
  23. if (keySelector == null)
  24. throw Error.ArgumentNull(nameof(keySelector));
  25. return ToLookupCore(source, keySelector, x => x, comparer: null, cancellationToken);
  26. }
  27. public static Task<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector)
  28. {
  29. if (source == null)
  30. throw Error.ArgumentNull(nameof(source));
  31. if (keySelector == null)
  32. throw Error.ArgumentNull(nameof(keySelector));
  33. return ToLookupCore<TSource, TKey, TSource>(source, keySelector, x => new ValueTask<TSource>(x), comparer: null, CancellationToken.None);
  34. }
  35. public static Task<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, CancellationToken cancellationToken)
  36. {
  37. if (source == null)
  38. throw Error.ArgumentNull(nameof(source));
  39. if (keySelector == null)
  40. throw Error.ArgumentNull(nameof(keySelector));
  41. return ToLookupCore<TSource, TKey, TSource>(source, keySelector, x => new ValueTask<TSource>(x), comparer: null, cancellationToken);
  42. }
  43. #if !NO_DEEP_CANCELLATION
  44. public static Task<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, CancellationToken cancellationToken)
  45. {
  46. if (source == null)
  47. throw Error.ArgumentNull(nameof(source));
  48. if (keySelector == null)
  49. throw Error.ArgumentNull(nameof(keySelector));
  50. return ToLookupCore<TSource, TKey, TSource>(source, x => keySelector(x, cancellationToken), x => new ValueTask<TSource>(x), comparer: null, cancellationToken);
  51. }
  52. #endif
  53. public static Task<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  54. {
  55. if (source == null)
  56. throw Error.ArgumentNull(nameof(source));
  57. if (keySelector == null)
  58. throw Error.ArgumentNull(nameof(keySelector));
  59. return ToLookupCore(source, keySelector, x => x, comparer, CancellationToken.None);
  60. }
  61. public static Task<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  62. {
  63. if (source == null)
  64. throw Error.ArgumentNull(nameof(source));
  65. if (keySelector == null)
  66. throw Error.ArgumentNull(nameof(keySelector));
  67. return ToLookupCore(source, keySelector, x => x, comparer, cancellationToken);
  68. }
  69. public static Task<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  70. {
  71. if (source == null)
  72. throw Error.ArgumentNull(nameof(source));
  73. if (keySelector == null)
  74. throw Error.ArgumentNull(nameof(keySelector));
  75. return ToLookupCore(source, keySelector, x => new ValueTask<TSource>(x), comparer, CancellationToken.None);
  76. }
  77. public static Task<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  78. {
  79. if (source == null)
  80. throw Error.ArgumentNull(nameof(source));
  81. if (keySelector == null)
  82. throw Error.ArgumentNull(nameof(keySelector));
  83. return ToLookupCore(source, keySelector, x => new ValueTask<TSource>(x), comparer, cancellationToken);
  84. }
  85. #if !NO_DEEP_CANCELLATION
  86. public static Task<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  87. {
  88. if (source == null)
  89. throw Error.ArgumentNull(nameof(source));
  90. if (keySelector == null)
  91. throw Error.ArgumentNull(nameof(keySelector));
  92. return ToLookupCore(source, x => keySelector(x, cancellationToken), x => new ValueTask<TSource>(x), comparer, cancellationToken);
  93. }
  94. #endif
  95. public static Task<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  96. {
  97. if (source == null)
  98. throw Error.ArgumentNull(nameof(source));
  99. if (keySelector == null)
  100. throw Error.ArgumentNull(nameof(keySelector));
  101. if (elementSelector == null)
  102. throw Error.ArgumentNull(nameof(elementSelector));
  103. return ToLookupCore(source, keySelector, elementSelector, comparer: null, CancellationToken.None);
  104. }
  105. public static Task<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken)
  106. {
  107. if (source == null)
  108. throw Error.ArgumentNull(nameof(source));
  109. if (keySelector == null)
  110. throw Error.ArgumentNull(nameof(keySelector));
  111. if (elementSelector == null)
  112. throw Error.ArgumentNull(nameof(elementSelector));
  113. return ToLookupCore(source, keySelector, elementSelector, comparer: null, cancellationToken);
  114. }
  115. public static Task<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector)
  116. {
  117. if (source == null)
  118. throw Error.ArgumentNull(nameof(source));
  119. if (keySelector == null)
  120. throw Error.ArgumentNull(nameof(keySelector));
  121. if (elementSelector == null)
  122. throw Error.ArgumentNull(nameof(elementSelector));
  123. return ToLookupCore<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer: null, CancellationToken.None);
  124. }
  125. public static Task<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, CancellationToken cancellationToken)
  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 ToLookupCore<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer: null, cancellationToken);
  134. }
  135. #if !NO_DEEP_CANCELLATION
  136. public static Task<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, CancellationToken cancellationToken)
  137. {
  138. if (source == null)
  139. throw Error.ArgumentNull(nameof(source));
  140. if (keySelector == null)
  141. throw Error.ArgumentNull(nameof(keySelector));
  142. if (elementSelector == null)
  143. throw Error.ArgumentNull(nameof(elementSelector));
  144. return ToLookupCore<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer: null, cancellationToken);
  145. }
  146. #endif
  147. public static Task<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
  148. {
  149. if (source == null)
  150. throw Error.ArgumentNull(nameof(source));
  151. if (keySelector == null)
  152. throw Error.ArgumentNull(nameof(keySelector));
  153. if (elementSelector == null)
  154. throw Error.ArgumentNull(nameof(elementSelector));
  155. return ToLookupCore(source, keySelector, elementSelector, comparer, CancellationToken.None);
  156. }
  157. public static Task<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  158. {
  159. if (source == null)
  160. throw Error.ArgumentNull(nameof(source));
  161. if (keySelector == null)
  162. throw Error.ArgumentNull(nameof(keySelector));
  163. if (elementSelector == null)
  164. throw Error.ArgumentNull(nameof(elementSelector));
  165. return ToLookupCore(source, keySelector, elementSelector, comparer, cancellationToken);
  166. }
  167. public static Task<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer)
  168. {
  169. if (source == null)
  170. throw Error.ArgumentNull(nameof(source));
  171. if (keySelector == null)
  172. throw Error.ArgumentNull(nameof(keySelector));
  173. if (elementSelector == null)
  174. throw Error.ArgumentNull(nameof(elementSelector));
  175. return ToLookupCore(source, keySelector, elementSelector, comparer, CancellationToken.None);
  176. }
  177. public static Task<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  178. {
  179. if (source == null)
  180. throw Error.ArgumentNull(nameof(source));
  181. if (keySelector == null)
  182. throw Error.ArgumentNull(nameof(keySelector));
  183. if (elementSelector == null)
  184. throw Error.ArgumentNull(nameof(elementSelector));
  185. return ToLookupCore(source, keySelector, elementSelector, comparer, cancellationToken);
  186. }
  187. #if !NO_DEEP_CANCELLATION
  188. public static Task<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  189. {
  190. if (source == null)
  191. throw Error.ArgumentNull(nameof(source));
  192. if (keySelector == null)
  193. throw Error.ArgumentNull(nameof(keySelector));
  194. if (elementSelector == null)
  195. throw Error.ArgumentNull(nameof(elementSelector));
  196. return ToLookupCore(source, keySelector, elementSelector, comparer, cancellationToken);
  197. }
  198. #endif
  199. private static async Task<ILookup<TKey, TElement>> ToLookupCore<TSource, TKey, TElement>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  200. {
  201. return await Internal.Lookup<TKey, TElement>.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false);
  202. }
  203. private static async Task<ILookup<TKey, TElement>> ToLookupCore<TSource, TKey, TElement>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  204. {
  205. return await Internal.LookupWithTask<TKey, TElement>.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false);
  206. }
  207. #if !NO_DEEP_CANCELLATION
  208. private static async Task<ILookup<TKey, TElement>> ToLookupCore<TSource, TKey, TElement>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  209. {
  210. return await Internal.LookupWithTask<TKey, TElement>.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false);
  211. }
  212. #endif
  213. }
  214. }