GroupBy.cs 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  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. /// Groups the elements of 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 grouping key computed for each element in the source sequence.</typeparam>
  16. /// <param name="source">An async-enumerable sequence whose elements to group.</param>
  17. /// <param name="keySelector">A function to extract the key for each element.</param>
  18. /// <returns>A sequence of async-enumerable groups, each of which corresponds to a unique key value, containing all elements that share that same key value.</returns>
  19. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  20. public static IAsyncEnumerable<IAsyncGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector) =>
  21. new GroupedAsyncEnumerable<TSource, TKey>(source, keySelector, comparer: null);
  22. /// <summary>
  23. /// Groups the elements of an async-enumerable sequence according to a specified key selector function and comparer.
  24. /// </summary>
  25. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  26. /// <typeparam name="TKey">The type of the grouping key computed for each element in the source sequence.</typeparam>
  27. /// <param name="source">An async-enumerable sequence whose elements to group.</param>
  28. /// <param name="keySelector">A function to extract the key for each element.</param>
  29. /// <param name="comparer">An equality comparer to compare keys with.</param>
  30. /// <returns>A sequence of async-enumerable groups, each of which corresponds to a unique key value, containing all elements that share that same key value.</returns>
  31. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="comparer"/> is null.</exception>
  32. public static IAsyncEnumerable<IAsyncGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer) =>
  33. new GroupedAsyncEnumerable<TSource, TKey>(source, keySelector, comparer);
  34. /// <summary>
  35. /// Groups the elements of an async-enumerable sequence according to a specified key selector function.
  36. /// </summary>
  37. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  38. /// <typeparam name="TKey">The type of the grouping key computed for each element in the source sequence.</typeparam>
  39. /// <param name="source">An async-enumerable sequence whose elements to group.</param>
  40. /// <param name="keySelector">An asynchronous function to extract the key for each element.</param>
  41. /// <returns>A sequence of async-enumerable groups, each of which corresponds to a unique key value, containing all elements that share that same key value.</returns>
  42. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is <see langword="null"/>.</exception>
  43. [GenerateAsyncOverload]
  44. private static IAsyncEnumerable<IAsyncGrouping<TKey, TSource>> GroupByAwaitCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector) =>
  45. new GroupedAsyncEnumerableWithTask<TSource, TKey>(source, keySelector, comparer: null);
  46. /// <summary>
  47. /// Groups the elements of an async-enumerable sequence according to a specified key selector function and comparer.
  48. /// </summary>
  49. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  50. /// <typeparam name="TKey">The type of the grouping key computed for each element in the source sequence.</typeparam>
  51. /// <param name="source">An async-enumerable sequence whose elements to group.</param>
  52. /// <param name="keySelector">An asynchronous function to extract the key for each element.</param>
  53. /// <param name="comparer">An equality comparer to compare keys with.</param>
  54. /// <returns>A sequence of async-enumerable groups, each of which corresponds to a unique key value, containing all elements that share that same key value.</returns>
  55. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="comparer"/> is <see langword="null"/>.</exception>
  56. [GenerateAsyncOverload]
  57. private static IAsyncEnumerable<IAsyncGrouping<TKey, TSource>> GroupByAwaitCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer) =>
  58. new GroupedAsyncEnumerableWithTask<TSource, TKey>(source, keySelector, comparer);
  59. #if !NO_DEEP_CANCELLATION
  60. [GenerateAsyncOverload]
  61. private static IAsyncEnumerable<IAsyncGrouping<TKey, TSource>> GroupByAwaitWithCancellationCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector) =>
  62. new GroupedAsyncEnumerableWithTaskAndCancellation<TSource, TKey>(source, keySelector, comparer: null);
  63. [GenerateAsyncOverload]
  64. private static IAsyncEnumerable<IAsyncGrouping<TKey, TSource>> GroupByAwaitWithCancellationCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer) =>
  65. new GroupedAsyncEnumerableWithTaskAndCancellation<TSource, TKey>(source, keySelector, comparer);
  66. #endif
  67. /// <summary>
  68. /// Groups the elements of an async-enumerable sequence and selects the resulting elements by using a specified function.
  69. /// </summary>
  70. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  71. /// <typeparam name="TKey">The type of the grouping key computed for each element in the source sequence.</typeparam>
  72. /// <typeparam name="TElement">The type of the elements within the groups computed for each element in the source sequence.</typeparam>
  73. /// <param name="source">An async-enumerable sequence whose elements to group.</param>
  74. /// <param name="keySelector">A function to extract the key for each element.</param>
  75. /// <param name="elementSelector">A function to map each source element to an element in an async-enumerable group.</param>
  76. /// <returns>A sequence of async-enumerable groups, each of which corresponds to a unique key value, containing all elements that share that same key value.</returns>
  77. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> is null.</exception>
  78. public static IAsyncEnumerable<IAsyncGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector) =>
  79. new GroupedAsyncEnumerable<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer: null);
  80. /// <summary>
  81. /// Groups the elements of an async-enumerable sequence according to a specified key selector function and comparer and selects the resulting elements by using a specified function.
  82. /// </summary>
  83. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  84. /// <typeparam name="TKey">The type of the grouping key computed for each element in the source sequence.</typeparam>
  85. /// <typeparam name="TElement">The type of the elements within the groups computed for each element in the source sequence.</typeparam>
  86. /// <param name="source">An async-enumerable sequence whose elements to group.</param>
  87. /// <param name="keySelector">A function to extract the key for each element.</param>
  88. /// <param name="elementSelector">A function to map each source element to an element in an async-enumerable group.</param>
  89. /// <param name="comparer">An equality comparer to compare keys with.</param>
  90. /// <returns>A sequence of async-enumerable groups, each of which corresponds to a unique key value, containing all elements that share that same key value.</returns>
  91. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> or <paramref name="comparer"/> is null.</exception>
  92. public static IAsyncEnumerable<IAsyncGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer) =>
  93. new GroupedAsyncEnumerable<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer);
  94. /// <summary>
  95. /// Groups the elements of an async-enumerable sequence and selects the resulting elements by using a specified function.
  96. /// </summary>
  97. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  98. /// <typeparam name="TKey">The type of the grouping key computed for each element in the source sequence.</typeparam>
  99. /// <typeparam name="TElement">The type of the elements within the groups computed for each element in the source sequence.</typeparam>
  100. /// <param name="source">An async-enumerable sequence whose elements to group.</param>
  101. /// <param name="keySelector">An asynchronous function to extract the key for each element.</param>
  102. /// <param name="elementSelector">An asynchronous function to map each source element to an element in an async-enumerable group.</param>
  103. /// <returns>A sequence of async-enumerable groups, each of which corresponds to a unique key value, containing all elements that share that same key value.</returns>
  104. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> is <see langword="null"/>.</exception>
  105. [GenerateAsyncOverload]
  106. private static IAsyncEnumerable<IAsyncGrouping<TKey, TElement>> GroupByAwaitCore<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector) =>
  107. new GroupedAsyncEnumerableWithTask<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer: null);
  108. /// <summary>
  109. /// Groups the elements of an async-enumerable sequence and selects the resulting elements by using a specified function.
  110. /// </summary>
  111. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  112. /// <typeparam name="TKey">The type of the grouping key computed for each element in the source sequence.</typeparam>
  113. /// <typeparam name="TElement">The type of the elements within the groups computed for each element in the source sequence.</typeparam>
  114. /// <param name="source">An async-enumerable sequence whose elements to group.</param>
  115. /// <param name="keySelector">An asynchronous function to extract the key for each element.</param>
  116. /// <param name="elementSelector">An asynchronous function to map each source element to an element in an async-enumerable group.</param>
  117. /// <param name="comparer">An equality comparer to use to compare keys.</param>
  118. /// <returns>A sequence of async-enumerable groups, each of which corresponds to a unique key value, containing all elements that share that same key value.</returns>
  119. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> or <paramref name="comparer"/> is <see langword="null"/>.</exception>
  120. [GenerateAsyncOverload]
  121. private static IAsyncEnumerable<IAsyncGrouping<TKey, TElement>> GroupByAwaitCore<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey>? comparer) =>
  122. new GroupedAsyncEnumerableWithTask<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer);
  123. #if !NO_DEEP_CANCELLATION
  124. [GenerateAsyncOverload]
  125. private static IAsyncEnumerable<IAsyncGrouping<TKey, TElement>> GroupByAwaitWithCancellationCore<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector) =>
  126. new GroupedAsyncEnumerableWithTaskAndCancellation<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer: null);
  127. [GenerateAsyncOverload]
  128. private static IAsyncEnumerable<IAsyncGrouping<TKey, TElement>> GroupByAwaitWithCancellationCore<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey>? comparer) =>
  129. new GroupedAsyncEnumerableWithTaskAndCancellation<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer);
  130. #endif
  131. public static IAsyncEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IAsyncEnumerable<TSource>, TResult> resultSelector) =>
  132. new GroupedResultAsyncEnumerable<TSource, TKey, TResult>(source, keySelector, resultSelector, comparer: null);
  133. public static IAsyncEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IAsyncEnumerable<TSource>, TResult> resultSelector, IEqualityComparer<TKey>? comparer) =>
  134. new GroupedResultAsyncEnumerable<TSource, TKey, TResult>(source, keySelector, resultSelector, comparer);
  135. /// <summary>
  136. /// Groups the elements of an async-enumerable sequence according to a specified key selector function, and then applies a result selector function to each group.
  137. /// </summary>
  138. /// <typeparam name="TSource">Type of element in the source sequence.</typeparam>
  139. /// <typeparam name="TKey">Type of the grouping key computed for each element in the source sequence.</typeparam>
  140. /// <typeparam name="TResult">The result type returned by the result selector function.</typeparam>
  141. /// <param name="source">An async-enumerable sequence whose elements to group.</param>
  142. /// <param name="keySelector">An asynchronous function to extract the key for each element.</param>
  143. /// <param name="resultSelector">An asynchronous function to transform each group into the result type.</param>
  144. /// <returns>An async-enumerable sequence of results obtained by invoking and awaiting the result-selector function on each group.</returns>
  145. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="resultSelector"/> is <see langword="null"/>.</exception>
  146. [GenerateAsyncOverload]
  147. private static IAsyncEnumerable<TResult> GroupByAwaitCore<TSource, TKey, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TKey, IAsyncEnumerable<TSource>, ValueTask<TResult>> resultSelector) =>
  148. new GroupedResultAsyncEnumerableWithTask<TSource, TKey, TResult>(source, keySelector, resultSelector, comparer: null);
  149. /// <summary>
  150. /// Groups the elements of an async-enumerable sequence according to a specified key selector function, and then applies a result selector function to each group.
  151. /// </summary>
  152. /// <typeparam name="TSource">Type of element in the source sequence.</typeparam>
  153. /// <typeparam name="TKey">Type of the grouping key computed for each element in the source sequence.</typeparam>
  154. /// <typeparam name="TResult">The result type returned by the result selector function.</typeparam>
  155. /// <param name="source">An async-enumerable sequence whose elements to group.</param>
  156. /// <param name="keySelector">An asynchronous function to extract the key for each element.</param>
  157. /// <param name="resultSelector">An asynchronous function to transform each group into the result type.</param>
  158. /// <param name="comparer">An equality comparer to use to compare keys.</param>
  159. /// <returns>An async-enumerable sequence of results obtained by invoking and awaiting the result-selector function on each group.</returns>
  160. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="resultSelector"/> or <paramref name="comparer"/> is <see langword="null"/>.</exception>
  161. [GenerateAsyncOverload]
  162. private static IAsyncEnumerable<TResult> GroupByAwaitCore<TSource, TKey, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TKey, IAsyncEnumerable<TSource>, ValueTask<TResult>> resultSelector, IEqualityComparer<TKey>? comparer) =>
  163. new GroupedResultAsyncEnumerableWithTask<TSource, TKey, TResult>(source, keySelector, resultSelector, comparer);
  164. #if !NO_DEEP_CANCELLATION
  165. [GenerateAsyncOverload]
  166. private static IAsyncEnumerable<TResult> GroupByAwaitWithCancellationCore<TSource, TKey, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TKey, IAsyncEnumerable<TSource>, CancellationToken, ValueTask<TResult>> resultSelector) =>
  167. new GroupedResultAsyncEnumerableWithTaskAndCancellation<TSource, TKey, TResult>(source, keySelector, resultSelector, comparer: null);
  168. [GenerateAsyncOverload]
  169. private static IAsyncEnumerable<TResult> GroupByAwaitWithCancellationCore<TSource, TKey, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TKey, IAsyncEnumerable<TSource>, CancellationToken, ValueTask<TResult>> resultSelector, IEqualityComparer<TKey>? comparer) =>
  170. new GroupedResultAsyncEnumerableWithTaskAndCancellation<TSource, TKey, TResult>(source, keySelector, resultSelector, comparer);
  171. #endif
  172. public static IAsyncEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IAsyncEnumerable<TElement>, TResult> resultSelector) =>
  173. new GroupedResultAsyncEnumerable<TSource, TKey, TElement, TResult>(source, keySelector, elementSelector, resultSelector, comparer: null);
  174. public static IAsyncEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IAsyncEnumerable<TElement>, TResult> resultSelector, IEqualityComparer<TKey>? comparer) =>
  175. new GroupedResultAsyncEnumerable<TSource, TKey, TElement, TResult>(source, keySelector, elementSelector, resultSelector, comparer);
  176. /// <summary>
  177. /// Groups the elements of an async-enumerable sequence according to a specified key-selector function, applies an element selector to each element of each group, then applies a result selector to each transformed group.
  178. /// </summary>
  179. /// <typeparam name="TSource">The type of element in the source sequence.</typeparam>
  180. /// <typeparam name="TKey">The type of the grouping key computed for each element in the source sequence.</typeparam>
  181. /// <typeparam name="TElement">The type of element computed by the element selector.</typeparam>
  182. /// <typeparam name="TResult">The type of the final result, computed by applying the result selector to each transformed group of elements.</typeparam>
  183. /// <param name="source">An async-enumerable sequence whose elements to group.</param>
  184. /// <param name="keySelector">An asynchronous function to extract the key for each element.</param>
  185. /// <param name="elementSelector">An asynchronous function to apply to each element of each group. </param>
  186. /// <param name="resultSelector">An asynchronous function to transform each group into the result type.</param>
  187. /// <returns>An async-enumerable sequence of results obtained by invoking the result selector function on each group and awaiting the result.</returns>
  188. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> or <paramref name="resultSelector"/> is <see langword="null"/>.</exception>
  189. [GenerateAsyncOverload]
  190. private static IAsyncEnumerable<TResult> GroupByAwaitCore<TSource, TKey, TElement, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, Func<TKey, IAsyncEnumerable<TElement>, ValueTask<TResult>> resultSelector) =>
  191. new GroupedResultAsyncEnumerableWithTask<TSource, TKey, TElement, TResult>(source, keySelector, elementSelector, resultSelector, comparer: null);
  192. /// <summary>
  193. /// Groups the elements of an async-enumerable sequence according to a specified key-selector function, applies an element selector to each element of each group, then applies a result selector to each transformed group.
  194. /// </summary>
  195. /// <typeparam name="TSource">The type of element in the source sequence.</typeparam>
  196. /// <typeparam name="TKey">The type of the grouping key computed for each element in the source sequence.</typeparam>
  197. /// <typeparam name="TElement">The type of element computed by the element selector.</typeparam>
  198. /// <typeparam name="TResult">The type of the final result, computed by applying the result selector to each transformed group of elements.</typeparam>
  199. /// <param name="source">An async-enumerable sequence whose elements to group.</param>
  200. /// <param name="keySelector">An asynchronous function to extract the key for each element.</param>
  201. /// <param name="elementSelector">An asynchronous function to apply to each element of each group. </param>
  202. /// <param name="resultSelector">An asynchronous function to transform each group into the result type.</param>
  203. /// <param name="comparer">An equality comparer to use to compare keys.</param>
  204. /// <returns>An async-enumerable sequence of results obtained by invoking the result selector function on each group and awaiting the result.</returns>
  205. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="elementSelector"/> or <paramref name="resultSelector"/> or <paramref name="comparer"/> is <see langword="null"/>.</exception>
  206. [GenerateAsyncOverload]
  207. private static IAsyncEnumerable<TResult> GroupByAwaitCore<TSource, TKey, TElement, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, Func<TKey, IAsyncEnumerable<TElement>, ValueTask<TResult>> resultSelector, IEqualityComparer<TKey>? comparer) =>
  208. new GroupedResultAsyncEnumerableWithTask<TSource, TKey, TElement, TResult>(source, keySelector, elementSelector, resultSelector, comparer);
  209. #if !NO_DEEP_CANCELLATION
  210. [GenerateAsyncOverload]
  211. private static IAsyncEnumerable<TResult> GroupByAwaitWithCancellationCore<TSource, TKey, TElement, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, Func<TKey, IAsyncEnumerable<TElement>, CancellationToken, ValueTask<TResult>> resultSelector) =>
  212. new GroupedResultAsyncEnumerableWithTaskAndCancellation<TSource, TKey, TElement, TResult>(source, keySelector, elementSelector, resultSelector, comparer: null);
  213. [GenerateAsyncOverload]
  214. private static IAsyncEnumerable<TResult> GroupByAwaitWithCancellationCore<TSource, TKey, TElement, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, Func<TKey, IAsyncEnumerable<TElement>, CancellationToken, ValueTask<TResult>> resultSelector, IEqualityComparer<TKey>? comparer) =>
  215. new GroupedResultAsyncEnumerableWithTaskAndCancellation<TSource, TKey, TElement, TResult>(source, keySelector, elementSelector, resultSelector, comparer);
  216. #endif
  217. private sealed class GroupedResultAsyncEnumerable<TSource, TKey, TResult> : AsyncIterator<TResult>, IAsyncIListProvider<TResult>
  218. {
  219. private readonly IAsyncEnumerable<TSource> _source;
  220. private readonly Func<TSource, TKey> _keySelector;
  221. private readonly Func<TKey, IAsyncEnumerable<TSource>, TResult> _resultSelector;
  222. private readonly IEqualityComparer<TKey>? _comparer;
  223. private Internal.Lookup<TKey, TSource>? _lookup;
  224. private IEnumerator<TResult>? _enumerator;
  225. public GroupedResultAsyncEnumerable(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IAsyncEnumerable<TSource>, TResult> resultSelector, IEqualityComparer<TKey>? comparer)
  226. {
  227. _source = source ?? throw Error.ArgumentNull(nameof(source));
  228. _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector));
  229. _resultSelector = resultSelector ?? throw Error.ArgumentNull(nameof(resultSelector));
  230. _comparer = comparer;
  231. }
  232. public override AsyncIteratorBase<TResult> Clone()
  233. {
  234. return new GroupedResultAsyncEnumerable<TSource, TKey, TResult>(_source, _keySelector, _resultSelector, _comparer);
  235. }
  236. public override async ValueTask DisposeAsync()
  237. {
  238. if (_enumerator != null)
  239. {
  240. _enumerator.Dispose();
  241. _enumerator = null;
  242. _lookup = null;
  243. }
  244. await base.DisposeAsync().ConfigureAwait(false);
  245. }
  246. protected override async ValueTask<bool> MoveNextCore()
  247. {
  248. switch (_state)
  249. {
  250. case AsyncIteratorState.Allocated:
  251. _lookup = await Internal.Lookup<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, _cancellationToken).ConfigureAwait(false);
  252. _enumerator = _lookup.ApplyResultSelector(_resultSelector).GetEnumerator();
  253. _state = AsyncIteratorState.Iterating;
  254. goto case AsyncIteratorState.Iterating;
  255. case AsyncIteratorState.Iterating:
  256. if (_enumerator!.MoveNext())
  257. {
  258. _current = _enumerator.Current;
  259. return true;
  260. }
  261. await DisposeAsync().ConfigureAwait(false);
  262. break;
  263. }
  264. return false;
  265. }
  266. public async ValueTask<TResult[]> ToArrayAsync(CancellationToken cancellationToken)
  267. {
  268. var l = await Internal.Lookup<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  269. return l.ToArray(_resultSelector);
  270. }
  271. public async ValueTask<List<TResult>> ToListAsync(CancellationToken cancellationToken)
  272. {
  273. var l = await Internal.Lookup<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  274. return l.ToList(_resultSelector);
  275. }
  276. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  277. {
  278. if (onlyIfCheap)
  279. {
  280. return new ValueTask<int>(-1);
  281. }
  282. return Core();
  283. async ValueTask<int> Core()
  284. {
  285. var l = await Internal.Lookup<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  286. return l.Count;
  287. }
  288. }
  289. }
  290. private sealed class GroupedResultAsyncEnumerableWithTask<TSource, TKey, TResult> : AsyncIterator<TResult>, IAsyncIListProvider<TResult>
  291. {
  292. private readonly IAsyncEnumerable<TSource> _source;
  293. private readonly Func<TSource, ValueTask<TKey>> _keySelector;
  294. private readonly Func<TKey, IAsyncEnumerable<TSource>, ValueTask<TResult>> _resultSelector;
  295. private readonly IEqualityComparer<TKey>? _comparer;
  296. private Internal.LookupWithTask<TKey, TSource>? _lookup;
  297. private IAsyncEnumerator<TResult>? _enumerator;
  298. public GroupedResultAsyncEnumerableWithTask(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TKey, IAsyncEnumerable<TSource>, ValueTask<TResult>> resultSelector, IEqualityComparer<TKey>? comparer)
  299. {
  300. _source = source ?? throw Error.ArgumentNull(nameof(source));
  301. _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector));
  302. _resultSelector = resultSelector ?? throw Error.ArgumentNull(nameof(resultSelector));
  303. _comparer = comparer;
  304. }
  305. public override AsyncIteratorBase<TResult> Clone()
  306. {
  307. return new GroupedResultAsyncEnumerableWithTask<TSource, TKey, TResult>(_source, _keySelector, _resultSelector, _comparer);
  308. }
  309. public override async ValueTask DisposeAsync()
  310. {
  311. if (_enumerator != null)
  312. {
  313. await _enumerator.DisposeAsync().ConfigureAwait(false);
  314. _enumerator = null;
  315. _lookup = null;
  316. }
  317. await base.DisposeAsync().ConfigureAwait(false);
  318. }
  319. protected override async ValueTask<bool> MoveNextCore()
  320. {
  321. switch (_state)
  322. {
  323. case AsyncIteratorState.Allocated:
  324. _lookup = await Internal.LookupWithTask<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, _cancellationToken).ConfigureAwait(false);
  325. _enumerator = _lookup.SelectAwaitCore(async g => await _resultSelector(g.Key, g).ConfigureAwait(false)).GetAsyncEnumerator(_cancellationToken); // REVIEW: Introduce another ApplyResultSelector?
  326. _state = AsyncIteratorState.Iterating;
  327. goto case AsyncIteratorState.Iterating;
  328. case AsyncIteratorState.Iterating:
  329. if (await _enumerator!.MoveNextAsync().ConfigureAwait(false))
  330. {
  331. _current = _enumerator.Current;
  332. return true;
  333. }
  334. await DisposeAsync().ConfigureAwait(false);
  335. break;
  336. }
  337. return false;
  338. }
  339. public async ValueTask<TResult[]> ToArrayAsync(CancellationToken cancellationToken)
  340. {
  341. var l = await Internal.LookupWithTask<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  342. return await l.ToArray(_resultSelector).ConfigureAwait(false);
  343. }
  344. public async ValueTask<List<TResult>> ToListAsync(CancellationToken cancellationToken)
  345. {
  346. var l = await Internal.LookupWithTask<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  347. return await l.ToList(_resultSelector).ConfigureAwait(false);
  348. }
  349. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  350. {
  351. if (onlyIfCheap)
  352. {
  353. return new ValueTask<int>(-1);
  354. }
  355. return Core();
  356. async ValueTask<int> Core()
  357. {
  358. var l = await Internal.LookupWithTask<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  359. return l.Count;
  360. }
  361. }
  362. }
  363. #if !NO_DEEP_CANCELLATION
  364. private sealed class GroupedResultAsyncEnumerableWithTaskAndCancellation<TSource, TKey, TResult> : AsyncIterator<TResult>, IAsyncIListProvider<TResult>
  365. {
  366. private readonly IAsyncEnumerable<TSource> _source;
  367. private readonly Func<TSource, CancellationToken, ValueTask<TKey>> _keySelector;
  368. private readonly Func<TKey, IAsyncEnumerable<TSource>, CancellationToken, ValueTask<TResult>> _resultSelector;
  369. private readonly IEqualityComparer<TKey>? _comparer;
  370. private Internal.LookupWithTask<TKey, TSource>? _lookup;
  371. private IAsyncEnumerator<TResult>? _enumerator;
  372. public GroupedResultAsyncEnumerableWithTaskAndCancellation(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TKey, IAsyncEnumerable<TSource>, CancellationToken, ValueTask<TResult>> resultSelector, IEqualityComparer<TKey>? comparer)
  373. {
  374. _source = source ?? throw Error.ArgumentNull(nameof(source));
  375. _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector));
  376. _resultSelector = resultSelector ?? throw Error.ArgumentNull(nameof(resultSelector));
  377. _comparer = comparer;
  378. }
  379. public override AsyncIteratorBase<TResult> Clone()
  380. {
  381. return new GroupedResultAsyncEnumerableWithTaskAndCancellation<TSource, TKey, TResult>(_source, _keySelector, _resultSelector, _comparer);
  382. }
  383. public override async ValueTask DisposeAsync()
  384. {
  385. if (_enumerator != null)
  386. {
  387. await _enumerator.DisposeAsync().ConfigureAwait(false);
  388. _enumerator = null;
  389. _lookup = null;
  390. }
  391. await base.DisposeAsync().ConfigureAwait(false);
  392. }
  393. protected override async ValueTask<bool> MoveNextCore()
  394. {
  395. switch (_state)
  396. {
  397. case AsyncIteratorState.Allocated:
  398. _lookup = await Internal.LookupWithTask<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, _cancellationToken).ConfigureAwait(false);
  399. _enumerator = _lookup.SelectAwaitCore(async g => await _resultSelector(g.Key, g, _cancellationToken).ConfigureAwait(false)).GetAsyncEnumerator(_cancellationToken); // REVIEW: Introduce another ApplyResultSelector?
  400. _state = AsyncIteratorState.Iterating;
  401. goto case AsyncIteratorState.Iterating;
  402. case AsyncIteratorState.Iterating:
  403. if (await _enumerator!.MoveNextAsync().ConfigureAwait(false))
  404. {
  405. _current = _enumerator.Current;
  406. return true;
  407. }
  408. await DisposeAsync().ConfigureAwait(false);
  409. break;
  410. }
  411. return false;
  412. }
  413. public async ValueTask<TResult[]> ToArrayAsync(CancellationToken cancellationToken)
  414. {
  415. var l = await Internal.LookupWithTask<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  416. return await l.ToArray(_resultSelector, cancellationToken).ConfigureAwait(false);
  417. }
  418. public async ValueTask<List<TResult>> ToListAsync(CancellationToken cancellationToken)
  419. {
  420. var l = await Internal.LookupWithTask<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  421. return await l.ToList(_resultSelector, cancellationToken).ConfigureAwait(false);
  422. }
  423. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  424. {
  425. if (onlyIfCheap)
  426. {
  427. return new ValueTask<int>(-1);
  428. }
  429. return Core();
  430. async ValueTask<int> Core()
  431. {
  432. var l = await Internal.LookupWithTask<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  433. return l.Count;
  434. }
  435. }
  436. }
  437. #endif
  438. private sealed class GroupedResultAsyncEnumerable<TSource, TKey, TElement, TResult> : AsyncIterator<TResult>, IAsyncIListProvider<TResult>
  439. {
  440. private readonly IAsyncEnumerable<TSource> _source;
  441. private readonly Func<TSource, TKey> _keySelector;
  442. private readonly Func<TSource, TElement> _elementSelector;
  443. private readonly Func<TKey, IAsyncEnumerable<TElement>, TResult> _resultSelector;
  444. private readonly IEqualityComparer<TKey>? _comparer;
  445. private Internal.Lookup<TKey, TElement>? _lookup;
  446. private IEnumerator<TResult>? _enumerator;
  447. public GroupedResultAsyncEnumerable(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IAsyncEnumerable<TElement>, TResult> resultSelector, IEqualityComparer<TKey>? comparer)
  448. {
  449. _source = source ?? throw Error.ArgumentNull(nameof(source));
  450. _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector));
  451. _elementSelector = elementSelector ?? throw Error.ArgumentNull(nameof(elementSelector));
  452. _resultSelector = resultSelector ?? throw Error.ArgumentNull(nameof(resultSelector));
  453. _comparer = comparer;
  454. }
  455. public override AsyncIteratorBase<TResult> Clone()
  456. {
  457. return new GroupedResultAsyncEnumerable<TSource, TKey, TElement, TResult>(_source, _keySelector, _elementSelector, _resultSelector, _comparer);
  458. }
  459. public override async ValueTask DisposeAsync()
  460. {
  461. if (_enumerator != null)
  462. {
  463. _enumerator.Dispose();
  464. _enumerator = null;
  465. _lookup = null;
  466. }
  467. await base.DisposeAsync().ConfigureAwait(false);
  468. }
  469. protected override async ValueTask<bool> MoveNextCore()
  470. {
  471. switch (_state)
  472. {
  473. case AsyncIteratorState.Allocated:
  474. _lookup = await Internal.Lookup<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, _cancellationToken).ConfigureAwait(false);
  475. _enumerator = _lookup.ApplyResultSelector(_resultSelector).GetEnumerator();
  476. _state = AsyncIteratorState.Iterating;
  477. goto case AsyncIteratorState.Iterating;
  478. case AsyncIteratorState.Iterating:
  479. if (_enumerator!.MoveNext())
  480. {
  481. _current = _enumerator.Current;
  482. return true;
  483. }
  484. await DisposeAsync().ConfigureAwait(false);
  485. break;
  486. }
  487. return false;
  488. }
  489. public async ValueTask<TResult[]> ToArrayAsync(CancellationToken cancellationToken)
  490. {
  491. var l = await Internal.Lookup<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  492. return l.ToArray(_resultSelector);
  493. }
  494. public async ValueTask<List<TResult>> ToListAsync(CancellationToken cancellationToken)
  495. {
  496. var l = await Internal.Lookup<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  497. return l.ToList(_resultSelector);
  498. }
  499. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  500. {
  501. if (onlyIfCheap)
  502. {
  503. return new ValueTask<int>(-1);
  504. }
  505. return Core();
  506. async ValueTask<int> Core()
  507. {
  508. var l = await Internal.Lookup<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  509. return l.Count;
  510. }
  511. }
  512. }
  513. private sealed class GroupedResultAsyncEnumerableWithTask<TSource, TKey, TElement, TResult> : AsyncIterator<TResult>, IAsyncIListProvider<TResult>
  514. {
  515. private readonly IAsyncEnumerable<TSource> _source;
  516. private readonly Func<TSource, ValueTask<TKey>> _keySelector;
  517. private readonly Func<TSource, ValueTask<TElement>> _elementSelector;
  518. private readonly Func<TKey, IAsyncEnumerable<TElement>, ValueTask<TResult>> _resultSelector;
  519. private readonly IEqualityComparer<TKey>? _comparer;
  520. private Internal.LookupWithTask<TKey, TElement>? _lookup;
  521. private IAsyncEnumerator<TResult>? _enumerator;
  522. public GroupedResultAsyncEnumerableWithTask(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, Func<TKey, IAsyncEnumerable<TElement>, ValueTask<TResult>> resultSelector, IEqualityComparer<TKey>? comparer)
  523. {
  524. _source = source ?? throw Error.ArgumentNull(nameof(source));
  525. _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector));
  526. _elementSelector = elementSelector ?? throw Error.ArgumentNull(nameof(elementSelector));
  527. _resultSelector = resultSelector ?? throw Error.ArgumentNull(nameof(resultSelector));
  528. _comparer = comparer;
  529. }
  530. public override AsyncIteratorBase<TResult> Clone()
  531. {
  532. return new GroupedResultAsyncEnumerableWithTask<TSource, TKey, TElement, TResult>(_source, _keySelector, _elementSelector, _resultSelector, _comparer);
  533. }
  534. public override async ValueTask DisposeAsync()
  535. {
  536. if (_enumerator != null)
  537. {
  538. await _enumerator.DisposeAsync().ConfigureAwait(false);
  539. _enumerator = null;
  540. _lookup = null;
  541. }
  542. await base.DisposeAsync().ConfigureAwait(false);
  543. }
  544. protected override async ValueTask<bool> MoveNextCore()
  545. {
  546. switch (_state)
  547. {
  548. case AsyncIteratorState.Allocated:
  549. _lookup = await Internal.LookupWithTask<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, _cancellationToken).ConfigureAwait(false);
  550. _enumerator = _lookup.SelectAwaitCore(async g => await _resultSelector(g.Key, g).ConfigureAwait(false)).GetAsyncEnumerator(_cancellationToken); // REVIEW: Introduce another ApplyResultSelector?
  551. _state = AsyncIteratorState.Iterating;
  552. goto case AsyncIteratorState.Iterating;
  553. case AsyncIteratorState.Iterating:
  554. if (await _enumerator!.MoveNextAsync().ConfigureAwait(false))
  555. {
  556. _current = _enumerator.Current;
  557. return true;
  558. }
  559. await DisposeAsync().ConfigureAwait(false);
  560. break;
  561. }
  562. return false;
  563. }
  564. public async ValueTask<TResult[]> ToArrayAsync(CancellationToken cancellationToken)
  565. {
  566. var l = await Internal.LookupWithTask<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  567. return await l.ToArray(_resultSelector).ConfigureAwait(false);
  568. }
  569. public async ValueTask<List<TResult>> ToListAsync(CancellationToken cancellationToken)
  570. {
  571. var l = await Internal.LookupWithTask<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  572. return await l.ToList(_resultSelector).ConfigureAwait(false);
  573. }
  574. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  575. {
  576. if (onlyIfCheap)
  577. {
  578. return new ValueTask<int>(-1);
  579. }
  580. return Core();
  581. async ValueTask<int> Core()
  582. {
  583. var l = await Internal.LookupWithTask<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  584. return l.Count;
  585. }
  586. }
  587. }
  588. #if !NO_DEEP_CANCELLATION
  589. private sealed class GroupedResultAsyncEnumerableWithTaskAndCancellation<TSource, TKey, TElement, TResult> : AsyncIterator<TResult>, IAsyncIListProvider<TResult>
  590. {
  591. private readonly IAsyncEnumerable<TSource> _source;
  592. private readonly Func<TSource, CancellationToken, ValueTask<TKey>> _keySelector;
  593. private readonly Func<TSource, CancellationToken, ValueTask<TElement>> _elementSelector;
  594. private readonly Func<TKey, IAsyncEnumerable<TElement>, CancellationToken, ValueTask<TResult>> _resultSelector;
  595. private readonly IEqualityComparer<TKey>? _comparer;
  596. private Internal.LookupWithTask<TKey, TElement>? _lookup;
  597. private IAsyncEnumerator<TResult>? _enumerator;
  598. public GroupedResultAsyncEnumerableWithTaskAndCancellation(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, Func<TKey, IAsyncEnumerable<TElement>, CancellationToken, ValueTask<TResult>> resultSelector, IEqualityComparer<TKey>? comparer)
  599. {
  600. _source = source ?? throw Error.ArgumentNull(nameof(source));
  601. _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector));
  602. _elementSelector = elementSelector ?? throw Error.ArgumentNull(nameof(elementSelector));
  603. _resultSelector = resultSelector ?? throw Error.ArgumentNull(nameof(resultSelector));
  604. _comparer = comparer;
  605. }
  606. public override AsyncIteratorBase<TResult> Clone()
  607. {
  608. return new GroupedResultAsyncEnumerableWithTaskAndCancellation<TSource, TKey, TElement, TResult>(_source, _keySelector, _elementSelector, _resultSelector, _comparer);
  609. }
  610. public override async ValueTask DisposeAsync()
  611. {
  612. if (_enumerator != null)
  613. {
  614. await _enumerator.DisposeAsync().ConfigureAwait(false);
  615. _enumerator = null;
  616. _lookup = null;
  617. }
  618. await base.DisposeAsync().ConfigureAwait(false);
  619. }
  620. protected override async ValueTask<bool> MoveNextCore()
  621. {
  622. switch (_state)
  623. {
  624. case AsyncIteratorState.Allocated:
  625. _lookup = await Internal.LookupWithTask<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, _cancellationToken).ConfigureAwait(false);
  626. _enumerator = _lookup.SelectAwaitCore(async g => await _resultSelector(g.Key, g, _cancellationToken).ConfigureAwait(false)).GetAsyncEnumerator(_cancellationToken); // REVIEW: Introduce another ApplyResultSelector?
  627. _state = AsyncIteratorState.Iterating;
  628. goto case AsyncIteratorState.Iterating;
  629. case AsyncIteratorState.Iterating:
  630. if (await _enumerator!.MoveNextAsync().ConfigureAwait(false))
  631. {
  632. _current = _enumerator.Current;
  633. return true;
  634. }
  635. await DisposeAsync().ConfigureAwait(false);
  636. break;
  637. }
  638. return false;
  639. }
  640. public async ValueTask<TResult[]> ToArrayAsync(CancellationToken cancellationToken)
  641. {
  642. var l = await Internal.LookupWithTask<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  643. return await l.ToArray(_resultSelector, cancellationToken).ConfigureAwait(false);
  644. }
  645. public async ValueTask<List<TResult>> ToListAsync(CancellationToken cancellationToken)
  646. {
  647. var l = await Internal.LookupWithTask<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  648. return await l.ToList(_resultSelector, cancellationToken).ConfigureAwait(false);
  649. }
  650. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  651. {
  652. if (onlyIfCheap)
  653. {
  654. return new ValueTask<int>(-1);
  655. }
  656. return Core();
  657. async ValueTask<int> Core()
  658. {
  659. var l = await Internal.LookupWithTask<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  660. return l.Count;
  661. }
  662. }
  663. }
  664. #endif
  665. private sealed class GroupedAsyncEnumerable<TSource, TKey, TElement> : AsyncIterator<IAsyncGrouping<TKey, TElement>>, IAsyncIListProvider<IAsyncGrouping<TKey, TElement>>
  666. {
  667. private readonly IAsyncEnumerable<TSource> _source;
  668. private readonly Func<TSource, TKey> _keySelector;
  669. private readonly Func<TSource, TElement> _elementSelector;
  670. private readonly IEqualityComparer<TKey>? _comparer;
  671. private Internal.Lookup<TKey, TElement>? _lookup;
  672. private IEnumerator<IGrouping<TKey, TElement>>? _enumerator;
  673. public GroupedAsyncEnumerable(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer)
  674. {
  675. _source = source ?? throw Error.ArgumentNull(nameof(source));
  676. _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector));
  677. _elementSelector = elementSelector ?? throw Error.ArgumentNull(nameof(elementSelector));
  678. _comparer = comparer;
  679. }
  680. public override AsyncIteratorBase<IAsyncGrouping<TKey, TElement>> Clone()
  681. {
  682. return new GroupedAsyncEnumerable<TSource, TKey, TElement>(_source, _keySelector, _elementSelector, _comparer);
  683. }
  684. public override async ValueTask DisposeAsync()
  685. {
  686. if (_enumerator != null)
  687. {
  688. _enumerator.Dispose();
  689. _enumerator = null;
  690. _lookup = null;
  691. }
  692. await base.DisposeAsync().ConfigureAwait(false);
  693. }
  694. protected override async ValueTask<bool> MoveNextCore()
  695. {
  696. switch (_state)
  697. {
  698. case AsyncIteratorState.Allocated:
  699. _lookup = await Internal.Lookup<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, _cancellationToken).ConfigureAwait(false);
  700. _enumerator = _lookup.GetEnumerator();
  701. _state = AsyncIteratorState.Iterating;
  702. goto case AsyncIteratorState.Iterating;
  703. case AsyncIteratorState.Iterating:
  704. if (_enumerator!.MoveNext())
  705. {
  706. _current = (IAsyncGrouping<TKey, TElement>)_enumerator.Current;
  707. return true;
  708. }
  709. await DisposeAsync().ConfigureAwait(false);
  710. break;
  711. }
  712. return false;
  713. }
  714. public async ValueTask<IAsyncGrouping<TKey, TElement>[]> ToArrayAsync(CancellationToken cancellationToken)
  715. {
  716. IAsyncIListProvider<IAsyncGrouping<TKey, TElement>> l = await Internal.Lookup<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  717. return await l.ToArrayAsync(cancellationToken).ConfigureAwait(false);
  718. }
  719. public async ValueTask<List<IAsyncGrouping<TKey, TElement>>> ToListAsync(CancellationToken cancellationToken)
  720. {
  721. IAsyncIListProvider<IAsyncGrouping<TKey, TElement>> l = await Internal.Lookup<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  722. return await l.ToListAsync(cancellationToken).ConfigureAwait(false);
  723. }
  724. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  725. {
  726. if (onlyIfCheap)
  727. {
  728. return new ValueTask<int>(-1);
  729. }
  730. return Core();
  731. async ValueTask<int> Core()
  732. {
  733. var l = await Internal.Lookup<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  734. return l.Count;
  735. }
  736. }
  737. }
  738. private sealed class GroupedAsyncEnumerableWithTask<TSource, TKey, TElement> : AsyncIterator<IAsyncGrouping<TKey, TElement>>, IAsyncIListProvider<IAsyncGrouping<TKey, TElement>>
  739. {
  740. private readonly IAsyncEnumerable<TSource> _source;
  741. private readonly Func<TSource, ValueTask<TKey>> _keySelector;
  742. private readonly Func<TSource, ValueTask<TElement>> _elementSelector;
  743. private readonly IEqualityComparer<TKey>? _comparer;
  744. private Internal.LookupWithTask<TKey, TElement>? _lookup;
  745. private IEnumerator<IGrouping<TKey, TElement>>? _enumerator;
  746. public GroupedAsyncEnumerableWithTask(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey>? comparer)
  747. {
  748. _source = source ?? throw Error.ArgumentNull(nameof(source));
  749. _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector));
  750. _elementSelector = elementSelector ?? throw Error.ArgumentNull(nameof(elementSelector));
  751. _comparer = comparer;
  752. }
  753. public override AsyncIteratorBase<IAsyncGrouping<TKey, TElement>> Clone()
  754. {
  755. return new GroupedAsyncEnumerableWithTask<TSource, TKey, TElement>(_source, _keySelector, _elementSelector, _comparer);
  756. }
  757. public override async ValueTask DisposeAsync()
  758. {
  759. if (_enumerator != null)
  760. {
  761. _enumerator.Dispose();
  762. _enumerator = null;
  763. _lookup = null;
  764. }
  765. await base.DisposeAsync().ConfigureAwait(false);
  766. }
  767. protected override async ValueTask<bool> MoveNextCore()
  768. {
  769. switch (_state)
  770. {
  771. case AsyncIteratorState.Allocated:
  772. _lookup = await Internal.LookupWithTask<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, _cancellationToken).ConfigureAwait(false);
  773. _enumerator = _lookup.GetEnumerator();
  774. _state = AsyncIteratorState.Iterating;
  775. goto case AsyncIteratorState.Iterating;
  776. case AsyncIteratorState.Iterating:
  777. if (_enumerator!.MoveNext())
  778. {
  779. _current = (IAsyncGrouping<TKey, TElement>)_enumerator.Current;
  780. return true;
  781. }
  782. await DisposeAsync().ConfigureAwait(false);
  783. break;
  784. }
  785. return false;
  786. }
  787. public async ValueTask<IAsyncGrouping<TKey, TElement>[]> ToArrayAsync(CancellationToken cancellationToken)
  788. {
  789. IAsyncIListProvider<IAsyncGrouping<TKey, TElement>> l = await Internal.LookupWithTask<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  790. return await l.ToArrayAsync(cancellationToken).ConfigureAwait(false);
  791. }
  792. public async ValueTask<List<IAsyncGrouping<TKey, TElement>>> ToListAsync(CancellationToken cancellationToken)
  793. {
  794. IAsyncIListProvider<IAsyncGrouping<TKey, TElement>> l = await Internal.LookupWithTask<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  795. return await l.ToListAsync(cancellationToken).ConfigureAwait(false);
  796. }
  797. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  798. {
  799. if (onlyIfCheap)
  800. {
  801. return new ValueTask<int>(-1);
  802. }
  803. return Core();
  804. async ValueTask<int> Core()
  805. {
  806. var l = await Internal.LookupWithTask<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  807. return l.Count;
  808. }
  809. }
  810. }
  811. #if !NO_DEEP_CANCELLATION
  812. private sealed class GroupedAsyncEnumerableWithTaskAndCancellation<TSource, TKey, TElement> : AsyncIterator<IAsyncGrouping<TKey, TElement>>, IAsyncIListProvider<IAsyncGrouping<TKey, TElement>>
  813. {
  814. private readonly IAsyncEnumerable<TSource> _source;
  815. private readonly Func<TSource, CancellationToken, ValueTask<TKey>> _keySelector;
  816. private readonly Func<TSource, CancellationToken, ValueTask<TElement>> _elementSelector;
  817. private readonly IEqualityComparer<TKey>? _comparer;
  818. private Internal.LookupWithTask<TKey, TElement>? _lookup;
  819. private IEnumerator<IGrouping<TKey, TElement>>? _enumerator;
  820. public GroupedAsyncEnumerableWithTaskAndCancellation(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey>? comparer)
  821. {
  822. _source = source ?? throw Error.ArgumentNull(nameof(source));
  823. _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector));
  824. _elementSelector = elementSelector ?? throw Error.ArgumentNull(nameof(elementSelector));
  825. _comparer = comparer;
  826. }
  827. public override AsyncIteratorBase<IAsyncGrouping<TKey, TElement>> Clone()
  828. {
  829. return new GroupedAsyncEnumerableWithTaskAndCancellation<TSource, TKey, TElement>(_source, _keySelector, _elementSelector, _comparer);
  830. }
  831. public override async ValueTask DisposeAsync()
  832. {
  833. if (_enumerator != null)
  834. {
  835. _enumerator.Dispose();
  836. _enumerator = null;
  837. _lookup = null;
  838. }
  839. await base.DisposeAsync().ConfigureAwait(false);
  840. }
  841. protected override async ValueTask<bool> MoveNextCore()
  842. {
  843. switch (_state)
  844. {
  845. case AsyncIteratorState.Allocated:
  846. _lookup = await Internal.LookupWithTask<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, _cancellationToken).ConfigureAwait(false);
  847. _enumerator = _lookup.GetEnumerator();
  848. _state = AsyncIteratorState.Iterating;
  849. goto case AsyncIteratorState.Iterating;
  850. case AsyncIteratorState.Iterating:
  851. if (_enumerator!.MoveNext())
  852. {
  853. _current = (IAsyncGrouping<TKey, TElement>)_enumerator.Current;
  854. return true;
  855. }
  856. await DisposeAsync().ConfigureAwait(false);
  857. break;
  858. }
  859. return false;
  860. }
  861. public async ValueTask<IAsyncGrouping<TKey, TElement>[]> ToArrayAsync(CancellationToken cancellationToken)
  862. {
  863. IAsyncIListProvider<IAsyncGrouping<TKey, TElement>> l = await Internal.LookupWithTask<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  864. return await l.ToArrayAsync(cancellationToken).ConfigureAwait(false);
  865. }
  866. public async ValueTask<List<IAsyncGrouping<TKey, TElement>>> ToListAsync(CancellationToken cancellationToken)
  867. {
  868. IAsyncIListProvider<IAsyncGrouping<TKey, TElement>> l = await Internal.LookupWithTask<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  869. return await l.ToListAsync(cancellationToken).ConfigureAwait(false);
  870. }
  871. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  872. {
  873. if (onlyIfCheap)
  874. {
  875. return new ValueTask<int>(-1);
  876. }
  877. return Core();
  878. async ValueTask<int> Core()
  879. {
  880. var l = await Internal.LookupWithTask<TKey, TElement>.CreateAsync(_source, _keySelector, _elementSelector, _comparer, cancellationToken).ConfigureAwait(false);
  881. return l.Count;
  882. }
  883. }
  884. }
  885. #endif
  886. private sealed class GroupedAsyncEnumerable<TSource, TKey> : AsyncIterator<IAsyncGrouping<TKey, TSource>>, IAsyncIListProvider<IAsyncGrouping<TKey, TSource>>
  887. {
  888. private readonly IAsyncEnumerable<TSource> _source;
  889. private readonly Func<TSource, TKey> _keySelector;
  890. private readonly IEqualityComparer<TKey>? _comparer;
  891. private Internal.Lookup<TKey, TSource>? _lookup;
  892. private IEnumerator<IGrouping<TKey, TSource>>? _enumerator;
  893. public GroupedAsyncEnumerable(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
  894. {
  895. _source = source ?? throw Error.ArgumentNull(nameof(source));
  896. _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector));
  897. _comparer = comparer;
  898. }
  899. public override AsyncIteratorBase<IAsyncGrouping<TKey, TSource>> Clone()
  900. {
  901. return new GroupedAsyncEnumerable<TSource, TKey>(_source, _keySelector, _comparer);
  902. }
  903. public override async ValueTask DisposeAsync()
  904. {
  905. if (_enumerator != null)
  906. {
  907. _enumerator.Dispose();
  908. _enumerator = null;
  909. _lookup = null;
  910. }
  911. await base.DisposeAsync().ConfigureAwait(false);
  912. }
  913. protected override async ValueTask<bool> MoveNextCore()
  914. {
  915. switch (_state)
  916. {
  917. case AsyncIteratorState.Allocated:
  918. _lookup = await Internal.Lookup<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, _cancellationToken).ConfigureAwait(false);
  919. _enumerator = _lookup.GetEnumerator();
  920. _state = AsyncIteratorState.Iterating;
  921. goto case AsyncIteratorState.Iterating;
  922. case AsyncIteratorState.Iterating:
  923. if (_enumerator!.MoveNext())
  924. {
  925. _current = (IAsyncGrouping<TKey, TSource>)_enumerator.Current;
  926. return true;
  927. }
  928. await DisposeAsync().ConfigureAwait(false);
  929. break;
  930. }
  931. return false;
  932. }
  933. public async ValueTask<IAsyncGrouping<TKey, TSource>[]> ToArrayAsync(CancellationToken cancellationToken)
  934. {
  935. IAsyncIListProvider<IAsyncGrouping<TKey, TSource>> l = await Internal.Lookup<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  936. return await l.ToArrayAsync(cancellationToken).ConfigureAwait(false);
  937. }
  938. public async ValueTask<List<IAsyncGrouping<TKey, TSource>>> ToListAsync(CancellationToken cancellationToken)
  939. {
  940. IAsyncIListProvider<IAsyncGrouping<TKey, TSource>> l = await Internal.Lookup<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  941. return await l.ToListAsync(cancellationToken).ConfigureAwait(false);
  942. }
  943. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  944. {
  945. if (onlyIfCheap)
  946. {
  947. return new ValueTask<int>(-1);
  948. }
  949. return Core();
  950. async ValueTask<int> Core()
  951. {
  952. var l = await Internal.Lookup<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  953. return l.Count;
  954. }
  955. }
  956. }
  957. private sealed class GroupedAsyncEnumerableWithTask<TSource, TKey> : AsyncIterator<IAsyncGrouping<TKey, TSource>>, IAsyncIListProvider<IAsyncGrouping<TKey, TSource>>
  958. {
  959. private readonly IAsyncEnumerable<TSource> _source;
  960. private readonly Func<TSource, ValueTask<TKey>> _keySelector;
  961. private readonly IEqualityComparer<TKey>? _comparer;
  962. private Internal.LookupWithTask<TKey, TSource>? _lookup;
  963. private IEnumerator<IGrouping<TKey, TSource>>? _enumerator;
  964. public GroupedAsyncEnumerableWithTask(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer)
  965. {
  966. _source = source ?? throw Error.ArgumentNull(nameof(source));
  967. _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector));
  968. _comparer = comparer;
  969. }
  970. public override AsyncIteratorBase<IAsyncGrouping<TKey, TSource>> Clone()
  971. {
  972. return new GroupedAsyncEnumerableWithTask<TSource, TKey>(_source, _keySelector, _comparer);
  973. }
  974. public override async ValueTask DisposeAsync()
  975. {
  976. if (_enumerator != null)
  977. {
  978. _enumerator.Dispose();
  979. _enumerator = null;
  980. _lookup = null;
  981. }
  982. await base.DisposeAsync().ConfigureAwait(false);
  983. }
  984. protected override async ValueTask<bool> MoveNextCore()
  985. {
  986. switch (_state)
  987. {
  988. case AsyncIteratorState.Allocated:
  989. _lookup = await Internal.LookupWithTask<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, _cancellationToken).ConfigureAwait(false);
  990. _enumerator = _lookup.GetEnumerator();
  991. _state = AsyncIteratorState.Iterating;
  992. goto case AsyncIteratorState.Iterating;
  993. case AsyncIteratorState.Iterating:
  994. if (_enumerator!.MoveNext())
  995. {
  996. _current = (IAsyncGrouping<TKey, TSource>)_enumerator.Current;
  997. return true;
  998. }
  999. await DisposeAsync().ConfigureAwait(false);
  1000. break;
  1001. }
  1002. return false;
  1003. }
  1004. public async ValueTask<IAsyncGrouping<TKey, TSource>[]> ToArrayAsync(CancellationToken cancellationToken)
  1005. {
  1006. IAsyncIListProvider<IAsyncGrouping<TKey, TSource>> l = await Internal.LookupWithTask<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  1007. return await l.ToArrayAsync(cancellationToken).ConfigureAwait(false);
  1008. }
  1009. public async ValueTask<List<IAsyncGrouping<TKey, TSource>>> ToListAsync(CancellationToken cancellationToken)
  1010. {
  1011. IAsyncIListProvider<IAsyncGrouping<TKey, TSource>> l = await Internal.LookupWithTask<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  1012. return await l.ToListAsync(cancellationToken).ConfigureAwait(false);
  1013. }
  1014. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  1015. {
  1016. if (onlyIfCheap)
  1017. {
  1018. return new ValueTask<int>(-1);
  1019. }
  1020. return Core();
  1021. async ValueTask<int> Core()
  1022. {
  1023. var l = await Internal.LookupWithTask<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  1024. return l.Count;
  1025. }
  1026. }
  1027. }
  1028. #if !NO_DEEP_CANCELLATION
  1029. private sealed class GroupedAsyncEnumerableWithTaskAndCancellation<TSource, TKey> : AsyncIterator<IAsyncGrouping<TKey, TSource>>, IAsyncIListProvider<IAsyncGrouping<TKey, TSource>>
  1030. {
  1031. private readonly IAsyncEnumerable<TSource> _source;
  1032. private readonly Func<TSource, CancellationToken, ValueTask<TKey>> _keySelector;
  1033. private readonly IEqualityComparer<TKey>? _comparer;
  1034. private Internal.LookupWithTask<TKey, TSource>? _lookup;
  1035. private IEnumerator<IGrouping<TKey, TSource>>? _enumerator;
  1036. public GroupedAsyncEnumerableWithTaskAndCancellation(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer)
  1037. {
  1038. _source = source ?? throw Error.ArgumentNull(nameof(source));
  1039. _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector));
  1040. _comparer = comparer;
  1041. }
  1042. public override AsyncIteratorBase<IAsyncGrouping<TKey, TSource>> Clone()
  1043. {
  1044. return new GroupedAsyncEnumerableWithTaskAndCancellation<TSource, TKey>(_source, _keySelector, _comparer);
  1045. }
  1046. public override async ValueTask DisposeAsync()
  1047. {
  1048. if (_enumerator != null)
  1049. {
  1050. _enumerator.Dispose();
  1051. _enumerator = null;
  1052. _lookup = null;
  1053. }
  1054. await base.DisposeAsync().ConfigureAwait(false);
  1055. }
  1056. protected override async ValueTask<bool> MoveNextCore()
  1057. {
  1058. switch (_state)
  1059. {
  1060. case AsyncIteratorState.Allocated:
  1061. _lookup = await Internal.LookupWithTask<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, _cancellationToken).ConfigureAwait(false);
  1062. _enumerator = _lookup.GetEnumerator();
  1063. _state = AsyncIteratorState.Iterating;
  1064. goto case AsyncIteratorState.Iterating;
  1065. case AsyncIteratorState.Iterating:
  1066. if (_enumerator!.MoveNext())
  1067. {
  1068. _current = (IAsyncGrouping<TKey, TSource>)_enumerator.Current;
  1069. return true;
  1070. }
  1071. await DisposeAsync().ConfigureAwait(false);
  1072. break;
  1073. }
  1074. return false;
  1075. }
  1076. public async ValueTask<IAsyncGrouping<TKey, TSource>[]> ToArrayAsync(CancellationToken cancellationToken)
  1077. {
  1078. IAsyncIListProvider<IAsyncGrouping<TKey, TSource>> l = await Internal.LookupWithTask<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  1079. return await l.ToArrayAsync(cancellationToken).ConfigureAwait(false);
  1080. }
  1081. public async ValueTask<List<IAsyncGrouping<TKey, TSource>>> ToListAsync(CancellationToken cancellationToken)
  1082. {
  1083. IAsyncIListProvider<IAsyncGrouping<TKey, TSource>> l = await Internal.LookupWithTask<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  1084. return await l.ToListAsync(cancellationToken).ConfigureAwait(false);
  1085. }
  1086. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  1087. {
  1088. if (onlyIfCheap)
  1089. {
  1090. return new ValueTask<int>(-1);
  1091. }
  1092. return Core();
  1093. async ValueTask<int> Core()
  1094. {
  1095. var l = await Internal.LookupWithTask<TKey, TSource>.CreateAsync(_source, _keySelector, _comparer, cancellationToken).ConfigureAwait(false);
  1096. return l.Count;
  1097. }
  1098. }
  1099. }
  1100. #endif
  1101. }
  1102. }