ToLookup.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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>, IIListProvider<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(CancellationToken))
  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();
  248. try
  249. {
  250. while (await enu.MoveNextAsync(cancellationToken).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(CancellationToken))
  265. {
  266. Debug.Assert(source != null);
  267. Debug.Assert(keySelector != null);
  268. var lookup = new Lookup<TKey, TElement>(comparer);
  269. var enu = source.GetAsyncEnumerator();
  270. try
  271. {
  272. while (await enu.MoveNextAsync(cancellationToken).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)
  285. {
  286. var lookup = new Lookup<TKey, TElement>(comparer);
  287. var enu = source.GetAsyncEnumerator();
  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()
  402. {
  403. return Enumerable.Cast<IAsyncGrouping<TKey, TElement>>(this).ToAsyncEnumerable().GetAsyncEnumerator();
  404. }
  405. Task<List<IAsyncGrouping<TKey, TElement>>> IIListProvider<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>[]> IIListProvider<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>, IIListProvider<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. public IEnumerable<TResult> ApplyResultSelector<TResult>(Func<TKey, IAsyncEnumerable<TElement>, TResult> resultSelector)
  486. {
  487. var g = _lastGrouping;
  488. if (g != null)
  489. {
  490. do
  491. {
  492. g = g._next;
  493. g.Trim();
  494. var result = resultSelector(g._key, g._elements.ToAsyncEnumerable());
  495. yield return result;
  496. } while (g != _lastGrouping);
  497. }
  498. }
  499. 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(CancellationToken))
  500. {
  501. Debug.Assert(source != null);
  502. Debug.Assert(keySelector != null);
  503. Debug.Assert(elementSelector != null);
  504. var lookup = new LookupWithTask<TKey, TElement>(comparer);
  505. var enu = source.GetAsyncEnumerator();
  506. try
  507. {
  508. while (await enu.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  509. {
  510. var key = await keySelector(enu.Current).ConfigureAwait(false);
  511. var group = lookup.GetGrouping(key, create: true);
  512. var element = await elementSelector(enu.Current).ConfigureAwait(false);
  513. group.Add(element);
  514. }
  515. }
  516. finally
  517. {
  518. await enu.DisposeAsync().ConfigureAwait(false);
  519. }
  520. return lookup;
  521. }
  522. internal static async Task<LookupWithTask<TKey, TElement>> CreateAsync(IAsyncEnumerable<TElement> source, Func<TElement, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default(CancellationToken))
  523. {
  524. Debug.Assert(source != null);
  525. Debug.Assert(keySelector != null);
  526. var lookup = new LookupWithTask<TKey, TElement>(comparer);
  527. var enu = source.GetAsyncEnumerator();
  528. try
  529. {
  530. while (await enu.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  531. {
  532. var key = await keySelector(enu.Current).ConfigureAwait(false);
  533. lookup.GetGrouping(key, create: true).Add(enu.Current);
  534. }
  535. }
  536. finally
  537. {
  538. await enu.DisposeAsync().ConfigureAwait(false);
  539. }
  540. return lookup;
  541. }
  542. internal static async Task<LookupWithTask<TKey, TElement>> CreateForJoinAsync(IAsyncEnumerable<TElement> source, Func<TElement, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  543. {
  544. var lookup = new LookupWithTask<TKey, TElement>(comparer);
  545. var enu = source.GetAsyncEnumerator();
  546. try
  547. {
  548. while (await enu.MoveNextAsync().ConfigureAwait(false))
  549. {
  550. var key = await keySelector(enu.Current).ConfigureAwait(false);
  551. if (key != null)
  552. {
  553. lookup.GetGrouping(key, create: true).Add(enu.Current);
  554. }
  555. }
  556. }
  557. finally
  558. {
  559. await enu.DisposeAsync().ConfigureAwait(false);
  560. }
  561. return lookup;
  562. }
  563. internal Grouping<TKey, TElement> GetGrouping(TKey key, bool create)
  564. {
  565. var hashCode = InternalGetHashCode(key);
  566. for (var g = _groupings[hashCode % _groupings.Length]; g != null; g = g._hashNext)
  567. {
  568. if (g._hashCode == hashCode && _comparer.Equals(g._key, key))
  569. {
  570. return g;
  571. }
  572. }
  573. if (create)
  574. {
  575. if (Count == _groupings.Length)
  576. {
  577. Resize();
  578. }
  579. var index = hashCode % _groupings.Length;
  580. var g = new Grouping<TKey, TElement>
  581. {
  582. _key = key,
  583. _hashCode = hashCode,
  584. _elements = new TElement[1],
  585. _hashNext = _groupings[index]
  586. };
  587. _groupings[index] = g;
  588. if (_lastGrouping == null)
  589. {
  590. g._next = g;
  591. }
  592. else
  593. {
  594. g._next = _lastGrouping._next;
  595. _lastGrouping._next = g;
  596. }
  597. _lastGrouping = g;
  598. Count++;
  599. return g;
  600. }
  601. return null;
  602. }
  603. internal int InternalGetHashCode(TKey key)
  604. {
  605. // Handle comparer implementations that throw when passed null
  606. return (key == null) ? 0 : _comparer.GetHashCode(key) & 0x7FFFFFFF;
  607. }
  608. internal async Task<TResult[]> ToArray<TResult>(Func<TKey, IAsyncEnumerable<TElement>, Task<TResult>> resultSelector)
  609. {
  610. var array = new TResult[Count];
  611. var index = 0;
  612. var g = _lastGrouping;
  613. if (g != null)
  614. {
  615. do
  616. {
  617. g = g._next;
  618. g.Trim();
  619. array[index] = await resultSelector(g._key, g._elements.ToAsyncEnumerable()).ConfigureAwait(false);
  620. ++index;
  621. } while (g != _lastGrouping);
  622. }
  623. return array;
  624. }
  625. internal async Task<List<TResult>> ToList<TResult>(Func<TKey, IAsyncEnumerable<TElement>, Task<TResult>> resultSelector)
  626. {
  627. var list = new List<TResult>(Count);
  628. var g = _lastGrouping;
  629. if (g != null)
  630. {
  631. do
  632. {
  633. g = g._next;
  634. g.Trim();
  635. var result = await resultSelector(g._key, g._elements.ToAsyncEnumerable()).ConfigureAwait(false);
  636. list.Add(result);
  637. } while (g != _lastGrouping);
  638. }
  639. return list;
  640. }
  641. private void Resize()
  642. {
  643. var newSize = checked((Count * 2) + 1);
  644. var newGroupings = new Grouping<TKey, TElement>[newSize];
  645. var g = _lastGrouping;
  646. do
  647. {
  648. g = g._next;
  649. var index = g._hashCode % newSize;
  650. g._hashNext = newGroupings[index];
  651. newGroupings[index] = g;
  652. } while (g != _lastGrouping);
  653. _groupings = newGroupings;
  654. }
  655. public Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  656. {
  657. return Task.FromResult(Count);
  658. }
  659. IAsyncEnumerator<IAsyncGrouping<TKey, TElement>> IAsyncEnumerable<IAsyncGrouping<TKey, TElement>>.GetAsyncEnumerator()
  660. {
  661. return Enumerable.Cast<IAsyncGrouping<TKey, TElement>>(this).ToAsyncEnumerable().GetAsyncEnumerator();
  662. }
  663. Task<List<IAsyncGrouping<TKey, TElement>>> IIListProvider<IAsyncGrouping<TKey, TElement>>.ToListAsync(CancellationToken cancellationToken)
  664. {
  665. var list = new List<IAsyncGrouping<TKey, TElement>>(Count);
  666. var g = _lastGrouping;
  667. if (g != null)
  668. {
  669. do
  670. {
  671. g = g._next;
  672. list.Add(g);
  673. }
  674. while (g != _lastGrouping);
  675. }
  676. return Task.FromResult(list);
  677. }
  678. Task<IAsyncGrouping<TKey, TElement>[]> IIListProvider<IAsyncGrouping<TKey, TElement>>.ToArrayAsync(CancellationToken cancellationToken)
  679. {
  680. var array = new IAsyncGrouping<TKey, TElement>[Count];
  681. var index = 0;
  682. var g = _lastGrouping;
  683. if (g != null)
  684. {
  685. do
  686. {
  687. g = g._next;
  688. array[index] = g;
  689. ++index;
  690. }
  691. while (g != _lastGrouping);
  692. }
  693. return Task.FromResult(array);
  694. }
  695. }
  696. }