ToLookup.cs 30 KB

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