Min.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. public static Task<decimal?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector)
  12. {
  13. if (source == null)
  14. {
  15. throw new ArgumentNullException(nameof(source));
  16. }
  17. if (selector == null)
  18. {
  19. throw new ArgumentNullException(nameof(selector));
  20. }
  21. return Min(source, selector, CancellationToken.None);
  22. }
  23. public static Task<TResult> Min<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector)
  24. {
  25. if (source == null)
  26. {
  27. throw new ArgumentNullException(nameof(source));
  28. }
  29. if (selector == null)
  30. {
  31. throw new ArgumentNullException(nameof(selector));
  32. }
  33. return Min(source, selector, CancellationToken.None);
  34. }
  35. public static Task<TSource> Min<TSource>(this IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
  36. {
  37. if (source == null)
  38. {
  39. throw new ArgumentNullException(nameof(source));
  40. }
  41. if (comparer == null)
  42. {
  43. throw new ArgumentNullException(nameof(comparer));
  44. }
  45. return Min_(source, comparer, cancellationToken);
  46. }
  47. public static Task<int> Min(this IAsyncEnumerable<int> source)
  48. {
  49. if (source == null)
  50. {
  51. throw new ArgumentNullException(nameof(source));
  52. }
  53. return Min(source, CancellationToken.None);
  54. }
  55. public static Task<long> Min(this IAsyncEnumerable<long> source)
  56. {
  57. if (source == null)
  58. {
  59. throw new ArgumentNullException(nameof(source));
  60. }
  61. return Min(source, CancellationToken.None);
  62. }
  63. public static Task<double> Min(this IAsyncEnumerable<double> source)
  64. {
  65. if (source == null)
  66. {
  67. throw new ArgumentNullException(nameof(source));
  68. }
  69. return Min(source, CancellationToken.None);
  70. }
  71. public static Task<float> Min(this IAsyncEnumerable<float> source)
  72. {
  73. if (source == null)
  74. {
  75. throw new ArgumentNullException(nameof(source));
  76. }
  77. return Min(source, CancellationToken.None);
  78. }
  79. public static Task<decimal> Min(this IAsyncEnumerable<decimal> source)
  80. {
  81. if (source == null)
  82. {
  83. throw new ArgumentNullException(nameof(source));
  84. }
  85. return Min(source, CancellationToken.None);
  86. }
  87. public static Task<int?> Min(this IAsyncEnumerable<int?> source)
  88. {
  89. if (source == null)
  90. {
  91. throw new ArgumentNullException(nameof(source));
  92. }
  93. return Min(source, CancellationToken.None);
  94. }
  95. public static Task<long?> Min(this IAsyncEnumerable<long?> source)
  96. {
  97. if (source == null)
  98. {
  99. throw new ArgumentNullException(nameof(source));
  100. }
  101. return Min(source, CancellationToken.None);
  102. }
  103. public static Task<double?> Min(this IAsyncEnumerable<double?> source)
  104. {
  105. if (source == null)
  106. {
  107. throw new ArgumentNullException(nameof(source));
  108. }
  109. return Min(source, CancellationToken.None);
  110. }
  111. public static Task<float?> Min(this IAsyncEnumerable<float?> source)
  112. {
  113. if (source == null)
  114. {
  115. throw new ArgumentNullException(nameof(source));
  116. }
  117. return Min(source, CancellationToken.None);
  118. }
  119. public static Task<decimal?> Min(this IAsyncEnumerable<decimal?> source)
  120. {
  121. if (source == null)
  122. {
  123. throw new ArgumentNullException(nameof(source));
  124. }
  125. return Min(source, CancellationToken.None);
  126. }
  127. public static Task<TSource> Min<TSource>(this IAsyncEnumerable<TSource> source)
  128. {
  129. if (source == null)
  130. {
  131. throw new ArgumentNullException(nameof(source));
  132. }
  133. return Min(source, CancellationToken.None);
  134. }
  135. public static Task<int> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector)
  136. {
  137. if (source == null)
  138. {
  139. throw new ArgumentNullException(nameof(source));
  140. }
  141. if (selector == null)
  142. {
  143. throw new ArgumentNullException(nameof(selector));
  144. }
  145. return Min(source, selector, CancellationToken.None);
  146. }
  147. public static Task<long> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector)
  148. {
  149. if (source == null)
  150. {
  151. throw new ArgumentNullException(nameof(source));
  152. }
  153. if (selector == null)
  154. {
  155. throw new ArgumentNullException(nameof(selector));
  156. }
  157. return Min(source, selector, CancellationToken.None);
  158. }
  159. public static Task<double> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector)
  160. {
  161. if (source == null)
  162. {
  163. throw new ArgumentNullException(nameof(source));
  164. }
  165. if (selector == null)
  166. {
  167. throw new ArgumentNullException(nameof(selector));
  168. }
  169. return Min(source, selector, CancellationToken.None);
  170. }
  171. public static Task<float> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector)
  172. {
  173. if (source == null)
  174. {
  175. throw new ArgumentNullException(nameof(source));
  176. }
  177. if (selector == null)
  178. {
  179. throw new ArgumentNullException(nameof(selector));
  180. }
  181. return Min(source, selector, CancellationToken.None);
  182. }
  183. public static Task<decimal> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector)
  184. {
  185. if (source == null)
  186. {
  187. throw new ArgumentNullException(nameof(source));
  188. }
  189. if (selector == null)
  190. {
  191. throw new ArgumentNullException(nameof(selector));
  192. }
  193. return Min(source, selector, CancellationToken.None);
  194. }
  195. public static Task<int?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector)
  196. {
  197. if (source == null)
  198. {
  199. throw new ArgumentNullException(nameof(source));
  200. }
  201. if (selector == null)
  202. {
  203. throw new ArgumentNullException(nameof(selector));
  204. }
  205. return Min(source, selector, CancellationToken.None);
  206. }
  207. public static Task<long?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector)
  208. {
  209. if (source == null)
  210. {
  211. throw new ArgumentNullException(nameof(source));
  212. }
  213. if (selector == null)
  214. {
  215. throw new ArgumentNullException(nameof(selector));
  216. }
  217. return Min(source, selector, CancellationToken.None);
  218. }
  219. public static Task<double?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector)
  220. {
  221. if (source == null)
  222. {
  223. throw new ArgumentNullException(nameof(source));
  224. }
  225. if (selector == null)
  226. {
  227. throw new ArgumentNullException(nameof(selector));
  228. }
  229. return Min(source, selector, CancellationToken.None);
  230. }
  231. public static Task<int> Min(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  232. {
  233. if (source == null)
  234. {
  235. throw new ArgumentNullException(nameof(source));
  236. }
  237. return source.Aggregate(Math.Min, cancellationToken);
  238. }
  239. public static Task<long> Min(this IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  240. {
  241. if (source == null)
  242. {
  243. throw new ArgumentNullException(nameof(source));
  244. }
  245. return source.Aggregate(Math.Min, cancellationToken);
  246. }
  247. public static Task<double> Min(this IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  248. {
  249. if (source == null)
  250. {
  251. throw new ArgumentNullException(nameof(source));
  252. }
  253. return source.Aggregate(Math.Min, cancellationToken);
  254. }
  255. public static Task<float> Min(this IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  256. {
  257. if (source == null)
  258. {
  259. throw new ArgumentNullException(nameof(source));
  260. }
  261. return source.Aggregate(Math.Min, cancellationToken);
  262. }
  263. public static Task<decimal> Min(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  264. {
  265. if (source == null)
  266. {
  267. throw new ArgumentNullException(nameof(source));
  268. }
  269. return source.Aggregate(Math.Min, cancellationToken);
  270. }
  271. public static Task<int?> Min(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  272. {
  273. if (source == null)
  274. {
  275. throw new ArgumentNullException(nameof(source));
  276. }
  277. return source.Aggregate(default(int?), NullableMin, cancellationToken);
  278. }
  279. public static Task<long?> Min(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  280. {
  281. if (source == null)
  282. {
  283. throw new ArgumentNullException(nameof(source));
  284. }
  285. return source.Aggregate(default(long?), NullableMin, cancellationToken);
  286. }
  287. public static Task<double?> Min(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  288. {
  289. if (source == null)
  290. {
  291. throw new ArgumentNullException(nameof(source));
  292. }
  293. return source.Aggregate(default(double?), NullableMin, cancellationToken);
  294. }
  295. public static Task<float?> Min(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  296. {
  297. if (source == null)
  298. {
  299. throw new ArgumentNullException(nameof(source));
  300. }
  301. return source.Aggregate(default(float?), NullableMin, cancellationToken);
  302. }
  303. public static Task<decimal?> Min(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  304. {
  305. if (source == null)
  306. {
  307. throw new ArgumentNullException(nameof(source));
  308. }
  309. return source.Aggregate(default(decimal?), NullableMin, cancellationToken);
  310. }
  311. public static Task<TSource> Min<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  312. {
  313. if (source == null)
  314. {
  315. throw new ArgumentNullException(nameof(source));
  316. }
  317. var comparer = Comparer<TSource>.Default;
  318. return source.Aggregate((x, y) => comparer.Compare(x, y) <= 0 ? x : y, cancellationToken);
  319. }
  320. public static Task<int> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  321. {
  322. if (source == null)
  323. {
  324. throw new ArgumentNullException(nameof(source));
  325. }
  326. if (selector == null)
  327. {
  328. throw new ArgumentNullException(nameof(selector));
  329. }
  330. return source.Select(selector)
  331. .Min(cancellationToken);
  332. }
  333. public static Task<long> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  334. {
  335. if (source == null)
  336. {
  337. throw new ArgumentNullException(nameof(source));
  338. }
  339. if (selector == null)
  340. {
  341. throw new ArgumentNullException(nameof(selector));
  342. }
  343. return source.Select(selector)
  344. .Min(cancellationToken);
  345. }
  346. public static Task<double> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  347. {
  348. if (source == null)
  349. {
  350. throw new ArgumentNullException(nameof(source));
  351. }
  352. if (selector == null)
  353. {
  354. throw new ArgumentNullException(nameof(selector));
  355. }
  356. return source.Select(selector)
  357. .Min(cancellationToken);
  358. }
  359. public static Task<float> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  360. {
  361. if (source == null)
  362. {
  363. throw new ArgumentNullException(nameof(source));
  364. }
  365. if (selector == null)
  366. {
  367. throw new ArgumentNullException(nameof(selector));
  368. }
  369. return source.Select(selector)
  370. .Min(cancellationToken);
  371. }
  372. public static Task<decimal> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  373. {
  374. if (source == null)
  375. {
  376. throw new ArgumentNullException(nameof(source));
  377. }
  378. if (selector == null)
  379. {
  380. throw new ArgumentNullException(nameof(selector));
  381. }
  382. return source.Select(selector)
  383. .Min(cancellationToken);
  384. }
  385. public static Task<int?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  386. {
  387. if (source == null)
  388. {
  389. throw new ArgumentNullException(nameof(source));
  390. }
  391. if (selector == null)
  392. {
  393. throw new ArgumentNullException(nameof(selector));
  394. }
  395. return source.Select(selector)
  396. .Min(cancellationToken);
  397. }
  398. public static Task<long?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  399. {
  400. if (source == null)
  401. {
  402. throw new ArgumentNullException(nameof(source));
  403. }
  404. if (selector == null)
  405. {
  406. throw new ArgumentNullException(nameof(selector));
  407. }
  408. return source.Select(selector)
  409. .Min(cancellationToken);
  410. }
  411. public static Task<double?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  412. {
  413. if (source == null)
  414. {
  415. throw new ArgumentNullException(nameof(source));
  416. }
  417. if (selector == null)
  418. {
  419. throw new ArgumentNullException(nameof(selector));
  420. }
  421. return source.Select(selector)
  422. .Min(cancellationToken);
  423. }
  424. public static Task<float?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  425. {
  426. if (source == null)
  427. {
  428. throw new ArgumentNullException(nameof(source));
  429. }
  430. if (selector == null)
  431. {
  432. throw new ArgumentNullException(nameof(selector));
  433. }
  434. return source.Select(selector)
  435. .Min(cancellationToken);
  436. }
  437. public static Task<TSource> Min<TSource>(this IAsyncEnumerable<TSource> source, IComparer<TSource> comparer)
  438. {
  439. if (source == null)
  440. {
  441. throw new ArgumentNullException(nameof(source));
  442. }
  443. if (comparer == null)
  444. {
  445. throw new ArgumentNullException(nameof(comparer));
  446. }
  447. return source.Min(comparer, CancellationToken.None);
  448. }
  449. public static Task<decimal?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  450. {
  451. if (source == null)
  452. {
  453. throw new ArgumentNullException(nameof(source));
  454. }
  455. if (selector == null)
  456. {
  457. throw new ArgumentNullException(nameof(selector));
  458. }
  459. return source.Select(selector)
  460. .Min(cancellationToken);
  461. }
  462. public static Task<TResult> Min<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
  463. {
  464. if (source == null)
  465. {
  466. throw new ArgumentNullException(nameof(source));
  467. }
  468. if (selector == null)
  469. {
  470. throw new ArgumentNullException(nameof(selector));
  471. }
  472. return source.Select(selector)
  473. .Min(cancellationToken);
  474. }
  475. public static Task<float?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector)
  476. {
  477. if (source == null)
  478. {
  479. throw new ArgumentNullException(nameof(source));
  480. }
  481. if (selector == null)
  482. {
  483. throw new ArgumentNullException(nameof(selector));
  484. }
  485. return Min(source, selector, CancellationToken.None);
  486. }
  487. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  488. {
  489. if (source == null)
  490. {
  491. throw new ArgumentNullException(nameof(source));
  492. }
  493. if (keySelector == null)
  494. {
  495. throw new ArgumentNullException(nameof(keySelector));
  496. }
  497. return source.MinBy(keySelector, CancellationToken.None);
  498. }
  499. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  500. {
  501. if (source == null)
  502. {
  503. throw new ArgumentNullException(nameof(source));
  504. }
  505. if (keySelector == null)
  506. {
  507. throw new ArgumentNullException(nameof(keySelector));
  508. }
  509. if (comparer == null)
  510. {
  511. throw new ArgumentNullException(nameof(comparer));
  512. }
  513. return source.MinBy(keySelector, comparer, CancellationToken.None);
  514. }
  515. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
  516. {
  517. if (source == null)
  518. {
  519. throw new ArgumentNullException(nameof(source));
  520. }
  521. if (keySelector == null)
  522. {
  523. throw new ArgumentNullException(nameof(keySelector));
  524. }
  525. return MinBy(source, keySelector, Comparer<TKey>.Default, cancellationToken);
  526. }
  527. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
  528. {
  529. if (source == null)
  530. {
  531. throw new ArgumentNullException(nameof(source));
  532. }
  533. if (keySelector == null)
  534. {
  535. throw new ArgumentNullException(nameof(keySelector));
  536. }
  537. if (comparer == null)
  538. {
  539. throw new ArgumentNullException(nameof(comparer));
  540. }
  541. return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
  542. }
  543. private static async Task<IList<TSource>> ExtremaBy<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare, CancellationToken cancellationToken)
  544. {
  545. var result = new List<TSource>();
  546. using (var e = source.GetEnumerator())
  547. {
  548. if (!await e.MoveNext(cancellationToken)
  549. .ConfigureAwait(false))
  550. {
  551. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  552. }
  553. var current = e.Current;
  554. var resKey = keySelector(current);
  555. result.Add(current);
  556. while (await e.MoveNext(cancellationToken)
  557. .ConfigureAwait(false))
  558. {
  559. var cur = e.Current;
  560. var key = keySelector(cur);
  561. var cmp = compare(key, resKey);
  562. if (cmp == 0)
  563. {
  564. result.Add(cur);
  565. }
  566. else if (cmp > 0)
  567. {
  568. result = new List<TSource>
  569. {
  570. cur
  571. };
  572. resKey = key;
  573. }
  574. }
  575. }
  576. return result;
  577. }
  578. private static async Task<TSource> Min_<TSource>(IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
  579. {
  580. return (await MinBy(source, x => x, comparer, cancellationToken)
  581. .ConfigureAwait(false)).First();
  582. }
  583. private static T? NullableMin<T>(T? x, T? y)
  584. where T : struct, IComparable<T>
  585. {
  586. if (!x.HasValue)
  587. {
  588. return y;
  589. }
  590. if (!y.HasValue)
  591. {
  592. return x;
  593. }
  594. if (x.Value.CompareTo(y.Value) <= 0)
  595. {
  596. return x;
  597. }
  598. return y;
  599. }
  600. }
  601. }