Max.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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<TSource> Max<TSource>(this IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
  12. {
  13. if (source == null)
  14. {
  15. throw new ArgumentNullException(nameof(source));
  16. }
  17. if (comparer == null)
  18. {
  19. throw new ArgumentNullException(nameof(comparer));
  20. }
  21. return Max_(source, comparer, cancellationToken);
  22. }
  23. public static Task<int> Max(this IAsyncEnumerable<int> source)
  24. {
  25. if (source == null)
  26. {
  27. throw new ArgumentNullException(nameof(source));
  28. }
  29. return Max(source, CancellationToken.None);
  30. }
  31. public static Task<long> Max(this IAsyncEnumerable<long> source)
  32. {
  33. if (source == null)
  34. {
  35. throw new ArgumentNullException(nameof(source));
  36. }
  37. return Max(source, CancellationToken.None);
  38. }
  39. public static Task<double> Max(this IAsyncEnumerable<double> source)
  40. {
  41. if (source == null)
  42. {
  43. throw new ArgumentNullException(nameof(source));
  44. }
  45. return Max(source, CancellationToken.None);
  46. }
  47. public static Task<float> Max(this IAsyncEnumerable<float> source)
  48. {
  49. if (source == null)
  50. {
  51. throw new ArgumentNullException(nameof(source));
  52. }
  53. return Max(source, CancellationToken.None);
  54. }
  55. public static Task<int> Max(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  56. {
  57. if (source == null)
  58. {
  59. throw new ArgumentNullException(nameof(source));
  60. }
  61. return source.Aggregate(Math.Max, cancellationToken);
  62. }
  63. public static Task<long> Max(this IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  64. {
  65. if (source == null)
  66. {
  67. throw new ArgumentNullException(nameof(source));
  68. }
  69. return source.Aggregate(Math.Max, cancellationToken);
  70. }
  71. public static Task<double> Max(this IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  72. {
  73. if (source == null)
  74. {
  75. throw new ArgumentNullException(nameof(source));
  76. }
  77. return source.Aggregate(Math.Max, cancellationToken);
  78. }
  79. public static Task<float> Max(this IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  80. {
  81. if (source == null)
  82. {
  83. throw new ArgumentNullException(nameof(source));
  84. }
  85. return source.Aggregate(Math.Max, cancellationToken);
  86. }
  87. public static Task<decimal> Max(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  88. {
  89. if (source == null)
  90. {
  91. throw new ArgumentNullException(nameof(source));
  92. }
  93. return source.Aggregate(Math.Max, cancellationToken);
  94. }
  95. public static Task<int?> Max(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  96. {
  97. if (source == null)
  98. {
  99. throw new ArgumentNullException(nameof(source));
  100. }
  101. return source.Aggregate(default(int?), NullableMax, cancellationToken);
  102. }
  103. public static Task<long?> Max(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  104. {
  105. if (source == null)
  106. {
  107. throw new ArgumentNullException(nameof(source));
  108. }
  109. return source.Aggregate(default(long?), NullableMax, cancellationToken);
  110. }
  111. public static Task<double?> Max(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  112. {
  113. if (source == null)
  114. {
  115. throw new ArgumentNullException(nameof(source));
  116. }
  117. return source.Aggregate(default(double?), NullableMax, cancellationToken);
  118. }
  119. public static Task<float?> Max(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  120. {
  121. if (source == null)
  122. {
  123. throw new ArgumentNullException(nameof(source));
  124. }
  125. return source.Aggregate(default(float?), NullableMax, cancellationToken);
  126. }
  127. public static Task<decimal?> Max(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  128. {
  129. if (source == null)
  130. {
  131. throw new ArgumentNullException(nameof(source));
  132. }
  133. return source.Aggregate(default(decimal?), NullableMax, cancellationToken);
  134. }
  135. public static Task<TSource> Max<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  136. {
  137. if (source == null)
  138. {
  139. throw new ArgumentNullException(nameof(source));
  140. }
  141. var comparer = Comparer<TSource>.Default;
  142. return source.Aggregate((x, y) => comparer.Compare(x, y) >= 0 ? x : y, cancellationToken);
  143. }
  144. public static Task<int> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  145. {
  146. if (source == null)
  147. {
  148. throw new ArgumentNullException(nameof(source));
  149. }
  150. if (selector == null)
  151. {
  152. throw new ArgumentNullException(nameof(selector));
  153. }
  154. return source.Select(selector)
  155. .Max(cancellationToken);
  156. }
  157. public static Task<long> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  158. {
  159. if (source == null)
  160. {
  161. throw new ArgumentNullException(nameof(source));
  162. }
  163. if (selector == null)
  164. {
  165. throw new ArgumentNullException(nameof(selector));
  166. }
  167. return source.Select(selector)
  168. .Max(cancellationToken);
  169. }
  170. public static Task<double> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  171. {
  172. if (source == null)
  173. {
  174. throw new ArgumentNullException(nameof(source));
  175. }
  176. if (selector == null)
  177. {
  178. throw new ArgumentNullException(nameof(selector));
  179. }
  180. return source.Select(selector)
  181. .Max(cancellationToken);
  182. }
  183. public static Task<float> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  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 source.Select(selector)
  194. .Max(cancellationToken);
  195. }
  196. public static Task<decimal> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  197. {
  198. if (source == null)
  199. {
  200. throw new ArgumentNullException(nameof(source));
  201. }
  202. if (selector == null)
  203. {
  204. throw new ArgumentNullException(nameof(selector));
  205. }
  206. return source.Select(selector)
  207. .Max(cancellationToken);
  208. }
  209. public static Task<int?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  210. {
  211. if (source == null)
  212. {
  213. throw new ArgumentNullException(nameof(source));
  214. }
  215. if (selector == null)
  216. {
  217. throw new ArgumentNullException(nameof(selector));
  218. }
  219. return source.Select(selector)
  220. .Max(cancellationToken);
  221. }
  222. public static Task<long?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  223. {
  224. if (source == null)
  225. {
  226. throw new ArgumentNullException(nameof(source));
  227. }
  228. if (selector == null)
  229. {
  230. throw new ArgumentNullException(nameof(selector));
  231. }
  232. return source.Select(selector)
  233. .Max(cancellationToken);
  234. }
  235. public static Task<double?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  236. {
  237. if (source == null)
  238. {
  239. throw new ArgumentNullException(nameof(source));
  240. }
  241. if (selector == null)
  242. {
  243. throw new ArgumentNullException(nameof(selector));
  244. }
  245. return source.Select(selector)
  246. .Max(cancellationToken);
  247. }
  248. public static Task<float?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  249. {
  250. if (source == null)
  251. {
  252. throw new ArgumentNullException(nameof(source));
  253. }
  254. if (selector == null)
  255. {
  256. throw new ArgumentNullException(nameof(selector));
  257. }
  258. return source.Select(selector)
  259. .Max(cancellationToken);
  260. }
  261. public static Task<decimal?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  262. {
  263. if (source == null)
  264. {
  265. throw new ArgumentNullException(nameof(source));
  266. }
  267. if (selector == null)
  268. {
  269. throw new ArgumentNullException(nameof(selector));
  270. }
  271. return source.Select(selector)
  272. .Max(cancellationToken);
  273. }
  274. public static Task<TResult> Max<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
  275. {
  276. if (source == null)
  277. {
  278. throw new ArgumentNullException(nameof(source));
  279. }
  280. if (selector == null)
  281. {
  282. throw new ArgumentNullException(nameof(selector));
  283. }
  284. return source.Select(selector)
  285. .Max(cancellationToken);
  286. }
  287. public static Task<decimal> Max(this IAsyncEnumerable<decimal> source)
  288. {
  289. if (source == null)
  290. {
  291. throw new ArgumentNullException(nameof(source));
  292. }
  293. return Max(source, CancellationToken.None);
  294. }
  295. public static Task<int?> Max(this IAsyncEnumerable<int?> source)
  296. {
  297. if (source == null)
  298. {
  299. throw new ArgumentNullException(nameof(source));
  300. }
  301. return Max(source, CancellationToken.None);
  302. }
  303. public static Task<long?> Max(this IAsyncEnumerable<long?> source)
  304. {
  305. if (source == null)
  306. {
  307. throw new ArgumentNullException(nameof(source));
  308. }
  309. return Max(source, CancellationToken.None);
  310. }
  311. public static Task<double?> Max(this IAsyncEnumerable<double?> source)
  312. {
  313. if (source == null)
  314. {
  315. throw new ArgumentNullException(nameof(source));
  316. }
  317. return Max(source, CancellationToken.None);
  318. }
  319. public static Task<float?> Max(this IAsyncEnumerable<float?> source)
  320. {
  321. if (source == null)
  322. {
  323. throw new ArgumentNullException(nameof(source));
  324. }
  325. return Max(source, CancellationToken.None);
  326. }
  327. public static Task<decimal?> Max(this IAsyncEnumerable<decimal?> source)
  328. {
  329. if (source == null)
  330. {
  331. throw new ArgumentNullException(nameof(source));
  332. }
  333. return Max(source, CancellationToken.None);
  334. }
  335. public static Task<TSource> Max<TSource>(this IAsyncEnumerable<TSource> source)
  336. {
  337. if (source == null)
  338. {
  339. throw new ArgumentNullException(nameof(source));
  340. }
  341. return Max(source, CancellationToken.None);
  342. }
  343. public static Task<int> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector)
  344. {
  345. if (source == null)
  346. {
  347. throw new ArgumentNullException(nameof(source));
  348. }
  349. if (selector == null)
  350. {
  351. throw new ArgumentNullException(nameof(selector));
  352. }
  353. return Max(source, selector, CancellationToken.None);
  354. }
  355. public static Task<long> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector)
  356. {
  357. if (source == null)
  358. {
  359. throw new ArgumentNullException(nameof(source));
  360. }
  361. if (selector == null)
  362. {
  363. throw new ArgumentNullException(nameof(selector));
  364. }
  365. return Max(source, selector, CancellationToken.None);
  366. }
  367. public static Task<double> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector)
  368. {
  369. if (source == null)
  370. {
  371. throw new ArgumentNullException(nameof(source));
  372. }
  373. if (selector == null)
  374. {
  375. throw new ArgumentNullException(nameof(selector));
  376. }
  377. return Max(source, selector, CancellationToken.None);
  378. }
  379. public static Task<float> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector)
  380. {
  381. if (source == null)
  382. {
  383. throw new ArgumentNullException(nameof(source));
  384. }
  385. if (selector == null)
  386. {
  387. throw new ArgumentNullException(nameof(selector));
  388. }
  389. return Max(source, selector, CancellationToken.None);
  390. }
  391. public static Task<decimal> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector)
  392. {
  393. if (source == null)
  394. {
  395. throw new ArgumentNullException(nameof(source));
  396. }
  397. if (selector == null)
  398. {
  399. throw new ArgumentNullException(nameof(selector));
  400. }
  401. return Max(source, selector, CancellationToken.None);
  402. }
  403. public static Task<int?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector)
  404. {
  405. if (source == null)
  406. {
  407. throw new ArgumentNullException(nameof(source));
  408. }
  409. if (selector == null)
  410. {
  411. throw new ArgumentNullException(nameof(selector));
  412. }
  413. return Max(source, selector, CancellationToken.None);
  414. }
  415. public static Task<long?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector)
  416. {
  417. if (source == null)
  418. {
  419. throw new ArgumentNullException(nameof(source));
  420. }
  421. if (selector == null)
  422. {
  423. throw new ArgumentNullException(nameof(selector));
  424. }
  425. return Max(source, selector, CancellationToken.None);
  426. }
  427. public static Task<double?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector)
  428. {
  429. if (source == null)
  430. {
  431. throw new ArgumentNullException(nameof(source));
  432. }
  433. if (selector == null)
  434. {
  435. throw new ArgumentNullException(nameof(selector));
  436. }
  437. return Max(source, selector, CancellationToken.None);
  438. }
  439. public static Task<float?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector)
  440. {
  441. if (source == null)
  442. {
  443. throw new ArgumentNullException(nameof(source));
  444. }
  445. if (selector == null)
  446. {
  447. throw new ArgumentNullException(nameof(selector));
  448. }
  449. return Max(source, selector, CancellationToken.None);
  450. }
  451. public static Task<decimal?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector)
  452. {
  453. if (source == null)
  454. {
  455. throw new ArgumentNullException(nameof(source));
  456. }
  457. if (selector == null)
  458. {
  459. throw new ArgumentNullException(nameof(selector));
  460. }
  461. return Max(source, selector, CancellationToken.None);
  462. }
  463. public static Task<TResult> Max<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector)
  464. {
  465. if (source == null)
  466. {
  467. throw new ArgumentNullException(nameof(source));
  468. }
  469. if (selector == null)
  470. {
  471. throw new ArgumentNullException(nameof(selector));
  472. }
  473. return Max(source, selector, CancellationToken.None);
  474. }
  475. public static Task<TSource> Max<TSource>(this IAsyncEnumerable<TSource> source, IComparer<TSource> comparer)
  476. {
  477. if (source == null)
  478. {
  479. throw new ArgumentNullException(nameof(source));
  480. }
  481. if (comparer == null)
  482. {
  483. throw new ArgumentNullException(nameof(comparer));
  484. }
  485. return source.Max(comparer, CancellationToken.None);
  486. }
  487. public static Task<IList<TSource>> MaxBy<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.MaxBy(keySelector, CancellationToken.None);
  498. }
  499. public static Task<IList<TSource>> MaxBy<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.MaxBy(keySelector, comparer, CancellationToken.None);
  514. }
  515. public static Task<IList<TSource>> MaxBy<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 MaxBy(source, keySelector, Comparer<TKey>.Default, cancellationToken);
  526. }
  527. public static Task<IList<TSource>> MaxBy<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<TSource> Max_<TSource>(IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
  544. {
  545. return (await MaxBy(source, x => x, comparer, cancellationToken)
  546. .ConfigureAwait(false)).First();
  547. }
  548. private static T? NullableMax<T>(T? x, T? y)
  549. where T : struct, IComparable<T>
  550. {
  551. if (!x.HasValue)
  552. {
  553. return y;
  554. }
  555. if (!y.HasValue)
  556. {
  557. return x;
  558. }
  559. if (x.Value.CompareTo(y.Value) >= 0)
  560. {
  561. return x;
  562. }
  563. return y;
  564. }
  565. }
  566. }