1
0

OrderedAsyncEnumerable.cs 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. // NB: Large portions of the implementation are based on https://github.com/dotnet/corefx/blob/master/src/System.Linq/src/System/Linq/OrderedEnumerable.cs.
  11. internal abstract class OrderedAsyncEnumerable<TElement> : AsyncIterator<TElement>, IOrderedAsyncEnumerable<TElement>, IAsyncPartition<TElement>
  12. {
  13. protected readonly IAsyncEnumerable<TElement> _source;
  14. private TElement[]? _buffer;
  15. private int[]? _indexes;
  16. private int _index;
  17. protected OrderedAsyncEnumerable(IAsyncEnumerable<TElement> source)
  18. {
  19. _source = source ?? throw Error.ArgumentNull(nameof(source));
  20. }
  21. IOrderedAsyncEnumerable<TElement> IOrderedAsyncEnumerable<TElement>.CreateOrderedEnumerable<TKey>(Func<TElement, TKey> keySelector, IComparer<TKey>? comparer, bool descending)
  22. {
  23. return new OrderedAsyncEnumerable<TElement, TKey>(_source, keySelector, comparer, descending, this);
  24. }
  25. IOrderedAsyncEnumerable<TElement> IOrderedAsyncEnumerable<TElement>.CreateOrderedEnumerable<TKey>(Func<TElement, ValueTask<TKey>> keySelector, IComparer<TKey>? comparer, bool descending)
  26. {
  27. return new OrderedAsyncEnumerableWithTask<TElement, TKey>(_source, keySelector, comparer, descending, this);
  28. }
  29. IOrderedAsyncEnumerable<TElement> IOrderedAsyncEnumerable<TElement>.CreateOrderedEnumerable<TKey>(Func<TElement, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey>? comparer, bool descending)
  30. {
  31. return new OrderedAsyncEnumerableWithTaskAndCancellation<TElement, TKey>(_source, keySelector, comparer, descending, this);
  32. }
  33. protected override async ValueTask<bool> MoveNextCore()
  34. {
  35. switch (_state)
  36. {
  37. case AsyncIteratorState.Allocated:
  38. _buffer = await _source.ToArrayAsync(_cancellationToken).ConfigureAwait(false); // TODO: Use buffer.
  39. var sorter = GetAsyncEnumerableSorter(_cancellationToken);
  40. _indexes = await sorter.Sort(_buffer, _buffer.Length).ConfigureAwait(false);
  41. _index = 0;
  42. _state = AsyncIteratorState.Iterating;
  43. goto case AsyncIteratorState.Iterating;
  44. case AsyncIteratorState.Iterating:
  45. if (_index < _buffer!.Length)
  46. {
  47. _current = _buffer[_indexes![_index++]];
  48. return true;
  49. }
  50. await DisposeAsync().ConfigureAwait(false);
  51. break;
  52. }
  53. return false;
  54. }
  55. public override async ValueTask DisposeAsync()
  56. {
  57. _buffer = null;
  58. _indexes = null;
  59. await base.DisposeAsync().ConfigureAwait(false);
  60. }
  61. internal abstract AsyncEnumerableSorter<TElement> GetAsyncEnumerableSorter(AsyncEnumerableSorter<TElement>? next, CancellationToken cancellationToken);
  62. internal AsyncEnumerableSorter<TElement> GetAsyncEnumerableSorter(CancellationToken cancellationToken) => GetAsyncEnumerableSorter(next: null, cancellationToken);
  63. public async ValueTask<TElement[]> ToArrayAsync(CancellationToken cancellationToken)
  64. {
  65. var elements = await AsyncEnumerableHelpers.ToArrayWithLength(_source, cancellationToken).ConfigureAwait(false);
  66. var count = elements.Length;
  67. if (count == 0)
  68. {
  69. #if NO_ARRAY_EMPTY
  70. return EmptyArray<TElement>.Value;
  71. #else
  72. return Array.Empty<TElement>();
  73. #endif
  74. }
  75. var array = elements.Array;
  76. var map = await SortedMap(array, count, cancellationToken).ConfigureAwait(false);
  77. var result = new TElement[count];
  78. for (var i = 0; i < result.Length; i++)
  79. {
  80. result[i] = array[map[i]];
  81. }
  82. return result;
  83. }
  84. internal async ValueTask<TElement[]> ToArrayAsync(int minIndexInclusive, int maxIndexInclusive, CancellationToken cancellationToken)
  85. {
  86. var elements = await AsyncEnumerableHelpers.ToArrayWithLength(_source, cancellationToken).ConfigureAwait(false);
  87. var count = elements.Length;
  88. if (count <= minIndexInclusive)
  89. {
  90. #if NO_ARRAY_EMPTY
  91. return EmptyArray<TElement>.Value;
  92. #else
  93. return Array.Empty<TElement>();
  94. #endif
  95. }
  96. if (count <= maxIndexInclusive)
  97. {
  98. maxIndexInclusive = count - 1;
  99. }
  100. var array = elements.Array;
  101. if (minIndexInclusive == maxIndexInclusive)
  102. {
  103. var sorter = GetAsyncEnumerableSorter(cancellationToken);
  104. var element = await sorter.ElementAt(array, count, minIndexInclusive).ConfigureAwait(false);
  105. return new TElement[] { element };
  106. }
  107. var map = await SortedMap(array, count, minIndexInclusive, maxIndexInclusive, cancellationToken).ConfigureAwait(false);
  108. var result = new TElement[maxIndexInclusive - minIndexInclusive + 1];
  109. for (var i = 0; minIndexInclusive <= maxIndexInclusive; i++)
  110. {
  111. result[i] = array[map[minIndexInclusive++]];
  112. }
  113. return result;
  114. }
  115. public async ValueTask<List<TElement>> ToListAsync(CancellationToken cancellationToken)
  116. {
  117. var elements = await AsyncEnumerableHelpers.ToArrayWithLength(_source, cancellationToken).ConfigureAwait(false);
  118. var count = elements.Length;
  119. if (count == 0)
  120. {
  121. return new List<TElement>(capacity: 0);
  122. }
  123. var array = elements.Array;
  124. var map = await SortedMap(array, count, cancellationToken).ConfigureAwait(false);
  125. var result = new List<TElement>(count);
  126. for (var i = 0; i < count; i++)
  127. {
  128. result.Add(array[map[i]]);
  129. }
  130. return result;
  131. }
  132. internal async ValueTask<List<TElement>> ToListAsync(int minIndexInclusive, int maxIndexInclusive, CancellationToken cancellationToken)
  133. {
  134. var elements = await AsyncEnumerableHelpers.ToArrayWithLength(_source, cancellationToken).ConfigureAwait(false);
  135. var count = elements.Length;
  136. if (count <= minIndexInclusive)
  137. {
  138. return new List<TElement>(0);
  139. }
  140. if (count <= maxIndexInclusive)
  141. {
  142. maxIndexInclusive = count - 1;
  143. }
  144. var array = elements.Array;
  145. if (minIndexInclusive == maxIndexInclusive)
  146. {
  147. var sorter = GetAsyncEnumerableSorter(cancellationToken);
  148. var element = await sorter.ElementAt(array, count, minIndexInclusive).ConfigureAwait(false);
  149. return new List<TElement>(1) { element };
  150. }
  151. var map = await SortedMap(array, count, minIndexInclusive, maxIndexInclusive, cancellationToken).ConfigureAwait(false);
  152. var list = new List<TElement>(maxIndexInclusive - minIndexInclusive + 1);
  153. while (minIndexInclusive <= maxIndexInclusive)
  154. {
  155. list.Add(array[map[minIndexInclusive++]]);
  156. }
  157. return list;
  158. }
  159. public async ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  160. {
  161. if (_source is IAsyncIListProvider<TElement> listProv)
  162. {
  163. var count = await listProv.GetCountAsync(onlyIfCheap, cancellationToken).ConfigureAwait(false);
  164. return count;
  165. }
  166. return !onlyIfCheap || _source is ICollection<TElement> || _source is ICollection ? await _source.CountAsync(cancellationToken).ConfigureAwait(false) : -1;
  167. }
  168. internal async ValueTask<int> GetCountAsync(int minIndexInclusive, int maxIndexInclusive, bool onlyIfCheap, CancellationToken cancellationToken)
  169. {
  170. var count = await GetCountAsync(onlyIfCheap, cancellationToken).ConfigureAwait(false);
  171. if (count <= 0)
  172. {
  173. return count;
  174. }
  175. if (count <= minIndexInclusive)
  176. {
  177. return 0;
  178. }
  179. return (count <= maxIndexInclusive ? count : maxIndexInclusive + 1) - minIndexInclusive;
  180. }
  181. private ValueTask<int[]> SortedMap(TElement[] elements, int count, CancellationToken cancellationToken)
  182. {
  183. var sorter = GetAsyncEnumerableSorter(cancellationToken);
  184. return sorter.Sort(elements, count);
  185. }
  186. private ValueTask<int[]> SortedMap(TElement[] elements, int count, int minIndexInclusive, int maxIndexInclusive, CancellationToken cancellationToken)
  187. {
  188. var sorter = GetAsyncEnumerableSorter(cancellationToken);
  189. return sorter.Sort(elements, count, minIndexInclusive, maxIndexInclusive);
  190. }
  191. private AsyncCachingComparer<TElement> GetComparer() => GetComparer(childComparer: null);
  192. internal abstract AsyncCachingComparer<TElement> GetComparer(AsyncCachingComparer<TElement>? childComparer);
  193. public async ValueTask<Maybe<TElement>> TryGetFirstAsync(CancellationToken cancellationToken)
  194. {
  195. await using var e = _source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  196. if (!await e.MoveNextAsync())
  197. {
  198. return new Maybe<TElement>();
  199. }
  200. var value = e.Current;
  201. var comparer = GetComparer();
  202. await comparer.SetElement(value, cancellationToken).ConfigureAwait(false);
  203. while (await e.MoveNextAsync())
  204. {
  205. var x = e.Current;
  206. if (await comparer.Compare(x, cacheLower: true, cancellationToken).ConfigureAwait(false) < 0)
  207. {
  208. value = x;
  209. }
  210. }
  211. return new Maybe<TElement>(value);
  212. }
  213. public async ValueTask<Maybe<TElement>> TryGetLastAsync(CancellationToken cancellationToken)
  214. {
  215. await using var e = _source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  216. if (!await e.MoveNextAsync())
  217. {
  218. return new Maybe<TElement>();
  219. }
  220. var value = e.Current;
  221. var comparer = GetComparer();
  222. await comparer.SetElement(value, cancellationToken).ConfigureAwait(false);
  223. while (await e.MoveNextAsync())
  224. {
  225. var current = e.Current;
  226. if (await comparer.Compare(current, cacheLower: false, cancellationToken).ConfigureAwait(false) >= 0)
  227. {
  228. value = current;
  229. }
  230. }
  231. return new Maybe<TElement>(value);
  232. }
  233. internal async ValueTask<Maybe<TElement>> TryGetLastAsync(int minIndexInclusive, int maxIndexInclusive, CancellationToken cancellationToken)
  234. {
  235. var elements = await AsyncEnumerableHelpers.ToArrayWithLength(_source, cancellationToken).ConfigureAwait(false);
  236. var count = elements.Length;
  237. if (minIndexInclusive >= count)
  238. {
  239. return new Maybe<TElement>();
  240. }
  241. var array = elements.Array;
  242. TElement last;
  243. if (maxIndexInclusive < count - 1)
  244. {
  245. var sorter = GetAsyncEnumerableSorter(cancellationToken);
  246. last = await sorter.ElementAt(array, count, maxIndexInclusive).ConfigureAwait(false);
  247. }
  248. else
  249. {
  250. last = await Last(array, count, cancellationToken).ConfigureAwait(false);
  251. }
  252. return new Maybe<TElement>(last);
  253. }
  254. private async ValueTask<TElement> Last(TElement[] items, int count, CancellationToken cancellationToken)
  255. {
  256. var value = items[0];
  257. var comparer = GetComparer();
  258. await comparer.SetElement(value, cancellationToken).ConfigureAwait(false);
  259. for (var i = 1; i != count; ++i)
  260. {
  261. var x = items[i];
  262. if (await comparer.Compare(x, cacheLower: false, cancellationToken).ConfigureAwait(false) >= 0)
  263. {
  264. value = x;
  265. }
  266. }
  267. return value;
  268. }
  269. public IAsyncPartition<TElement> Skip(int count) => new OrderedAsyncPartition<TElement>(this, count, int.MaxValue);
  270. public IAsyncPartition<TElement> Take(int count) => new OrderedAsyncPartition<TElement>(this, 0, count - 1);
  271. public ValueTask<Maybe<TElement>> TryGetElementAtAsync(int index, CancellationToken cancellationToken)
  272. {
  273. if (index == 0)
  274. {
  275. return TryGetFirstAsync(cancellationToken);
  276. }
  277. if (index > 0)
  278. {
  279. return Core();
  280. async ValueTask<Maybe<TElement>> Core()
  281. {
  282. var elements = await AsyncEnumerableHelpers.ToArrayWithLength(_source, cancellationToken).ConfigureAwait(false);
  283. var count = elements.Length;
  284. if (index < count)
  285. {
  286. var sorter = GetAsyncEnumerableSorter(cancellationToken);
  287. var element = await sorter.ElementAt(elements.Array, count, index).ConfigureAwait(false);
  288. return new Maybe<TElement>(element);
  289. }
  290. return new Maybe<TElement>();
  291. }
  292. }
  293. return new ValueTask<Maybe<TElement>>(new Maybe<TElement>());
  294. }
  295. }
  296. internal sealed class OrderedAsyncEnumerable<TElement, TKey> : OrderedAsyncEnumerable<TElement>
  297. {
  298. private readonly IComparer<TKey> _comparer;
  299. private readonly bool _descending;
  300. private readonly Func<TElement, TKey> _keySelector;
  301. private readonly OrderedAsyncEnumerable<TElement>? _parent;
  302. public OrderedAsyncEnumerable(IAsyncEnumerable<TElement> source, Func<TElement, TKey> keySelector, IComparer<TKey>? comparer, bool descending, OrderedAsyncEnumerable<TElement>? parent)
  303. : base(source)
  304. {
  305. _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector));
  306. _comparer = comparer ?? Comparer<TKey>.Default;
  307. _descending = descending;
  308. _parent = parent;
  309. }
  310. public override AsyncIteratorBase<TElement> Clone()
  311. {
  312. return new OrderedAsyncEnumerable<TElement, TKey>(_source, _keySelector, _comparer, _descending, _parent);
  313. }
  314. internal override AsyncEnumerableSorter<TElement> GetAsyncEnumerableSorter(AsyncEnumerableSorter<TElement>? next, CancellationToken cancellationToken)
  315. {
  316. var sorter = new SyncKeySelectorAsyncEnumerableSorter<TElement, TKey>(_keySelector, _comparer, _descending, next);
  317. if (_parent != null)
  318. {
  319. return _parent.GetAsyncEnumerableSorter(sorter, cancellationToken);
  320. }
  321. return sorter;
  322. }
  323. internal override AsyncCachingComparer<TElement> GetComparer(AsyncCachingComparer<TElement>? childComparer)
  324. {
  325. AsyncCachingComparer<TElement> cmp = childComparer == null
  326. ? new AsyncCachingComparer<TElement, TKey>(_keySelector, _comparer, _descending)
  327. : new AsyncCachingComparerWithChild<TElement, TKey>(_keySelector, _comparer, _descending, childComparer);
  328. return _parent != null ? _parent.GetComparer(cmp) : cmp;
  329. }
  330. }
  331. internal sealed class OrderedAsyncEnumerableWithTask<TElement, TKey> : OrderedAsyncEnumerable<TElement>
  332. {
  333. private readonly IComparer<TKey> _comparer;
  334. private readonly bool _descending;
  335. private readonly Func<TElement, ValueTask<TKey>> _keySelector;
  336. private readonly OrderedAsyncEnumerable<TElement>? _parent;
  337. public OrderedAsyncEnumerableWithTask(IAsyncEnumerable<TElement> source, Func<TElement, ValueTask<TKey>> keySelector, IComparer<TKey>? comparer, bool descending, OrderedAsyncEnumerable<TElement>? parent)
  338. : base(source)
  339. {
  340. _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector));
  341. _comparer = comparer ?? Comparer<TKey>.Default;
  342. _descending = descending;
  343. _parent = parent;
  344. }
  345. public override AsyncIteratorBase<TElement> Clone()
  346. {
  347. return new OrderedAsyncEnumerableWithTask<TElement, TKey>(_source, _keySelector, _comparer, _descending, _parent);
  348. }
  349. internal override AsyncEnumerableSorter<TElement> GetAsyncEnumerableSorter(AsyncEnumerableSorter<TElement>? next, CancellationToken cancellationToken)
  350. {
  351. var sorter = new AsyncKeySelectorAsyncEnumerableSorter<TElement, TKey>(_keySelector, _comparer, _descending, next);
  352. if (_parent != null)
  353. {
  354. return _parent.GetAsyncEnumerableSorter(sorter, cancellationToken);
  355. }
  356. return sorter;
  357. }
  358. internal override AsyncCachingComparer<TElement> GetComparer(AsyncCachingComparer<TElement>? childComparer)
  359. {
  360. AsyncCachingComparer<TElement> cmp = childComparer == null
  361. ? new AsyncCachingComparerWithTask<TElement, TKey>(_keySelector, _comparer, _descending)
  362. : new AsyncCachingComparerWithTaskAndChild<TElement, TKey>(_keySelector, _comparer, _descending, childComparer);
  363. return _parent != null ? _parent.GetComparer(cmp) : cmp;
  364. }
  365. }
  366. #if !NO_DEEP_CANCELLATION
  367. internal sealed class OrderedAsyncEnumerableWithTaskAndCancellation<TElement, TKey> : OrderedAsyncEnumerable<TElement>
  368. {
  369. private readonly IComparer<TKey> _comparer;
  370. private readonly bool _descending;
  371. private readonly Func<TElement, CancellationToken, ValueTask<TKey>> _keySelector;
  372. private readonly OrderedAsyncEnumerable<TElement>? _parent;
  373. public OrderedAsyncEnumerableWithTaskAndCancellation(IAsyncEnumerable<TElement> source, Func<TElement, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey>? comparer, bool descending, OrderedAsyncEnumerable<TElement>? parent)
  374. : base(source)
  375. {
  376. _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector));
  377. _comparer = comparer ?? Comparer<TKey>.Default;
  378. _descending = descending;
  379. _parent = parent;
  380. }
  381. public override AsyncIteratorBase<TElement> Clone()
  382. {
  383. return new OrderedAsyncEnumerableWithTaskAndCancellation<TElement, TKey>(_source, _keySelector, _comparer, _descending, _parent);
  384. }
  385. internal override AsyncEnumerableSorter<TElement> GetAsyncEnumerableSorter(AsyncEnumerableSorter<TElement>? next, CancellationToken cancellationToken)
  386. {
  387. var sorter = new AsyncKeySelectorAsyncEnumerableSorterWithCancellation<TElement, TKey>(_keySelector, _comparer, _descending, next, cancellationToken);
  388. if (_parent != null)
  389. {
  390. return _parent.GetAsyncEnumerableSorter(sorter, cancellationToken);
  391. }
  392. return sorter;
  393. }
  394. internal override AsyncCachingComparer<TElement> GetComparer(AsyncCachingComparer<TElement>? childComparer)
  395. {
  396. AsyncCachingComparer<TElement> cmp = childComparer == null
  397. ? new AsyncCachingComparerWithTaskAndCancellation<TElement, TKey>(_keySelector, _comparer, _descending)
  398. : new AsyncCachingComparerWithTaskAndCancellationAndChild<TElement, TKey>(_keySelector, _comparer, _descending, childComparer);
  399. return _parent != null ? _parent.GetComparer(cmp) : cmp;
  400. }
  401. }
  402. #endif
  403. internal abstract class AsyncEnumerableSorter<TElement>
  404. {
  405. internal abstract ValueTask ComputeKeys(TElement[] elements, int count);
  406. internal abstract int CompareAnyKeys(int index1, int index2);
  407. public async ValueTask<int[]> Sort(TElement[] elements, int count)
  408. {
  409. var map = await ComputeMap(elements, count).ConfigureAwait(false);
  410. QuickSort(map, 0, count - 1);
  411. return map;
  412. }
  413. public async ValueTask<int[]> Sort(TElement[] elements, int count, int minIndexInclusive, int maxIndexInclusive)
  414. {
  415. var map = await ComputeMap(elements, count).ConfigureAwait(false);
  416. PartialQuickSort(map, 0, count - 1, minIndexInclusive, maxIndexInclusive);
  417. return map;
  418. }
  419. public async ValueTask<TElement> ElementAt(TElement[] elements, int count, int index)
  420. {
  421. var map = await ComputeMap(elements, count).ConfigureAwait(false);
  422. return index == 0 ?
  423. elements[Min(map, count)] :
  424. elements[QuickSelect(map, count - 1, index)];
  425. }
  426. private async ValueTask<int[]> ComputeMap(TElement[] elements, int count)
  427. {
  428. await ComputeKeys(elements, count).ConfigureAwait(false);
  429. var map = new int[count];
  430. for (var i = 0; i < count; i++)
  431. {
  432. map[i] = i;
  433. }
  434. return map;
  435. }
  436. protected abstract void QuickSort(int[] map, int left, int right);
  437. protected abstract void PartialQuickSort(int[] map, int left, int right, int minIndexInclusive, int maxIndexInclusive);
  438. protected abstract int QuickSelect(int[] map, int right, int idx);
  439. protected abstract int Min(int[] map, int count);
  440. }
  441. internal abstract class AsyncEnumerableSorterBase<TElement, TKey> : AsyncEnumerableSorter<TElement>
  442. {
  443. private readonly IComparer<TKey> _comparer;
  444. private readonly bool _descending;
  445. protected readonly AsyncEnumerableSorter<TElement>? _next;
  446. protected TKey[]? _keys;
  447. public AsyncEnumerableSorterBase(IComparer<TKey> comparer, bool descending, AsyncEnumerableSorter<TElement>? next)
  448. {
  449. _comparer = comparer;
  450. _descending = descending;
  451. _next = next;
  452. }
  453. internal sealed override int CompareAnyKeys(int index1, int index2)
  454. {
  455. var c = _comparer.Compare(_keys![index1], _keys[index2]); // NB: _keys is assigned before calling this method.
  456. if (c == 0)
  457. {
  458. return _next == null ? index1 - index2 : _next.CompareAnyKeys(index1, index2);
  459. }
  460. else
  461. {
  462. return (_descending != (c > 0)) ? 1 : -1;
  463. }
  464. }
  465. private int CompareKeys(int index1, int index2) => index1 == index2 ? 0 : CompareAnyKeys(index1, index2);
  466. protected override void QuickSort(int[] keys, int lo, int hi) => Array.Sort(keys, lo, hi - lo + 1, Comparer<int>.Create(CompareAnyKeys));
  467. protected override void PartialQuickSort(int[] map, int left, int right, int minIndexInclusive, int maxIndexInclusive)
  468. {
  469. do
  470. {
  471. var i = left;
  472. var j = right;
  473. var x = map[i + ((j - i) >> 1)];
  474. do
  475. {
  476. while (i < map.Length && CompareKeys(x, map[i]) > 0)
  477. {
  478. i++;
  479. }
  480. while (j >= 0 && CompareKeys(x, map[j]) < 0)
  481. {
  482. j--;
  483. }
  484. if (i > j)
  485. {
  486. break;
  487. }
  488. if (i < j)
  489. {
  490. var temp = map[i];
  491. map[i] = map[j];
  492. map[j] = temp;
  493. }
  494. i++;
  495. j--;
  496. }
  497. while (i <= j);
  498. if (minIndexInclusive >= i)
  499. {
  500. left = i + 1;
  501. }
  502. else if (maxIndexInclusive <= j)
  503. {
  504. right = j - 1;
  505. }
  506. if (j - left <= right - i)
  507. {
  508. if (left < j)
  509. {
  510. PartialQuickSort(map, left, j, minIndexInclusive, maxIndexInclusive);
  511. }
  512. left = i;
  513. }
  514. else
  515. {
  516. if (i < right)
  517. {
  518. PartialQuickSort(map, i, right, minIndexInclusive, maxIndexInclusive);
  519. }
  520. right = j;
  521. }
  522. }
  523. while (left < right);
  524. }
  525. protected override int QuickSelect(int[] map, int right, int idx)
  526. {
  527. var left = 0;
  528. do
  529. {
  530. var i = left;
  531. var j = right;
  532. var x = map[i + ((j - i) >> 1)];
  533. do
  534. {
  535. while (i < map.Length && CompareKeys(x, map[i]) > 0)
  536. {
  537. i++;
  538. }
  539. while (j >= 0 && CompareKeys(x, map[j]) < 0)
  540. {
  541. j--;
  542. }
  543. if (i > j)
  544. {
  545. break;
  546. }
  547. if (i < j)
  548. {
  549. var temp = map[i];
  550. map[i] = map[j];
  551. map[j] = temp;
  552. }
  553. i++;
  554. j--;
  555. }
  556. while (i <= j);
  557. if (i <= idx)
  558. {
  559. left = i + 1;
  560. }
  561. else
  562. {
  563. right = j - 1;
  564. }
  565. if (j - left <= right - i)
  566. {
  567. if (left < j)
  568. {
  569. right = j;
  570. }
  571. left = i;
  572. }
  573. else
  574. {
  575. if (i < right)
  576. {
  577. left = i;
  578. }
  579. right = j;
  580. }
  581. }
  582. while (left < right);
  583. return map[idx];
  584. }
  585. protected override int Min(int[] map, int count)
  586. {
  587. var index = 0;
  588. for (var i = 1; i < count; i++)
  589. {
  590. if (CompareKeys(map[i], map[index]) < 0)
  591. {
  592. index = i;
  593. }
  594. }
  595. return map[index];
  596. }
  597. }
  598. internal sealed class SyncKeySelectorAsyncEnumerableSorter<TElement, TKey> : AsyncEnumerableSorterBase<TElement, TKey>
  599. {
  600. private readonly Func<TElement, TKey> _keySelector;
  601. public SyncKeySelectorAsyncEnumerableSorter(Func<TElement, TKey> keySelector, IComparer<TKey> comparer, bool descending, AsyncEnumerableSorter<TElement>? next)
  602. : base(comparer, descending, next)
  603. {
  604. _keySelector = keySelector;
  605. }
  606. internal override async ValueTask ComputeKeys(TElement[] elements, int count)
  607. {
  608. _keys = new TKey[count];
  609. for (var i = 0; i < count; i++)
  610. {
  611. _keys[i] = _keySelector(elements[i]);
  612. }
  613. if (_next != null)
  614. {
  615. await _next.ComputeKeys(elements, count).ConfigureAwait(false);
  616. }
  617. }
  618. }
  619. internal sealed class AsyncKeySelectorAsyncEnumerableSorter<TElement, TKey> : AsyncEnumerableSorterBase<TElement, TKey>
  620. {
  621. private readonly Func<TElement, ValueTask<TKey>> _keySelector;
  622. public AsyncKeySelectorAsyncEnumerableSorter(Func<TElement, ValueTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending, AsyncEnumerableSorter<TElement>? next)
  623. : base(comparer, descending, next)
  624. {
  625. _keySelector = keySelector;
  626. }
  627. internal override async ValueTask ComputeKeys(TElement[] elements, int count)
  628. {
  629. _keys = new TKey[count];
  630. for (var i = 0; i < count; i++)
  631. {
  632. _keys[i] = await _keySelector(elements[i]).ConfigureAwait(false);
  633. }
  634. if (_next != null)
  635. {
  636. await _next.ComputeKeys(elements, count).ConfigureAwait(false);
  637. }
  638. }
  639. }
  640. #if !NO_DEEP_CANCELLATION
  641. internal sealed class AsyncKeySelectorAsyncEnumerableSorterWithCancellation<TElement, TKey> : AsyncEnumerableSorterBase<TElement, TKey>
  642. {
  643. private readonly Func<TElement, CancellationToken, ValueTask<TKey>> _keySelector;
  644. private readonly CancellationToken _cancellationToken;
  645. public AsyncKeySelectorAsyncEnumerableSorterWithCancellation(Func<TElement, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending, AsyncEnumerableSorter<TElement>? next, CancellationToken cancellationToken)
  646. : base(comparer, descending, next)
  647. {
  648. _keySelector = keySelector;
  649. _cancellationToken = cancellationToken;
  650. }
  651. internal override async ValueTask ComputeKeys(TElement[] elements, int count)
  652. {
  653. _keys = new TKey[count];
  654. for (var i = 0; i < count; i++)
  655. {
  656. _keys[i] = await _keySelector(elements[i], _cancellationToken).ConfigureAwait(false);
  657. }
  658. if (_next != null)
  659. {
  660. await _next.ComputeKeys(elements, count).ConfigureAwait(false);
  661. }
  662. }
  663. }
  664. #endif
  665. internal sealed class OrderedAsyncPartition<TElement> : AsyncIterator<TElement>, IAsyncPartition<TElement>
  666. {
  667. private readonly OrderedAsyncEnumerable<TElement> _source;
  668. private readonly int _minIndexInclusive;
  669. private readonly int _maxIndexInclusive;
  670. public OrderedAsyncPartition(OrderedAsyncEnumerable<TElement> source, int minIndexInclusive, int maxIndexInclusive)
  671. {
  672. _source = source;
  673. _minIndexInclusive = minIndexInclusive;
  674. _maxIndexInclusive = maxIndexInclusive;
  675. }
  676. public override AsyncIteratorBase<TElement> Clone() => new OrderedAsyncPartition<TElement>(_source, _minIndexInclusive, _maxIndexInclusive);
  677. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken) =>
  678. _source.GetCountAsync(_minIndexInclusive, _maxIndexInclusive, onlyIfCheap, cancellationToken);
  679. public IAsyncPartition<TElement> Skip(int count)
  680. {
  681. var minIndex = unchecked(_minIndexInclusive + count);
  682. if (unchecked((uint)minIndex > (uint)_maxIndexInclusive))
  683. {
  684. return AsyncEnumerable.EmptyAsyncIterator<TElement>.Instance;
  685. }
  686. return new OrderedAsyncPartition<TElement>(_source, minIndex, _maxIndexInclusive);
  687. }
  688. public IAsyncPartition<TElement> Take(int count)
  689. {
  690. var maxIndex = unchecked(_minIndexInclusive + count - 1);
  691. if (unchecked((uint)maxIndex >= (uint)_maxIndexInclusive))
  692. {
  693. return this;
  694. }
  695. return new OrderedAsyncPartition<TElement>(_source, _minIndexInclusive, maxIndex);
  696. }
  697. public ValueTask<TElement[]> ToArrayAsync(CancellationToken cancellationToken) =>
  698. _source.ToArrayAsync(_minIndexInclusive, _maxIndexInclusive, cancellationToken);
  699. public ValueTask<List<TElement>> ToListAsync(CancellationToken cancellationToken) =>
  700. _source.ToListAsync(_minIndexInclusive, _maxIndexInclusive, cancellationToken);
  701. public ValueTask<Maybe<TElement>> TryGetElementAtAsync(int index, CancellationToken cancellationToken)
  702. {
  703. if (unchecked((uint)index <= (uint)(_maxIndexInclusive - _minIndexInclusive)))
  704. {
  705. return _source.TryGetElementAtAsync(index + _minIndexInclusive, cancellationToken);
  706. }
  707. return new ValueTask<Maybe<TElement>>(new Maybe<TElement>());
  708. }
  709. public ValueTask<Maybe<TElement>> TryGetFirstAsync(CancellationToken cancellationToken) =>
  710. _source.TryGetElementAtAsync(_minIndexInclusive, cancellationToken);
  711. public ValueTask<Maybe<TElement>> TryGetLastAsync(CancellationToken cancellationToken) =>
  712. _source.TryGetLastAsync(_minIndexInclusive, _maxIndexInclusive, cancellationToken);
  713. // REVIEW: Consider to tear off an iterator object rather than storing this state here?
  714. private TElement[]? _buffer;
  715. private int[]? _indexes;
  716. private int _minIndexIterator;
  717. private int _maxIndexIterator;
  718. protected override async ValueTask<bool> MoveNextCore()
  719. {
  720. switch (_state)
  721. {
  722. case AsyncIteratorState.Allocated:
  723. _buffer = await _source.ToArrayAsync(_cancellationToken).ConfigureAwait(false); // TODO: Use buffer.
  724. _minIndexIterator = _minIndexInclusive;
  725. _maxIndexIterator = _maxIndexInclusive;
  726. var count = _buffer.Length;
  727. if (count > _minIndexIterator)
  728. {
  729. if (count <= _maxIndexIterator)
  730. {
  731. _maxIndexIterator = count - 1;
  732. }
  733. var sorter = _source.GetAsyncEnumerableSorter(_cancellationToken);
  734. if (_minIndexIterator == _maxIndexIterator)
  735. {
  736. _current = await sorter.ElementAt(_buffer, _buffer.Length, _minIndexIterator).ConfigureAwait(false);
  737. _minIndexIterator = int.MaxValue;
  738. _maxIndexIterator = int.MinValue;
  739. _state = AsyncIteratorState.Iterating;
  740. return true;
  741. }
  742. else
  743. {
  744. _indexes = await sorter.Sort(_buffer, _buffer.Length, _minIndexIterator, _maxIndexIterator).ConfigureAwait(false);
  745. }
  746. _state = AsyncIteratorState.Iterating;
  747. goto case AsyncIteratorState.Iterating;
  748. }
  749. await DisposeAsync();
  750. break;
  751. case AsyncIteratorState.Iterating:
  752. if (_minIndexIterator <= _maxIndexIterator)
  753. {
  754. _current = _buffer![_indexes![_minIndexIterator++]];
  755. return true;
  756. }
  757. await DisposeAsync().ConfigureAwait(false);
  758. break;
  759. }
  760. return false;
  761. }
  762. public override async ValueTask DisposeAsync()
  763. {
  764. _buffer = null;
  765. _indexes = null;
  766. await base.DisposeAsync().ConfigureAwait(false);
  767. }
  768. }
  769. internal abstract class AsyncCachingComparer<TElement>
  770. {
  771. internal abstract ValueTask<int> Compare(TElement element, bool cacheLower, CancellationToken cancellationToken);
  772. internal abstract ValueTask SetElement(TElement element, CancellationToken cancellationToken);
  773. }
  774. internal class AsyncCachingComparer<TElement, TKey> : AsyncCachingComparer<TElement>
  775. {
  776. protected readonly Func<TElement, TKey> _keySelector;
  777. protected readonly IComparer<TKey> _comparer;
  778. protected readonly bool _descending;
  779. protected TKey _lastKey;
  780. public AsyncCachingComparer(Func<TElement, TKey> keySelector, IComparer<TKey> comparer, bool descending)
  781. {
  782. _keySelector = keySelector;
  783. _comparer = comparer;
  784. _descending = descending;
  785. _lastKey = default!;
  786. }
  787. internal override ValueTask<int> Compare(TElement element, bool cacheLower, CancellationToken cancellationToken)
  788. {
  789. var newKey = _keySelector(element);
  790. var cmp = _descending ? _comparer.Compare(_lastKey, newKey) : _comparer.Compare(newKey, _lastKey);
  791. if (cacheLower == cmp < 0)
  792. {
  793. _lastKey = newKey;
  794. }
  795. return new ValueTask<int>(cmp);
  796. }
  797. internal override ValueTask SetElement(TElement element, CancellationToken cancellationToken)
  798. {
  799. _lastKey = _keySelector(element);
  800. return new ValueTask();
  801. }
  802. }
  803. internal sealed class AsyncCachingComparerWithChild<TElement, TKey> : AsyncCachingComparer<TElement, TKey>
  804. {
  805. private readonly AsyncCachingComparer<TElement> _child;
  806. public AsyncCachingComparerWithChild(Func<TElement, TKey> keySelector, IComparer<TKey> comparer, bool descending, AsyncCachingComparer<TElement> child)
  807. : base(keySelector, comparer, descending)
  808. {
  809. _child = child;
  810. }
  811. internal override async ValueTask<int> Compare(TElement element, bool cacheLower, CancellationToken cancellationToken)
  812. {
  813. var newKey = _keySelector(element);
  814. var cmp = _descending ? _comparer.Compare(_lastKey, newKey) : _comparer.Compare(newKey, _lastKey);
  815. if (cmp == 0)
  816. {
  817. return await _child.Compare(element, cacheLower, cancellationToken).ConfigureAwait(false);
  818. }
  819. if (cacheLower == cmp < 0)
  820. {
  821. _lastKey = newKey;
  822. await _child.SetElement(element, cancellationToken).ConfigureAwait(false);
  823. }
  824. return cmp;
  825. }
  826. internal override async ValueTask SetElement(TElement element, CancellationToken cancellationToken)
  827. {
  828. await base.SetElement(element, cancellationToken).ConfigureAwait(false);
  829. await _child.SetElement(element, cancellationToken).ConfigureAwait(false);
  830. }
  831. }
  832. internal class AsyncCachingComparerWithTask<TElement, TKey> : AsyncCachingComparer<TElement>
  833. {
  834. protected readonly Func<TElement, ValueTask<TKey>> _keySelector;
  835. protected readonly IComparer<TKey> _comparer;
  836. protected readonly bool _descending;
  837. protected TKey _lastKey;
  838. public AsyncCachingComparerWithTask(Func<TElement, ValueTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending)
  839. {
  840. _keySelector = keySelector;
  841. _comparer = comparer;
  842. _descending = descending;
  843. _lastKey = default!;
  844. }
  845. internal override async ValueTask<int> Compare(TElement element, bool cacheLower, CancellationToken cancellationToken)
  846. {
  847. var newKey = await _keySelector(element).ConfigureAwait(false);
  848. var cmp = _descending ? _comparer.Compare(_lastKey, newKey) : _comparer.Compare(newKey, _lastKey);
  849. if (cacheLower == cmp < 0)
  850. {
  851. _lastKey = newKey;
  852. }
  853. return cmp;
  854. }
  855. internal override async ValueTask SetElement(TElement element, CancellationToken cancellationToken)
  856. {
  857. _lastKey = await _keySelector(element).ConfigureAwait(false);
  858. }
  859. }
  860. internal sealed class AsyncCachingComparerWithTaskAndChild<TElement, TKey> : AsyncCachingComparerWithTask<TElement, TKey>
  861. {
  862. private readonly AsyncCachingComparer<TElement> _child;
  863. public AsyncCachingComparerWithTaskAndChild(Func<TElement, ValueTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending, AsyncCachingComparer<TElement> child)
  864. : base(keySelector, comparer, descending)
  865. {
  866. _child = child;
  867. }
  868. internal override async ValueTask<int> Compare(TElement element, bool cacheLower, CancellationToken cancellationToken)
  869. {
  870. var newKey = await _keySelector(element).ConfigureAwait(false);
  871. var cmp = _descending ? _comparer.Compare(_lastKey, newKey) : _comparer.Compare(newKey, _lastKey);
  872. if (cmp == 0)
  873. {
  874. return await _child.Compare(element, cacheLower, cancellationToken).ConfigureAwait(false);
  875. }
  876. if (cacheLower == cmp < 0)
  877. {
  878. _lastKey = newKey;
  879. await _child.SetElement(element, cancellationToken).ConfigureAwait(false);
  880. }
  881. return cmp;
  882. }
  883. internal override async ValueTask SetElement(TElement element, CancellationToken cancellationToken)
  884. {
  885. await base.SetElement(element, cancellationToken).ConfigureAwait(false);
  886. await _child.SetElement(element, cancellationToken).ConfigureAwait(false);
  887. }
  888. }
  889. #if !NO_DEEP_CANCELLATION
  890. internal class AsyncCachingComparerWithTaskAndCancellation<TElement, TKey> : AsyncCachingComparer<TElement>
  891. {
  892. protected readonly Func<TElement, CancellationToken, ValueTask<TKey>> _keySelector;
  893. protected readonly IComparer<TKey> _comparer;
  894. protected readonly bool _descending;
  895. protected TKey _lastKey;
  896. public AsyncCachingComparerWithTaskAndCancellation(Func<TElement, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending)
  897. {
  898. _keySelector = keySelector;
  899. _comparer = comparer;
  900. _descending = descending;
  901. _lastKey = default!;
  902. }
  903. internal override async ValueTask<int> Compare(TElement element, bool cacheLower, CancellationToken cancellationToken)
  904. {
  905. var newKey = await _keySelector(element, cancellationToken).ConfigureAwait(false);
  906. var cmp = _descending ? _comparer.Compare(_lastKey, newKey) : _comparer.Compare(newKey, _lastKey);
  907. if (cacheLower == cmp < 0)
  908. {
  909. _lastKey = newKey;
  910. }
  911. return cmp;
  912. }
  913. internal override async ValueTask SetElement(TElement element, CancellationToken cancellationToken)
  914. {
  915. _lastKey = await _keySelector(element, cancellationToken).ConfigureAwait(false);
  916. }
  917. }
  918. internal sealed class AsyncCachingComparerWithTaskAndCancellationAndChild<TElement, TKey> : AsyncCachingComparerWithTaskAndCancellation<TElement, TKey>
  919. {
  920. private readonly AsyncCachingComparer<TElement> _child;
  921. public AsyncCachingComparerWithTaskAndCancellationAndChild(Func<TElement, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending, AsyncCachingComparer<TElement> child)
  922. : base(keySelector, comparer, descending)
  923. {
  924. _child = child;
  925. }
  926. internal override async ValueTask<int> Compare(TElement element, bool cacheLower, CancellationToken cancellationToken)
  927. {
  928. var newKey = await _keySelector(element, cancellationToken).ConfigureAwait(false);
  929. var cmp = _descending ? _comparer.Compare(_lastKey, newKey) : _comparer.Compare(newKey, _lastKey);
  930. if (cmp == 0)
  931. {
  932. return await _child.Compare(element, cacheLower, cancellationToken).ConfigureAwait(false);
  933. }
  934. if (cacheLower == cmp < 0)
  935. {
  936. _lastKey = newKey;
  937. await _child.SetElement(element, cancellationToken).ConfigureAwait(false);
  938. }
  939. return cmp;
  940. }
  941. internal override async ValueTask SetElement(TElement element, CancellationToken cancellationToken)
  942. {
  943. await base.SetElement(element, cancellationToken).ConfigureAwait(false);
  944. await _child.SetElement(element, cancellationToken).ConfigureAwait(false);
  945. }
  946. }
  947. #endif
  948. }