Lookup.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. // This is internal because System.Linq exposes a public Lookup that we cannot directly use here
  10. namespace System.Linq.Internal
  11. {
  12. internal class Lookup<TKey, TElement> : ILookup<TKey, TElement>, IAsyncIListProvider<IAsyncGrouping<TKey, TElement>>
  13. {
  14. private readonly IEqualityComparer<TKey> _comparer;
  15. private Grouping<TKey, TElement>[] _groupings;
  16. private Grouping<TKey, TElement> _lastGrouping;
  17. private Lookup(IEqualityComparer<TKey> comparer)
  18. {
  19. _comparer = comparer ?? EqualityComparer<TKey>.Default;
  20. _groupings = new Grouping<TKey, TElement>[7];
  21. }
  22. public int Count { get; private set; }
  23. public IEnumerable<TElement> this[TKey key]
  24. {
  25. get
  26. {
  27. var grouping = GetGrouping(key, create: false);
  28. if (grouping != null)
  29. {
  30. return grouping;
  31. }
  32. #if NO_ARRAY_EMPTY
  33. return EmptyArray<TElement>.Value;
  34. #else
  35. return Array.Empty<TElement>();
  36. #endif
  37. }
  38. }
  39. public bool Contains(TKey key)
  40. {
  41. return GetGrouping(key, create: false) != null;
  42. }
  43. IEnumerator IEnumerable.GetEnumerator()
  44. {
  45. return GetEnumerator();
  46. }
  47. public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
  48. {
  49. var g = _lastGrouping;
  50. if (g != null)
  51. {
  52. do
  53. {
  54. g = g._next;
  55. yield return g;
  56. } while (g != _lastGrouping);
  57. }
  58. }
  59. public IEnumerable<TResult> ApplyResultSelector<TResult>(Func<TKey, IAsyncEnumerable<TElement>, TResult> resultSelector)
  60. {
  61. var g = _lastGrouping;
  62. if (g != null)
  63. {
  64. do
  65. {
  66. g = g._next;
  67. g.Trim();
  68. var result = resultSelector(g._key, g._elements.ToAsyncEnumerable());
  69. yield return result;
  70. } while (g != _lastGrouping);
  71. }
  72. }
  73. internal static async Task<Lookup<TKey, TElement>> CreateAsync<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  74. {
  75. Debug.Assert(source != null);
  76. Debug.Assert(keySelector != null);
  77. Debug.Assert(elementSelector != null);
  78. var lookup = new Lookup<TKey, TElement>(comparer);
  79. var enu = source.GetAsyncEnumerator(cancellationToken);
  80. try
  81. {
  82. while (await enu.MoveNextAsync().ConfigureAwait(false))
  83. {
  84. var key = keySelector(enu.Current);
  85. var group = lookup.GetGrouping(key, create: true);
  86. var element = elementSelector(enu.Current);
  87. group.Add(element);
  88. }
  89. }
  90. finally
  91. {
  92. await enu.DisposeAsync().ConfigureAwait(false);
  93. }
  94. return lookup;
  95. }
  96. internal static async Task<Lookup<TKey, TElement>> CreateAsync(IAsyncEnumerable<TElement> source, Func<TElement, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  97. {
  98. Debug.Assert(source != null);
  99. Debug.Assert(keySelector != null);
  100. var lookup = new Lookup<TKey, TElement>(comparer);
  101. var enu = source.GetAsyncEnumerator(cancellationToken);
  102. try
  103. {
  104. while (await enu.MoveNextAsync().ConfigureAwait(false))
  105. {
  106. var key = keySelector(enu.Current);
  107. lookup.GetGrouping(key, create: true).Add(enu.Current);
  108. }
  109. }
  110. finally
  111. {
  112. await enu.DisposeAsync().ConfigureAwait(false);
  113. }
  114. return lookup;
  115. }
  116. internal static async Task<Lookup<TKey, TElement>> CreateForJoinAsync(IAsyncEnumerable<TElement> source, Func<TElement, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  117. {
  118. var lookup = new Lookup<TKey, TElement>(comparer);
  119. var enu = source.GetAsyncEnumerator(cancellationToken);
  120. try
  121. {
  122. while (await enu.MoveNextAsync().ConfigureAwait(false))
  123. {
  124. var key = keySelector(enu.Current);
  125. if (key != null)
  126. {
  127. lookup.GetGrouping(key, create: true).Add(enu.Current);
  128. }
  129. }
  130. }
  131. finally
  132. {
  133. await enu.DisposeAsync().ConfigureAwait(false);
  134. }
  135. return lookup;
  136. }
  137. internal Grouping<TKey, TElement> GetGrouping(TKey key, bool create)
  138. {
  139. var hashCode = InternalGetHashCode(key);
  140. for (var g = _groupings[hashCode % _groupings.Length]; g != null; g = g._hashNext)
  141. {
  142. if (g._hashCode == hashCode && _comparer.Equals(g._key, key))
  143. {
  144. return g;
  145. }
  146. }
  147. if (create)
  148. {
  149. if (Count == _groupings.Length)
  150. {
  151. Resize();
  152. }
  153. var index = hashCode % _groupings.Length;
  154. var g = new Grouping<TKey, TElement>
  155. {
  156. _key = key,
  157. _hashCode = hashCode,
  158. _elements = new TElement[1],
  159. _hashNext = _groupings[index]
  160. };
  161. _groupings[index] = g;
  162. if (_lastGrouping == null)
  163. {
  164. g._next = g;
  165. }
  166. else
  167. {
  168. g._next = _lastGrouping._next;
  169. _lastGrouping._next = g;
  170. }
  171. _lastGrouping = g;
  172. Count++;
  173. return g;
  174. }
  175. return null;
  176. }
  177. internal int InternalGetHashCode(TKey key)
  178. {
  179. // Handle comparer implementations that throw when passed null
  180. return (key == null) ? 0 : _comparer.GetHashCode(key) & 0x7FFFFFFF;
  181. }
  182. internal TResult[] ToArray<TResult>(Func<TKey, IAsyncEnumerable<TElement>, TResult> resultSelector)
  183. {
  184. var array = new TResult[Count];
  185. var index = 0;
  186. var g = _lastGrouping;
  187. if (g != null)
  188. {
  189. do
  190. {
  191. g = g._next;
  192. g.Trim();
  193. array[index] = resultSelector(g._key, g._elements.ToAsyncEnumerable());
  194. ++index;
  195. } while (g != _lastGrouping);
  196. }
  197. return array;
  198. }
  199. internal List<TResult> ToList<TResult>(Func<TKey, IAsyncEnumerable<TElement>, TResult> resultSelector)
  200. {
  201. var list = new List<TResult>(Count);
  202. var g = _lastGrouping;
  203. if (g != null)
  204. {
  205. do
  206. {
  207. g = g._next;
  208. g.Trim();
  209. var result = resultSelector(g._key, g._elements.ToAsyncEnumerable());
  210. list.Add(result);
  211. } while (g != _lastGrouping);
  212. }
  213. return list;
  214. }
  215. private void Resize()
  216. {
  217. var newSize = checked((Count * 2) + 1);
  218. var newGroupings = new Grouping<TKey, TElement>[newSize];
  219. var g = _lastGrouping;
  220. do
  221. {
  222. g = g._next;
  223. var index = g._hashCode % newSize;
  224. g._hashNext = newGroupings[index];
  225. newGroupings[index] = g;
  226. } while (g != _lastGrouping);
  227. _groupings = newGroupings;
  228. }
  229. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  230. {
  231. return new ValueTask<int>(Count);
  232. }
  233. IAsyncEnumerator<IAsyncGrouping<TKey, TElement>> IAsyncEnumerable<IAsyncGrouping<TKey, TElement>>.GetAsyncEnumerator(CancellationToken cancellationToken)
  234. {
  235. cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior.
  236. return Enumerable.Cast<IAsyncGrouping<TKey, TElement>>(this).ToAsyncEnumerable().GetAsyncEnumerator(cancellationToken);
  237. }
  238. ValueTask<List<IAsyncGrouping<TKey, TElement>>> IAsyncIListProvider<IAsyncGrouping<TKey, TElement>>.ToListAsync(CancellationToken cancellationToken)
  239. {
  240. cancellationToken.ThrowIfCancellationRequested();
  241. var list = new List<IAsyncGrouping<TKey, TElement>>(Count);
  242. var g = _lastGrouping;
  243. if (g != null)
  244. {
  245. do
  246. {
  247. g = g._next;
  248. list.Add(g);
  249. }
  250. while (g != _lastGrouping);
  251. }
  252. return new ValueTask<List<IAsyncGrouping<TKey, TElement>>>(list);
  253. }
  254. ValueTask<IAsyncGrouping<TKey, TElement>[]> IAsyncIListProvider<IAsyncGrouping<TKey, TElement>>.ToArrayAsync(CancellationToken cancellationToken)
  255. {
  256. cancellationToken.ThrowIfCancellationRequested();
  257. var array = new IAsyncGrouping<TKey, TElement>[Count];
  258. var index = 0;
  259. var g = _lastGrouping;
  260. if (g != null)
  261. {
  262. do
  263. {
  264. g = g._next;
  265. array[index] = g;
  266. ++index;
  267. }
  268. while (g != _lastGrouping);
  269. }
  270. return new ValueTask<IAsyncGrouping<TKey, TElement>[]>(array);
  271. }
  272. }
  273. internal class LookupWithTask<TKey, TElement> : ILookup<TKey, TElement>, IAsyncIListProvider<IAsyncGrouping<TKey, TElement>>
  274. {
  275. private readonly IEqualityComparer<TKey> _comparer;
  276. private Grouping<TKey, TElement>[] _groupings;
  277. private Grouping<TKey, TElement> _lastGrouping;
  278. private LookupWithTask(IEqualityComparer<TKey> comparer)
  279. {
  280. _comparer = comparer ?? EqualityComparer<TKey>.Default;
  281. _groupings = new Grouping<TKey, TElement>[7];
  282. }
  283. public int Count { get; private set; }
  284. public IEnumerable<TElement> this[TKey key]
  285. {
  286. get
  287. {
  288. var grouping = GetGrouping(key, create: false);
  289. if (grouping != null)
  290. {
  291. return grouping;
  292. }
  293. #if NO_ARRAY_EMPTY
  294. return EmptyArray<TElement>.Value;
  295. #else
  296. return Array.Empty<TElement>();
  297. #endif
  298. }
  299. }
  300. public bool Contains(TKey key)
  301. {
  302. return GetGrouping(key, create: false) != null;
  303. }
  304. IEnumerator IEnumerable.GetEnumerator()
  305. {
  306. return GetEnumerator();
  307. }
  308. public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
  309. {
  310. var g = _lastGrouping;
  311. if (g != null)
  312. {
  313. do
  314. {
  315. g = g._next;
  316. yield return g;
  317. } while (g != _lastGrouping);
  318. }
  319. }
  320. internal static async Task<LookupWithTask<TKey, TElement>> CreateAsync<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TSource, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  321. {
  322. Debug.Assert(source != null);
  323. Debug.Assert(keySelector != null);
  324. Debug.Assert(elementSelector != null);
  325. var lookup = new LookupWithTask<TKey, TElement>(comparer);
  326. var enu = source.GetAsyncEnumerator(cancellationToken);
  327. try
  328. {
  329. while (await enu.MoveNextAsync().ConfigureAwait(false))
  330. {
  331. var key = await keySelector(enu.Current).ConfigureAwait(false);
  332. var group = lookup.GetGrouping(key, create: true);
  333. var element = await elementSelector(enu.Current).ConfigureAwait(false);
  334. group.Add(element);
  335. }
  336. }
  337. finally
  338. {
  339. await enu.DisposeAsync().ConfigureAwait(false);
  340. }
  341. return lookup;
  342. }
  343. #if !NO_DEEP_CANCELLATION
  344. internal static async Task<LookupWithTask<TKey, TElement>> CreateAsync<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  345. {
  346. Debug.Assert(source != null);
  347. Debug.Assert(keySelector != null);
  348. Debug.Assert(elementSelector != null);
  349. var lookup = new LookupWithTask<TKey, TElement>(comparer);
  350. var enu = source.GetAsyncEnumerator(cancellationToken);
  351. try
  352. {
  353. while (await enu.MoveNextAsync().ConfigureAwait(false))
  354. {
  355. var key = await keySelector(enu.Current, cancellationToken).ConfigureAwait(false);
  356. var group = lookup.GetGrouping(key, create: true);
  357. var element = await elementSelector(enu.Current, cancellationToken).ConfigureAwait(false);
  358. group.Add(element);
  359. }
  360. }
  361. finally
  362. {
  363. await enu.DisposeAsync().ConfigureAwait(false);
  364. }
  365. return lookup;
  366. }
  367. #endif
  368. internal static async Task<LookupWithTask<TKey, TElement>> CreateAsync(IAsyncEnumerable<TElement> source, Func<TElement, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  369. {
  370. Debug.Assert(source != null);
  371. Debug.Assert(keySelector != null);
  372. var lookup = new LookupWithTask<TKey, TElement>(comparer);
  373. var enu = source.GetAsyncEnumerator(cancellationToken);
  374. try
  375. {
  376. while (await enu.MoveNextAsync().ConfigureAwait(false))
  377. {
  378. var key = await keySelector(enu.Current).ConfigureAwait(false);
  379. lookup.GetGrouping(key, create: true).Add(enu.Current);
  380. }
  381. }
  382. finally
  383. {
  384. await enu.DisposeAsync().ConfigureAwait(false);
  385. }
  386. return lookup;
  387. }
  388. #if !NO_DEEP_CANCELLATION
  389. internal static async Task<LookupWithTask<TKey, TElement>> CreateAsync(IAsyncEnumerable<TElement> source, Func<TElement, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  390. {
  391. Debug.Assert(source != null);
  392. Debug.Assert(keySelector != null);
  393. var lookup = new LookupWithTask<TKey, TElement>(comparer);
  394. var enu = source.GetAsyncEnumerator(cancellationToken);
  395. try
  396. {
  397. while (await enu.MoveNextAsync().ConfigureAwait(false))
  398. {
  399. var key = await keySelector(enu.Current, cancellationToken).ConfigureAwait(false);
  400. lookup.GetGrouping(key, create: true).Add(enu.Current);
  401. }
  402. }
  403. finally
  404. {
  405. await enu.DisposeAsync().ConfigureAwait(false);
  406. }
  407. return lookup;
  408. }
  409. #endif
  410. internal static async Task<LookupWithTask<TKey, TElement>> CreateForJoinAsync(IAsyncEnumerable<TElement> source, Func<TElement, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  411. {
  412. var lookup = new LookupWithTask<TKey, TElement>(comparer);
  413. var enu = source.GetAsyncEnumerator(cancellationToken);
  414. try
  415. {
  416. while (await enu.MoveNextAsync().ConfigureAwait(false))
  417. {
  418. var key = await keySelector(enu.Current).ConfigureAwait(false);
  419. if (key != null)
  420. {
  421. lookup.GetGrouping(key, create: true).Add(enu.Current);
  422. }
  423. }
  424. }
  425. finally
  426. {
  427. await enu.DisposeAsync().ConfigureAwait(false);
  428. }
  429. return lookup;
  430. }
  431. #if !NO_DEEP_CANCELLATION
  432. internal static async Task<LookupWithTask<TKey, TElement>> CreateForJoinAsync(IAsyncEnumerable<TElement> source, Func<TElement, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  433. {
  434. var lookup = new LookupWithTask<TKey, TElement>(comparer);
  435. var enu = source.GetAsyncEnumerator(cancellationToken);
  436. try
  437. {
  438. while (await enu.MoveNextAsync().ConfigureAwait(false))
  439. {
  440. var key = await keySelector(enu.Current, cancellationToken).ConfigureAwait(false);
  441. if (key != null)
  442. {
  443. lookup.GetGrouping(key, create: true).Add(enu.Current);
  444. }
  445. }
  446. }
  447. finally
  448. {
  449. await enu.DisposeAsync().ConfigureAwait(false);
  450. }
  451. return lookup;
  452. }
  453. #endif
  454. internal Grouping<TKey, TElement> GetGrouping(TKey key, bool create)
  455. {
  456. var hashCode = InternalGetHashCode(key);
  457. for (var g = _groupings[hashCode % _groupings.Length]; g != null; g = g._hashNext)
  458. {
  459. if (g._hashCode == hashCode && _comparer.Equals(g._key, key))
  460. {
  461. return g;
  462. }
  463. }
  464. if (create)
  465. {
  466. if (Count == _groupings.Length)
  467. {
  468. Resize();
  469. }
  470. var index = hashCode % _groupings.Length;
  471. var g = new Grouping<TKey, TElement>
  472. {
  473. _key = key,
  474. _hashCode = hashCode,
  475. _elements = new TElement[1],
  476. _hashNext = _groupings[index]
  477. };
  478. _groupings[index] = g;
  479. if (_lastGrouping == null)
  480. {
  481. g._next = g;
  482. }
  483. else
  484. {
  485. g._next = _lastGrouping._next;
  486. _lastGrouping._next = g;
  487. }
  488. _lastGrouping = g;
  489. Count++;
  490. return g;
  491. }
  492. return null;
  493. }
  494. internal int InternalGetHashCode(TKey key)
  495. {
  496. // Handle comparer implementations that throw when passed null
  497. return (key == null) ? 0 : _comparer.GetHashCode(key) & 0x7FFFFFFF;
  498. }
  499. internal async Task<TResult[]> ToArray<TResult>(Func<TKey, IAsyncEnumerable<TElement>, ValueTask<TResult>> resultSelector)
  500. {
  501. var array = new TResult[Count];
  502. var index = 0;
  503. var g = _lastGrouping;
  504. if (g != null)
  505. {
  506. do
  507. {
  508. g = g._next;
  509. g.Trim();
  510. array[index] = await resultSelector(g._key, g._elements.ToAsyncEnumerable()).ConfigureAwait(false);
  511. ++index;
  512. } while (g != _lastGrouping);
  513. }
  514. return array;
  515. }
  516. #if !NO_DEEP_CANCELLATION
  517. internal async Task<TResult[]> ToArray<TResult>(Func<TKey, IAsyncEnumerable<TElement>, CancellationToken, ValueTask<TResult>> resultSelector, CancellationToken cancellationToken)
  518. {
  519. cancellationToken.ThrowIfCancellationRequested();
  520. var array = new TResult[Count];
  521. var index = 0;
  522. var g = _lastGrouping;
  523. if (g != null)
  524. {
  525. do
  526. {
  527. g = g._next;
  528. g.Trim();
  529. array[index] = await resultSelector(g._key, g._elements.ToAsyncEnumerable(), cancellationToken).ConfigureAwait(false);
  530. ++index;
  531. } while (g != _lastGrouping);
  532. }
  533. return array;
  534. }
  535. #endif
  536. internal async Task<List<TResult>> ToList<TResult>(Func<TKey, IAsyncEnumerable<TElement>, ValueTask<TResult>> resultSelector)
  537. {
  538. var list = new List<TResult>(Count);
  539. var g = _lastGrouping;
  540. if (g != null)
  541. {
  542. do
  543. {
  544. g = g._next;
  545. g.Trim();
  546. var result = await resultSelector(g._key, g._elements.ToAsyncEnumerable()).ConfigureAwait(false);
  547. list.Add(result);
  548. } while (g != _lastGrouping);
  549. }
  550. return list;
  551. }
  552. #if !NO_DEEP_CANCELLATION
  553. internal async Task<List<TResult>> ToList<TResult>(Func<TKey, IAsyncEnumerable<TElement>, CancellationToken, ValueTask<TResult>> resultSelector, CancellationToken cancellationToken)
  554. {
  555. cancellationToken.ThrowIfCancellationRequested();
  556. var list = new List<TResult>(Count);
  557. var g = _lastGrouping;
  558. if (g != null)
  559. {
  560. do
  561. {
  562. g = g._next;
  563. g.Trim();
  564. var result = await resultSelector(g._key, g._elements.ToAsyncEnumerable(), cancellationToken).ConfigureAwait(false);
  565. list.Add(result);
  566. } while (g != _lastGrouping);
  567. }
  568. return list;
  569. }
  570. #endif
  571. private void Resize()
  572. {
  573. var newSize = checked((Count * 2) + 1);
  574. var newGroupings = new Grouping<TKey, TElement>[newSize];
  575. var g = _lastGrouping;
  576. do
  577. {
  578. g = g._next;
  579. var index = g._hashCode % newSize;
  580. g._hashNext = newGroupings[index];
  581. newGroupings[index] = g;
  582. } while (g != _lastGrouping);
  583. _groupings = newGroupings;
  584. }
  585. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  586. {
  587. return new ValueTask<int>(Count);
  588. }
  589. IAsyncEnumerator<IAsyncGrouping<TKey, TElement>> IAsyncEnumerable<IAsyncGrouping<TKey, TElement>>.GetAsyncEnumerator(CancellationToken cancellationToken)
  590. {
  591. cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior.
  592. return Enumerable.Cast<IAsyncGrouping<TKey, TElement>>(this).ToAsyncEnumerable().GetAsyncEnumerator(cancellationToken);
  593. }
  594. ValueTask<List<IAsyncGrouping<TKey, TElement>>> IAsyncIListProvider<IAsyncGrouping<TKey, TElement>>.ToListAsync(CancellationToken cancellationToken)
  595. {
  596. cancellationToken.ThrowIfCancellationRequested();
  597. var list = new List<IAsyncGrouping<TKey, TElement>>(Count);
  598. var g = _lastGrouping;
  599. if (g != null)
  600. {
  601. do
  602. {
  603. g = g._next;
  604. list.Add(g);
  605. }
  606. while (g != _lastGrouping);
  607. }
  608. return new ValueTask<List<IAsyncGrouping<TKey, TElement>>>(list);
  609. }
  610. ValueTask<IAsyncGrouping<TKey, TElement>[]> IAsyncIListProvider<IAsyncGrouping<TKey, TElement>>.ToArrayAsync(CancellationToken cancellationToken)
  611. {
  612. cancellationToken.ThrowIfCancellationRequested();
  613. var array = new IAsyncGrouping<TKey, TElement>[Count];
  614. var index = 0;
  615. var g = _lastGrouping;
  616. if (g != null)
  617. {
  618. do
  619. {
  620. g = g._next;
  621. array[index] = g;
  622. ++index;
  623. }
  624. while (g != _lastGrouping);
  625. }
  626. return new ValueTask<IAsyncGrouping<TKey, TElement>[]>(array);
  627. }
  628. }
  629. }