ToLookup.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static Task<ILookup<TKey, TSource>> ToLookup<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  14. {
  15. if (source == null)
  16. throw Error.ArgumentNull(nameof(source));
  17. if (keySelector == null)
  18. throw Error.ArgumentNull(nameof(keySelector));
  19. return ToLookupCore(source, keySelector, x => x, EqualityComparer<TKey>.Default, CancellationToken.None);
  20. }
  21. public static Task<ILookup<TKey, TSource>> ToLookup<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
  22. {
  23. if (source == null)
  24. throw Error.ArgumentNull(nameof(source));
  25. if (keySelector == null)
  26. throw Error.ArgumentNull(nameof(keySelector));
  27. return ToLookupCore(source, keySelector, x => x, EqualityComparer<TKey>.Default, cancellationToken);
  28. }
  29. public static Task<ILookup<TKey, TSource>> ToLookup<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector)
  30. {
  31. if (source == null)
  32. throw Error.ArgumentNull(nameof(source));
  33. if (keySelector == null)
  34. throw Error.ArgumentNull(nameof(keySelector));
  35. return ToLookupCore(source, keySelector, x => Task.FromResult(x), EqualityComparer<TKey>.Default, CancellationToken.None);
  36. }
  37. public static Task<ILookup<TKey, TSource>> ToLookup<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, CancellationToken cancellationToken)
  38. {
  39. if (source == null)
  40. throw Error.ArgumentNull(nameof(source));
  41. if (keySelector == null)
  42. throw Error.ArgumentNull(nameof(keySelector));
  43. return ToLookupCore(source, keySelector, x => Task.FromResult(x), EqualityComparer<TKey>.Default, cancellationToken);
  44. }
  45. public static Task<ILookup<TKey, TSource>> ToLookup<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  46. {
  47. if (source == null)
  48. throw Error.ArgumentNull(nameof(source));
  49. if (keySelector == null)
  50. throw Error.ArgumentNull(nameof(keySelector));
  51. if (comparer == null)
  52. throw Error.ArgumentNull(nameof(comparer));
  53. return ToLookupCore(source, keySelector, x => x, comparer, CancellationToken.None);
  54. }
  55. public static Task<ILookup<TKey, TSource>> ToLookup<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  56. {
  57. if (source == null)
  58. throw Error.ArgumentNull(nameof(source));
  59. if (keySelector == null)
  60. throw Error.ArgumentNull(nameof(keySelector));
  61. if (comparer == null)
  62. throw Error.ArgumentNull(nameof(comparer));
  63. return ToLookupCore(source, keySelector, x => x, comparer, cancellationToken);
  64. }
  65. public static Task<ILookup<TKey, TSource>> ToLookup<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  66. {
  67. if (source == null)
  68. throw Error.ArgumentNull(nameof(source));
  69. if (keySelector == null)
  70. throw Error.ArgumentNull(nameof(keySelector));
  71. if (comparer == null)
  72. throw Error.ArgumentNull(nameof(comparer));
  73. return ToLookupCore(source, keySelector, x => Task.FromResult(x), comparer, CancellationToken.None);
  74. }
  75. public static Task<ILookup<TKey, TSource>> ToLookup<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  76. {
  77. if (source == null)
  78. throw Error.ArgumentNull(nameof(source));
  79. if (keySelector == null)
  80. throw Error.ArgumentNull(nameof(keySelector));
  81. if (comparer == null)
  82. throw Error.ArgumentNull(nameof(comparer));
  83. return ToLookupCore(source, keySelector, x => Task.FromResult(x), comparer, cancellationToken);
  84. }
  85. public static Task<ILookup<TKey, TElement>> ToLookup<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  86. {
  87. if (source == null)
  88. throw Error.ArgumentNull(nameof(source));
  89. if (keySelector == null)
  90. throw Error.ArgumentNull(nameof(keySelector));
  91. if (elementSelector == null)
  92. throw Error.ArgumentNull(nameof(elementSelector));
  93. return ToLookupCore(source, keySelector, elementSelector, EqualityComparer<TKey>.Default, CancellationToken.None);
  94. }
  95. public static Task<ILookup<TKey, TElement>> ToLookup<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken)
  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, EqualityComparer<TKey>.Default, cancellationToken);
  104. }
  105. public static Task<ILookup<TKey, TElement>> ToLookup<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, Func<TSource, Task<TElement>> elementSelector)
  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, EqualityComparer<TKey>.Default, CancellationToken.None);
  114. }
  115. public static Task<ILookup<TKey, TElement>> ToLookup<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, Func<TSource, Task<TElement>> elementSelector, CancellationToken cancellationToken)
  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(source, keySelector, elementSelector, EqualityComparer<TKey>.Default, cancellationToken);
  124. }
  125. public static Task<ILookup<TKey, TElement>> ToLookup<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
  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. if (comparer == null)
  134. throw Error.ArgumentNull(nameof(comparer));
  135. return ToLookupCore(source, keySelector, elementSelector, comparer, CancellationToken.None);
  136. }
  137. public static Task<ILookup<TKey, TElement>> ToLookup<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  138. {
  139. if (source == null)
  140. throw Error.ArgumentNull(nameof(source));
  141. if (keySelector == null)
  142. throw Error.ArgumentNull(nameof(keySelector));
  143. if (elementSelector == null)
  144. throw Error.ArgumentNull(nameof(elementSelector));
  145. if (comparer == null)
  146. throw Error.ArgumentNull(nameof(comparer));
  147. return ToLookupCore(source, keySelector, elementSelector, comparer, cancellationToken);
  148. }
  149. public static Task<ILookup<TKey, TElement>> ToLookup<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, Func<TSource, Task<TElement>> elementSelector, IEqualityComparer<TKey> comparer)
  150. {
  151. if (source == null)
  152. throw Error.ArgumentNull(nameof(source));
  153. if (keySelector == null)
  154. throw Error.ArgumentNull(nameof(keySelector));
  155. if (elementSelector == null)
  156. throw Error.ArgumentNull(nameof(elementSelector));
  157. if (comparer == null)
  158. throw Error.ArgumentNull(nameof(comparer));
  159. return ToLookupCore(source, keySelector, elementSelector, comparer, CancellationToken.None);
  160. }
  161. public static Task<ILookup<TKey, TElement>> ToLookup<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, Func<TSource, Task<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  162. {
  163. if (source == null)
  164. throw Error.ArgumentNull(nameof(source));
  165. if (keySelector == null)
  166. throw Error.ArgumentNull(nameof(keySelector));
  167. if (elementSelector == null)
  168. throw Error.ArgumentNull(nameof(elementSelector));
  169. if (comparer == null)
  170. throw Error.ArgumentNull(nameof(comparer));
  171. return ToLookupCore(source, keySelector, elementSelector, comparer, cancellationToken);
  172. }
  173. 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)
  174. {
  175. return await Internal.Lookup<TKey, TElement>.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false);
  176. }
  177. private static async Task<ILookup<TKey, TElement>> ToLookupCore<TSource, TKey, TElement>(IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, Func<TSource, Task<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  178. {
  179. return await Internal.LookupWithTask<TKey, TElement>.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false);
  180. }
  181. }
  182. }