Min.cs 21 KB

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