ToLookup.cs 30 KB

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