Max.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  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<TSource> Max<TSource>(this IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. if (comparer == null)
  18. throw new ArgumentNullException(nameof(comparer));
  19. return Max_(source, comparer, cancellationToken);
  20. }
  21. public static Task<int> Max(this IAsyncEnumerable<int> source)
  22. {
  23. if (source == null)
  24. throw new ArgumentNullException(nameof(source));
  25. return Max(source, CancellationToken.None);
  26. }
  27. public static Task<long> Max(this IAsyncEnumerable<long> source)
  28. {
  29. if (source == null)
  30. throw new ArgumentNullException(nameof(source));
  31. return Max(source, CancellationToken.None);
  32. }
  33. public static Task<double> Max(this IAsyncEnumerable<double> source)
  34. {
  35. if (source == null)
  36. throw new ArgumentNullException(nameof(source));
  37. return Max(source, CancellationToken.None);
  38. }
  39. public static Task<float> Max(this IAsyncEnumerable<float> source)
  40. {
  41. if (source == null)
  42. throw new ArgumentNullException(nameof(source));
  43. return Max(source, CancellationToken.None);
  44. }
  45. public static Task<int> Max(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  46. {
  47. if (source == null)
  48. throw new ArgumentNullException(nameof(source));
  49. return source.Aggregate(Math.Max, cancellationToken);
  50. }
  51. public static Task<long> Max(this IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  52. {
  53. if (source == null)
  54. throw new ArgumentNullException(nameof(source));
  55. return source.Aggregate(Math.Max, cancellationToken);
  56. }
  57. public static Task<double> Max(this IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  58. {
  59. if (source == null)
  60. throw new ArgumentNullException(nameof(source));
  61. return source.Aggregate(Math.Max, cancellationToken);
  62. }
  63. public static Task<float> Max(this IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  64. {
  65. if (source == null)
  66. throw new ArgumentNullException(nameof(source));
  67. return source.Aggregate(Math.Max, cancellationToken);
  68. }
  69. public static Task<decimal> Max(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  70. {
  71. if (source == null)
  72. throw new ArgumentNullException(nameof(source));
  73. return source.Aggregate(Math.Max, cancellationToken);
  74. }
  75. public static Task<int?> Max(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  76. {
  77. if (source == null)
  78. throw new ArgumentNullException(nameof(source));
  79. return source.Aggregate(default(int?), NullableMax, cancellationToken);
  80. }
  81. public static Task<long?> Max(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  82. {
  83. if (source == null)
  84. throw new ArgumentNullException(nameof(source));
  85. return source.Aggregate(default(long?), NullableMax, cancellationToken);
  86. }
  87. public static Task<double?> Max(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  88. {
  89. if (source == null)
  90. throw new ArgumentNullException(nameof(source));
  91. return source.Aggregate(default(double?), NullableMax, cancellationToken);
  92. }
  93. public static Task<float?> Max(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  94. {
  95. if (source == null)
  96. throw new ArgumentNullException(nameof(source));
  97. return source.Aggregate(default(float?), NullableMax, cancellationToken);
  98. }
  99. public static Task<decimal?> Max(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  100. {
  101. if (source == null)
  102. throw new ArgumentNullException(nameof(source));
  103. return source.Aggregate(default(decimal?), NullableMax, cancellationToken);
  104. }
  105. public static Task<TSource> Max<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  106. {
  107. if (source == null)
  108. throw new ArgumentNullException(nameof(source));
  109. var comparer = Comparer<TSource>.Default;
  110. return source.Aggregate((x, y) => comparer.Compare(x, y) >= 0 ? x : y, cancellationToken);
  111. }
  112. public static Task<int> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  113. {
  114. if (source == null)
  115. throw new ArgumentNullException(nameof(source));
  116. if (selector == null)
  117. throw new ArgumentNullException(nameof(selector));
  118. return source.Select(selector)
  119. .Max(cancellationToken);
  120. }
  121. public static Task<long> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  122. {
  123. if (source == null)
  124. throw new ArgumentNullException(nameof(source));
  125. if (selector == null)
  126. throw new ArgumentNullException(nameof(selector));
  127. return source.Select(selector)
  128. .Max(cancellationToken);
  129. }
  130. public static Task<double> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  131. {
  132. if (source == null)
  133. throw new ArgumentNullException(nameof(source));
  134. if (selector == null)
  135. throw new ArgumentNullException(nameof(selector));
  136. return source.Select(selector)
  137. .Max(cancellationToken);
  138. }
  139. public static Task<float> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  140. {
  141. if (source == null)
  142. throw new ArgumentNullException(nameof(source));
  143. if (selector == null)
  144. throw new ArgumentNullException(nameof(selector));
  145. return source.Select(selector)
  146. .Max(cancellationToken);
  147. }
  148. public static Task<decimal> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  149. {
  150. if (source == null)
  151. throw new ArgumentNullException(nameof(source));
  152. if (selector == null)
  153. throw new ArgumentNullException(nameof(selector));
  154. return source.Select(selector)
  155. .Max(cancellationToken);
  156. }
  157. public static Task<int?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  158. {
  159. if (source == null)
  160. throw new ArgumentNullException(nameof(source));
  161. if (selector == null)
  162. throw new ArgumentNullException(nameof(selector));
  163. return source.Select(selector)
  164. .Max(cancellationToken);
  165. }
  166. public static Task<long?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  167. {
  168. if (source == null)
  169. throw new ArgumentNullException(nameof(source));
  170. if (selector == null)
  171. throw new ArgumentNullException(nameof(selector));
  172. return source.Select(selector)
  173. .Max(cancellationToken);
  174. }
  175. public static Task<double?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  176. {
  177. if (source == null)
  178. throw new ArgumentNullException(nameof(source));
  179. if (selector == null)
  180. throw new ArgumentNullException(nameof(selector));
  181. return source.Select(selector)
  182. .Max(cancellationToken);
  183. }
  184. public static Task<float?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  185. {
  186. if (source == null)
  187. throw new ArgumentNullException(nameof(source));
  188. if (selector == null)
  189. throw new ArgumentNullException(nameof(selector));
  190. return source.Select(selector)
  191. .Max(cancellationToken);
  192. }
  193. public static Task<decimal?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  194. {
  195. if (source == null)
  196. throw new ArgumentNullException(nameof(source));
  197. if (selector == null)
  198. throw new ArgumentNullException(nameof(selector));
  199. return source.Select(selector)
  200. .Max(cancellationToken);
  201. }
  202. public static Task<TResult> Max<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
  203. {
  204. if (source == null)
  205. throw new ArgumentNullException(nameof(source));
  206. if (selector == null)
  207. throw new ArgumentNullException(nameof(selector));
  208. return source.Select(selector)
  209. .Max(cancellationToken);
  210. }
  211. public static Task<decimal> Max(this IAsyncEnumerable<decimal> source)
  212. {
  213. if (source == null)
  214. throw new ArgumentNullException(nameof(source));
  215. return Max(source, CancellationToken.None);
  216. }
  217. public static Task<int?> Max(this IAsyncEnumerable<int?> source)
  218. {
  219. if (source == null)
  220. throw new ArgumentNullException(nameof(source));
  221. return Max(source, CancellationToken.None);
  222. }
  223. public static Task<long?> Max(this IAsyncEnumerable<long?> source)
  224. {
  225. if (source == null)
  226. throw new ArgumentNullException(nameof(source));
  227. return Max(source, CancellationToken.None);
  228. }
  229. public static Task<double?> Max(this IAsyncEnumerable<double?> source)
  230. {
  231. if (source == null)
  232. throw new ArgumentNullException(nameof(source));
  233. return Max(source, CancellationToken.None);
  234. }
  235. public static Task<float?> Max(this IAsyncEnumerable<float?> source)
  236. {
  237. if (source == null)
  238. throw new ArgumentNullException(nameof(source));
  239. return Max(source, CancellationToken.None);
  240. }
  241. public static Task<decimal?> Max(this IAsyncEnumerable<decimal?> source)
  242. {
  243. if (source == null)
  244. throw new ArgumentNullException(nameof(source));
  245. return Max(source, CancellationToken.None);
  246. }
  247. public static Task<TSource> Max<TSource>(this IAsyncEnumerable<TSource> source)
  248. {
  249. if (source == null)
  250. throw new ArgumentNullException(nameof(source));
  251. return Max(source, CancellationToken.None);
  252. }
  253. public static Task<int> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector)
  254. {
  255. if (source == null)
  256. throw new ArgumentNullException(nameof(source));
  257. if (selector == null)
  258. throw new ArgumentNullException(nameof(selector));
  259. return Max(source, selector, CancellationToken.None);
  260. }
  261. public static Task<long> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector)
  262. {
  263. if (source == null)
  264. throw new ArgumentNullException(nameof(source));
  265. if (selector == null)
  266. throw new ArgumentNullException(nameof(selector));
  267. return Max(source, selector, CancellationToken.None);
  268. }
  269. public static Task<double> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector)
  270. {
  271. if (source == null)
  272. throw new ArgumentNullException(nameof(source));
  273. if (selector == null)
  274. throw new ArgumentNullException(nameof(selector));
  275. return Max(source, selector, CancellationToken.None);
  276. }
  277. public static Task<float> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector)
  278. {
  279. if (source == null)
  280. throw new ArgumentNullException(nameof(source));
  281. if (selector == null)
  282. throw new ArgumentNullException(nameof(selector));
  283. return Max(source, selector, CancellationToken.None);
  284. }
  285. public static Task<decimal> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector)
  286. {
  287. if (source == null)
  288. throw new ArgumentNullException(nameof(source));
  289. if (selector == null)
  290. throw new ArgumentNullException(nameof(selector));
  291. return Max(source, selector, CancellationToken.None);
  292. }
  293. public static Task<int?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector)
  294. {
  295. if (source == null)
  296. throw new ArgumentNullException(nameof(source));
  297. if (selector == null)
  298. throw new ArgumentNullException(nameof(selector));
  299. return Max(source, selector, CancellationToken.None);
  300. }
  301. public static Task<long?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector)
  302. {
  303. if (source == null)
  304. throw new ArgumentNullException(nameof(source));
  305. if (selector == null)
  306. throw new ArgumentNullException(nameof(selector));
  307. return Max(source, selector, CancellationToken.None);
  308. }
  309. public static Task<double?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector)
  310. {
  311. if (source == null)
  312. throw new ArgumentNullException(nameof(source));
  313. if (selector == null)
  314. throw new ArgumentNullException(nameof(selector));
  315. return Max(source, selector, CancellationToken.None);
  316. }
  317. public static Task<float?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector)
  318. {
  319. if (source == null)
  320. throw new ArgumentNullException(nameof(source));
  321. if (selector == null)
  322. throw new ArgumentNullException(nameof(selector));
  323. return Max(source, selector, CancellationToken.None);
  324. }
  325. public static Task<decimal?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector)
  326. {
  327. if (source == null)
  328. throw new ArgumentNullException(nameof(source));
  329. if (selector == null)
  330. throw new ArgumentNullException(nameof(selector));
  331. return Max(source, selector, CancellationToken.None);
  332. }
  333. public static Task<TResult> Max<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector)
  334. {
  335. if (source == null)
  336. throw new ArgumentNullException(nameof(source));
  337. if (selector == null)
  338. throw new ArgumentNullException(nameof(selector));
  339. return Max(source, selector, CancellationToken.None);
  340. }
  341. public static Task<TSource> Max<TSource>(this IAsyncEnumerable<TSource> source, IComparer<TSource> comparer)
  342. {
  343. if (source == null)
  344. throw new ArgumentNullException(nameof(source));
  345. if (comparer == null)
  346. throw new ArgumentNullException(nameof(comparer));
  347. return source.Max(comparer, CancellationToken.None);
  348. }
  349. public static Task<IList<TSource>> MaxBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  350. {
  351. if (source == null)
  352. throw new ArgumentNullException(nameof(source));
  353. if (keySelector == null)
  354. throw new ArgumentNullException(nameof(keySelector));
  355. return source.MaxBy(keySelector, CancellationToken.None);
  356. }
  357. public static Task<IList<TSource>> MaxBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  358. {
  359. if (source == null)
  360. throw new ArgumentNullException(nameof(source));
  361. if (keySelector == null)
  362. throw new ArgumentNullException(nameof(keySelector));
  363. if (comparer == null)
  364. throw new ArgumentNullException(nameof(comparer));
  365. return source.MaxBy(keySelector, comparer, CancellationToken.None);
  366. }
  367. public static Task<IList<TSource>> MaxBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
  368. {
  369. if (source == null)
  370. throw new ArgumentNullException(nameof(source));
  371. if (keySelector == null)
  372. throw new ArgumentNullException(nameof(keySelector));
  373. return MaxBy(source, keySelector, Comparer<TKey>.Default, cancellationToken);
  374. }
  375. public static Task<IList<TSource>> MaxBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
  376. {
  377. if (source == null)
  378. throw new ArgumentNullException(nameof(source));
  379. if (keySelector == null)
  380. throw new ArgumentNullException(nameof(keySelector));
  381. if (comparer == null)
  382. throw new ArgumentNullException(nameof(comparer));
  383. return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue), cancellationToken);
  384. }
  385. private static async Task<TSource> Max_<TSource>(IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
  386. {
  387. return (await MaxBy(source, x => x, comparer, cancellationToken)
  388. .ConfigureAwait(false)).First();
  389. }
  390. private static T? NullableMax<T>(T? x, T? y)
  391. where T : struct, IComparable<T>
  392. {
  393. if (!x.HasValue)
  394. return y;
  395. if (!y.HasValue)
  396. return x;
  397. if (x.Value.CompareTo(y.Value) >= 0)
  398. return x;
  399. return y;
  400. }
  401. }
  402. }