Lookup.cs 24 KB

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