1
0

ToLookup.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. public static Task<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  44. {
  45. if (source == null)
  46. throw Error.ArgumentNull(nameof(source));
  47. if (keySelector == null)
  48. throw Error.ArgumentNull(nameof(keySelector));
  49. return ToLookupCore(source, keySelector, x => x, comparer, CancellationToken.None);
  50. }
  51. public static Task<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  52. {
  53. if (source == null)
  54. throw Error.ArgumentNull(nameof(source));
  55. if (keySelector == null)
  56. throw Error.ArgumentNull(nameof(keySelector));
  57. return ToLookupCore(source, keySelector, x => x, comparer, cancellationToken);
  58. }
  59. public static Task<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  60. {
  61. if (source == null)
  62. throw Error.ArgumentNull(nameof(source));
  63. if (keySelector == null)
  64. throw Error.ArgumentNull(nameof(keySelector));
  65. return ToLookupCore(source, keySelector, x => new ValueTask<TSource>(x), comparer, CancellationToken.None);
  66. }
  67. public static Task<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  68. {
  69. if (source == null)
  70. throw Error.ArgumentNull(nameof(source));
  71. if (keySelector == null)
  72. throw Error.ArgumentNull(nameof(keySelector));
  73. return ToLookupCore(source, keySelector, x => new ValueTask<TSource>(x), comparer, cancellationToken);
  74. }
  75. public static Task<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  76. {
  77. if (source == null)
  78. throw Error.ArgumentNull(nameof(source));
  79. if (keySelector == null)
  80. throw Error.ArgumentNull(nameof(keySelector));
  81. if (elementSelector == null)
  82. throw Error.ArgumentNull(nameof(elementSelector));
  83. return ToLookupCore(source, keySelector, elementSelector, comparer: null, CancellationToken.None);
  84. }
  85. public static Task<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken)
  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, comparer: null, cancellationToken);
  94. }
  95. public static Task<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<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<TSource, TKey, TElement>(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, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<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<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer: null, cancellationToken);
  114. }
  115. 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)
  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, comparer, CancellationToken.None);
  124. }
  125. 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)
  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(source, keySelector, elementSelector, comparer, cancellationToken);
  134. }
  135. 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)
  136. {
  137. if (source == null)
  138. throw Error.ArgumentNull(nameof(source));
  139. if (keySelector == null)
  140. throw Error.ArgumentNull(nameof(keySelector));
  141. if (elementSelector == null)
  142. throw Error.ArgumentNull(nameof(elementSelector));
  143. return ToLookupCore(source, keySelector, elementSelector, comparer, CancellationToken.None);
  144. }
  145. 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)
  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 ToLookupCore(source, keySelector, elementSelector, comparer, cancellationToken);
  154. }
  155. 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)
  156. {
  157. return await Internal.Lookup<TKey, TElement>.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false);
  158. }
  159. 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)
  160. {
  161. return await Internal.LookupWithTask<TKey, TElement>.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false);
  162. }
  163. }
  164. }