GroupBy.cs 81 KB

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