Lookup.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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. var list = new List<IAsyncGrouping<TKey, TElement>>(Count);
  241. var g = _lastGrouping;
  242. if (g != null)
  243. {
  244. do
  245. {
  246. g = g._next;
  247. list.Add(g);
  248. }
  249. while (g != _lastGrouping);
  250. }
  251. return new ValueTask<List<IAsyncGrouping<TKey, TElement>>>(list);
  252. }
  253. ValueTask<IAsyncGrouping<TKey, TElement>[]> IAsyncIListProvider<IAsyncGrouping<TKey, TElement>>.ToArrayAsync(CancellationToken cancellationToken)
  254. {
  255. var array = new IAsyncGrouping<TKey, TElement>[Count];
  256. var index = 0;
  257. var g = _lastGrouping;
  258. if (g != null)
  259. {
  260. do
  261. {
  262. g = g._next;
  263. array[index] = g;
  264. ++index;
  265. }
  266. while (g != _lastGrouping);
  267. }
  268. return new ValueTask<IAsyncGrouping<TKey, TElement>[]>(array);
  269. }
  270. }
  271. internal class LookupWithTask<TKey, TElement> : ILookup<TKey, TElement>, IAsyncIListProvider<IAsyncGrouping<TKey, TElement>>
  272. {
  273. private readonly IEqualityComparer<TKey> _comparer;
  274. private Grouping<TKey, TElement>[] _groupings;
  275. private Grouping<TKey, TElement> _lastGrouping;
  276. private LookupWithTask(IEqualityComparer<TKey> comparer)
  277. {
  278. _comparer = comparer ?? EqualityComparer<TKey>.Default;
  279. _groupings = new Grouping<TKey, TElement>[7];
  280. }
  281. public int Count { get; private set; }
  282. public IEnumerable<TElement> this[TKey key]
  283. {
  284. get
  285. {
  286. var grouping = GetGrouping(key, create: false);
  287. if (grouping != null)
  288. {
  289. return grouping;
  290. }
  291. #if NO_ARRAY_EMPTY
  292. return EmptyArray<TElement>.Value;
  293. #else
  294. return Array.Empty<TElement>();
  295. #endif
  296. }
  297. }
  298. public bool Contains(TKey key)
  299. {
  300. return GetGrouping(key, create: false) != null;
  301. }
  302. IEnumerator IEnumerable.GetEnumerator()
  303. {
  304. return GetEnumerator();
  305. }
  306. public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
  307. {
  308. var g = _lastGrouping;
  309. if (g != null)
  310. {
  311. do
  312. {
  313. g = g._next;
  314. yield return g;
  315. } while (g != _lastGrouping);
  316. }
  317. }
  318. 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)
  319. {
  320. Debug.Assert(source != null);
  321. Debug.Assert(keySelector != null);
  322. Debug.Assert(elementSelector != null);
  323. var lookup = new LookupWithTask<TKey, TElement>(comparer);
  324. var enu = source.GetAsyncEnumerator(cancellationToken);
  325. try
  326. {
  327. while (await enu.MoveNextAsync().ConfigureAwait(false))
  328. {
  329. var key = await keySelector(enu.Current).ConfigureAwait(false);
  330. var group = lookup.GetGrouping(key, create: true);
  331. var element = await elementSelector(enu.Current).ConfigureAwait(false);
  332. group.Add(element);
  333. }
  334. }
  335. finally
  336. {
  337. await enu.DisposeAsync().ConfigureAwait(false);
  338. }
  339. return lookup;
  340. }
  341. #if !NO_DEEP_CANCELLATION
  342. 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)
  343. {
  344. Debug.Assert(source != null);
  345. Debug.Assert(keySelector != null);
  346. Debug.Assert(elementSelector != null);
  347. var lookup = new LookupWithTask<TKey, TElement>(comparer);
  348. var enu = source.GetAsyncEnumerator(cancellationToken);
  349. try
  350. {
  351. while (await enu.MoveNextAsync().ConfigureAwait(false))
  352. {
  353. var key = await keySelector(enu.Current, cancellationToken).ConfigureAwait(false);
  354. var group = lookup.GetGrouping(key, create: true);
  355. var element = await elementSelector(enu.Current, cancellationToken).ConfigureAwait(false);
  356. group.Add(element);
  357. }
  358. }
  359. finally
  360. {
  361. await enu.DisposeAsync().ConfigureAwait(false);
  362. }
  363. return lookup;
  364. }
  365. #endif
  366. internal static async Task<LookupWithTask<TKey, TElement>> CreateAsync(IAsyncEnumerable<TElement> source, Func<TElement, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  367. {
  368. Debug.Assert(source != null);
  369. Debug.Assert(keySelector != null);
  370. var lookup = new LookupWithTask<TKey, TElement>(comparer);
  371. var enu = source.GetAsyncEnumerator(cancellationToken);
  372. try
  373. {
  374. while (await enu.MoveNextAsync().ConfigureAwait(false))
  375. {
  376. var key = await keySelector(enu.Current).ConfigureAwait(false);
  377. lookup.GetGrouping(key, create: true).Add(enu.Current);
  378. }
  379. }
  380. finally
  381. {
  382. await enu.DisposeAsync().ConfigureAwait(false);
  383. }
  384. return lookup;
  385. }
  386. #if !NO_DEEP_CANCELLATION
  387. internal static async Task<LookupWithTask<TKey, TElement>> CreateAsync(IAsyncEnumerable<TElement> source, Func<TElement, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  388. {
  389. Debug.Assert(source != null);
  390. Debug.Assert(keySelector != null);
  391. var lookup = new LookupWithTask<TKey, TElement>(comparer);
  392. var enu = source.GetAsyncEnumerator(cancellationToken);
  393. try
  394. {
  395. while (await enu.MoveNextAsync().ConfigureAwait(false))
  396. {
  397. var key = await keySelector(enu.Current, cancellationToken).ConfigureAwait(false);
  398. lookup.GetGrouping(key, create: true).Add(enu.Current);
  399. }
  400. }
  401. finally
  402. {
  403. await enu.DisposeAsync().ConfigureAwait(false);
  404. }
  405. return lookup;
  406. }
  407. #endif
  408. internal static async Task<LookupWithTask<TKey, TElement>> CreateForJoinAsync(IAsyncEnumerable<TElement> source, Func<TElement, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  409. {
  410. var lookup = new LookupWithTask<TKey, TElement>(comparer);
  411. var enu = source.GetAsyncEnumerator(cancellationToken);
  412. try
  413. {
  414. while (await enu.MoveNextAsync().ConfigureAwait(false))
  415. {
  416. var key = await keySelector(enu.Current).ConfigureAwait(false);
  417. if (key != null)
  418. {
  419. lookup.GetGrouping(key, create: true).Add(enu.Current);
  420. }
  421. }
  422. }
  423. finally
  424. {
  425. await enu.DisposeAsync().ConfigureAwait(false);
  426. }
  427. return lookup;
  428. }
  429. #if !NO_DEEP_CANCELLATION
  430. internal static async Task<LookupWithTask<TKey, TElement>> CreateForJoinAsync(IAsyncEnumerable<TElement> source, Func<TElement, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  431. {
  432. var lookup = new LookupWithTask<TKey, TElement>(comparer);
  433. var enu = source.GetAsyncEnumerator(cancellationToken);
  434. try
  435. {
  436. while (await enu.MoveNextAsync().ConfigureAwait(false))
  437. {
  438. var key = await keySelector(enu.Current, cancellationToken).ConfigureAwait(false);
  439. if (key != null)
  440. {
  441. lookup.GetGrouping(key, create: true).Add(enu.Current);
  442. }
  443. }
  444. }
  445. finally
  446. {
  447. await enu.DisposeAsync().ConfigureAwait(false);
  448. }
  449. return lookup;
  450. }
  451. #endif
  452. internal Grouping<TKey, TElement> GetGrouping(TKey key, bool create)
  453. {
  454. var hashCode = InternalGetHashCode(key);
  455. for (var g = _groupings[hashCode % _groupings.Length]; g != null; g = g._hashNext)
  456. {
  457. if (g._hashCode == hashCode && _comparer.Equals(g._key, key))
  458. {
  459. return g;
  460. }
  461. }
  462. if (create)
  463. {
  464. if (Count == _groupings.Length)
  465. {
  466. Resize();
  467. }
  468. var index = hashCode % _groupings.Length;
  469. var g = new Grouping<TKey, TElement>
  470. {
  471. _key = key,
  472. _hashCode = hashCode,
  473. _elements = new TElement[1],
  474. _hashNext = _groupings[index]
  475. };
  476. _groupings[index] = g;
  477. if (_lastGrouping == null)
  478. {
  479. g._next = g;
  480. }
  481. else
  482. {
  483. g._next = _lastGrouping._next;
  484. _lastGrouping._next = g;
  485. }
  486. _lastGrouping = g;
  487. Count++;
  488. return g;
  489. }
  490. return null;
  491. }
  492. internal int InternalGetHashCode(TKey key)
  493. {
  494. // Handle comparer implementations that throw when passed null
  495. return (key == null) ? 0 : _comparer.GetHashCode(key) & 0x7FFFFFFF;
  496. }
  497. internal async Task<TResult[]> ToArray<TResult>(Func<TKey, IAsyncEnumerable<TElement>, ValueTask<TResult>> resultSelector)
  498. {
  499. var array = new TResult[Count];
  500. var index = 0;
  501. var g = _lastGrouping;
  502. if (g != null)
  503. {
  504. do
  505. {
  506. g = g._next;
  507. g.Trim();
  508. array[index] = await resultSelector(g._key, g._elements.ToAsyncEnumerable()).ConfigureAwait(false);
  509. ++index;
  510. } while (g != _lastGrouping);
  511. }
  512. return array;
  513. }
  514. #if !NO_DEEP_CANCELLATION
  515. internal async Task<TResult[]> ToArray<TResult>(Func<TKey, IAsyncEnumerable<TElement>, CancellationToken, ValueTask<TResult>> resultSelector, CancellationToken cancellationToken)
  516. {
  517. var array = new TResult[Count];
  518. var index = 0;
  519. var g = _lastGrouping;
  520. if (g != null)
  521. {
  522. do
  523. {
  524. g = g._next;
  525. g.Trim();
  526. array[index] = await resultSelector(g._key, g._elements.ToAsyncEnumerable(), cancellationToken).ConfigureAwait(false);
  527. ++index;
  528. } while (g != _lastGrouping);
  529. }
  530. return array;
  531. }
  532. #endif
  533. internal async Task<List<TResult>> ToList<TResult>(Func<TKey, IAsyncEnumerable<TElement>, ValueTask<TResult>> resultSelector)
  534. {
  535. var list = new List<TResult>(Count);
  536. var g = _lastGrouping;
  537. if (g != null)
  538. {
  539. do
  540. {
  541. g = g._next;
  542. g.Trim();
  543. var result = await resultSelector(g._key, g._elements.ToAsyncEnumerable()).ConfigureAwait(false);
  544. list.Add(result);
  545. } while (g != _lastGrouping);
  546. }
  547. return list;
  548. }
  549. #if !NO_DEEP_CANCELLATION
  550. internal async Task<List<TResult>> ToList<TResult>(Func<TKey, IAsyncEnumerable<TElement>, CancellationToken, ValueTask<TResult>> resultSelector, CancellationToken cancellationToken)
  551. {
  552. var list = new List<TResult>(Count);
  553. var g = _lastGrouping;
  554. if (g != null)
  555. {
  556. do
  557. {
  558. g = g._next;
  559. g.Trim();
  560. var result = await resultSelector(g._key, g._elements.ToAsyncEnumerable(), cancellationToken).ConfigureAwait(false);
  561. list.Add(result);
  562. } while (g != _lastGrouping);
  563. }
  564. return list;
  565. }
  566. #endif
  567. private void Resize()
  568. {
  569. var newSize = checked((Count * 2) + 1);
  570. var newGroupings = new Grouping<TKey, TElement>[newSize];
  571. var g = _lastGrouping;
  572. do
  573. {
  574. g = g._next;
  575. var index = g._hashCode % newSize;
  576. g._hashNext = newGroupings[index];
  577. newGroupings[index] = g;
  578. } while (g != _lastGrouping);
  579. _groupings = newGroupings;
  580. }
  581. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  582. {
  583. return new ValueTask<int>(Count);
  584. }
  585. IAsyncEnumerator<IAsyncGrouping<TKey, TElement>> IAsyncEnumerable<IAsyncGrouping<TKey, TElement>>.GetAsyncEnumerator(CancellationToken cancellationToken)
  586. {
  587. cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior.
  588. return Enumerable.Cast<IAsyncGrouping<TKey, TElement>>(this).ToAsyncEnumerable().GetAsyncEnumerator(cancellationToken);
  589. }
  590. ValueTask<List<IAsyncGrouping<TKey, TElement>>> IAsyncIListProvider<IAsyncGrouping<TKey, TElement>>.ToListAsync(CancellationToken cancellationToken)
  591. {
  592. var list = new List<IAsyncGrouping<TKey, TElement>>(Count);
  593. var g = _lastGrouping;
  594. if (g != null)
  595. {
  596. do
  597. {
  598. g = g._next;
  599. list.Add(g);
  600. }
  601. while (g != _lastGrouping);
  602. }
  603. return new ValueTask<List<IAsyncGrouping<TKey, TElement>>>(list);
  604. }
  605. ValueTask<IAsyncGrouping<TKey, TElement>[]> IAsyncIListProvider<IAsyncGrouping<TKey, TElement>>.ToArrayAsync(CancellationToken cancellationToken)
  606. {
  607. var array = new IAsyncGrouping<TKey, TElement>[Count];
  608. var index = 0;
  609. var g = _lastGrouping;
  610. if (g != null)
  611. {
  612. do
  613. {
  614. g = g._next;
  615. array[index] = g;
  616. ++index;
  617. }
  618. while (g != _lastGrouping);
  619. }
  620. return new ValueTask<IAsyncGrouping<TKey, TElement>[]>(array);
  621. }
  622. }
  623. }