Lookup.cs 25 KB

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