Lookup.cs 25 KB

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