MinMax.Generated.tt 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <#@ template debug="false" hostspecific="false" language="C#" #>
  2. <#@ assembly name="System.Core" #>
  3. <#@ import namespace="System.Linq" #>
  4. <#@ import namespace="System.Text" #>
  5. <#@ import namespace="System.Collections.Generic" #>
  6. <#@ output extension=".cs" #>
  7. // Licensed to the .NET Foundation under one or more agreements.
  8. // The .NET Foundation licenses this file to you under the MIT license.
  9. // See the LICENSE file in the project root for more information.
  10. using System.Collections.Generic;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace System.Linq
  14. {
  15. public static partial class AsyncEnumerable
  16. {
  17. <#
  18. foreach (var m in new[] { "Max", "Min" })
  19. {
  20. var comparison = m == "Max" ? ">" : "<";
  21. foreach (var t in new[] { "int", "int?", "long", "long?", "float", "float?", "double", "double?", "decimal", "decimal?" })
  22. {
  23. var isFloatingPoint = t.StartsWith("float") || t.StartsWith("double");
  24. var isInteger = t.StartsWith("int") || t.StartsWith("long");
  25. var isNullable = t.EndsWith("?");
  26. var shortCircuit = t.StartsWith("decimal");
  27. #>
  28. public static Task<<#=t#>> <#=m#>Async(this IAsyncEnumerable<<#=t#>> source, CancellationToken cancellationToken = default)
  29. {
  30. if (source == null)
  31. throw Error.ArgumentNull(nameof(source));
  32. return Core(source, cancellationToken);
  33. static async Task<<#=t#>> Core(IAsyncEnumerable<<#=t#>> _source, CancellationToken _cancellationToken)
  34. {
  35. <#
  36. if (!isNullable)
  37. {
  38. #>
  39. <#=t#> value;
  40. var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false);
  41. try // REVIEW: Can use `await using` if we get pattern bind (HAS_AWAIT_USING_PATTERN_BIND)
  42. {
  43. if (!await e.MoveNextAsync())
  44. {
  45. throw Error.NoElements();
  46. }
  47. value = e.Current;
  48. <#
  49. if (isFloatingPoint && m == "Max")
  50. {
  51. #>
  52. // NaN is ordered less than all other values. We need to do explicit checks
  53. // to ensure this, but once we've found a value that is not NaN we need no
  54. // longer worry about it, so first loop until such a value is found (or not,
  55. // as the case may be).
  56. while (<#=t#>.IsNaN(value))
  57. {
  58. if (!await e.MoveNextAsync())
  59. {
  60. return value;
  61. }
  62. value = e.Current;
  63. }
  64. <#
  65. }
  66. #>
  67. while (await e.MoveNextAsync())
  68. {
  69. var x = e.Current;
  70. if (x <#=comparison#> value)
  71. {
  72. value = x;
  73. }
  74. <#
  75. if (isFloatingPoint && m == "Min")
  76. {
  77. #>
  78. else
  79. {
  80. // Normally NaN < anything is false, as is anything < NaN
  81. // However, this leads to some irksome outcomes in Min and Max.
  82. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  83. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  84. // ordering where NaN is smaller than every value, including
  85. // negative infinity.
  86. // Not testing for NaN therefore isn't an option, but since we
  87. // can't find a smaller value, we can short-circuit.
  88. if (<#=t#>.IsNaN(x))
  89. {
  90. return x;
  91. }
  92. }
  93. <#
  94. }
  95. #>
  96. }
  97. }
  98. finally
  99. {
  100. await e.DisposeAsync();
  101. }
  102. return value;
  103. <#
  104. }
  105. else
  106. {
  107. #>
  108. <#=t#> value = null;
  109. var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false);
  110. try // REVIEW: Can use `await using` if we get pattern bind (HAS_AWAIT_USING_PATTERN_BIND)
  111. {
  112. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  113. // so we don't have to keep testing for nullity.
  114. do
  115. {
  116. if (!await e.MoveNextAsync())
  117. {
  118. return value;
  119. }
  120. value = e.Current;
  121. }
  122. while (!value.HasValue);
  123. // Keep hold of the wrapped value, and do comparisons on that, rather than
  124. // using the lifted operation each time.
  125. var valueVal = value.GetValueOrDefault();
  126. <#
  127. if (isInteger && m == "Max")
  128. {
  129. #>
  130. if (valueVal >= 0)
  131. {
  132. // We can fast-path this case where we know HasValue will
  133. // never affect the outcome, without constantly checking
  134. // if we're in such a state. Similar fast-paths could
  135. // be done for other cases, but as all-positive or mostly-
  136. // positive integer values are quite common in real-world
  137. // uses, it's only been done for int? and long?.
  138. while (await e.MoveNextAsync())
  139. {
  140. var cur = e.Current;
  141. var x = cur.GetValueOrDefault();
  142. if (x <#=comparison#> valueVal)
  143. {
  144. valueVal = x;
  145. value = cur;
  146. }
  147. }
  148. }
  149. else
  150. {
  151. while (await e.MoveNextAsync())
  152. {
  153. var cur = e.Current;
  154. var x = cur.GetValueOrDefault();
  155. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  156. // unless nulls either never happen or always happen.
  157. if (cur.HasValue & x <#=comparison#> valueVal)
  158. {
  159. valueVal = x;
  160. value = cur;
  161. }
  162. }
  163. }
  164. <#
  165. }
  166. else if (isFloatingPoint && m == "Min")
  167. {
  168. #>
  169. while (await e.MoveNextAsync())
  170. {
  171. var cur = e.Current;
  172. if (cur.HasValue)
  173. {
  174. var x = cur.GetValueOrDefault();
  175. if (x <#=comparison#> valueVal)
  176. {
  177. valueVal = x;
  178. value = cur;
  179. }
  180. else
  181. {
  182. // Normally NaN < anything is false, as is anything < NaN
  183. // However, this leads to some irksome outcomes in Min and Max.
  184. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  185. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  186. // ordering where NaN is smaller than every value, including
  187. // negative infinity.
  188. // Not testing for NaN therefore isn't an option, but since we
  189. // can't find a smaller value, we can short-circuit.
  190. if (<#=t.TrimEnd('?')#>.IsNaN(x))
  191. {
  192. return cur;
  193. }
  194. }
  195. }
  196. }
  197. <#
  198. }
  199. else
  200. {
  201. if (isFloatingPoint && m == "Max")
  202. {
  203. #>
  204. // NaN is ordered less than all other values. We need to do explicit checks
  205. // to ensure this, but once we've found a value that is not NaN we need no
  206. // longer worry about it, so first loop until such a value is found (or not,
  207. // as the case may be).
  208. while (<#=t.TrimEnd('?')#>.IsNaN(valueVal))
  209. {
  210. if (!await e.MoveNextAsync())
  211. {
  212. return value;
  213. }
  214. var cur = e.Current;
  215. if (cur.HasValue)
  216. {
  217. valueVal = (value = cur).GetValueOrDefault();
  218. }
  219. }
  220. <#
  221. }
  222. #>
  223. while (await e.MoveNextAsync())
  224. {
  225. var cur = e.Current;
  226. var x = cur.GetValueOrDefault();
  227. <#
  228. if (shortCircuit)
  229. {
  230. #>
  231. if (cur.HasValue && x <#=comparison#> valueVal)
  232. <#
  233. }
  234. else
  235. {
  236. #>
  237. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  238. // unless nulls either never happen or always happen.
  239. if (cur.HasValue & x <#=comparison#> valueVal)
  240. <#
  241. }
  242. #>
  243. {
  244. valueVal = x;
  245. value = cur;
  246. }
  247. }
  248. <#
  249. }
  250. #>
  251. }
  252. finally
  253. {
  254. await e.DisposeAsync();
  255. }
  256. return value;
  257. <#
  258. }
  259. #>
  260. }
  261. }
  262. <#
  263. foreach (var overload in new[] {
  264. new { selector = "Func<TSource, " + t + ">", invoke = "_selector(e.Current)" },
  265. new { selector = "Func<TSource, ValueTask<" + t + ">>", invoke = "await _selector(e.Current).ConfigureAwait(false)" },
  266. new { selector = "Func<TSource, CancellationToken, ValueTask<" + t + ">>", invoke = "await _selector(e.Current, _cancellationToken).ConfigureAwait(false)" },
  267. })
  268. {
  269. var isDeepCancellation = overload.selector.Contains("CancellationToken");
  270. if (isDeepCancellation)
  271. {
  272. #>
  273. #if !NO_DEEP_CANCELLATION
  274. <#
  275. }
  276. #>
  277. public static Task<<#=t#>> <#=m#>Async<TSource>(this IAsyncEnumerable<TSource> source, <#=overload.selector#> selector, CancellationToken cancellationToken = default)
  278. {
  279. if (source == null)
  280. throw Error.ArgumentNull(nameof(source));
  281. if (selector == null)
  282. throw Error.ArgumentNull(nameof(selector));
  283. return Core(source, selector, cancellationToken);
  284. static async Task<<#=t#>> Core(IAsyncEnumerable<TSource> _source, <#=overload.selector#> _selector, CancellationToken _cancellationToken)
  285. {
  286. <#
  287. if (!isNullable)
  288. {
  289. #>
  290. <#=t#> value;
  291. var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false);
  292. try // REVIEW: Can use `await using` if we get pattern bind (HAS_AWAIT_USING_PATTERN_BIND)
  293. {
  294. if (!await e.MoveNextAsync())
  295. {
  296. throw Error.NoElements();
  297. }
  298. value = <#=overload.invoke#>;
  299. <#
  300. if (isFloatingPoint && m == "Max")
  301. {
  302. #>
  303. // NaN is ordered less than all other values. We need to do explicit checks
  304. // to ensure this, but once we've found a value that is not NaN we need no
  305. // longer worry about it, so first loop until such a value is found (or not,
  306. // as the case may be).
  307. while (<#=t#>.IsNaN(value))
  308. {
  309. if (!await e.MoveNextAsync())
  310. {
  311. return value;
  312. }
  313. value = <#=overload.invoke#>;
  314. }
  315. <#
  316. }
  317. #>
  318. while (await e.MoveNextAsync())
  319. {
  320. var x = <#=overload.invoke#>;
  321. if (x <#=comparison#> value)
  322. {
  323. value = x;
  324. }
  325. <#
  326. if (isFloatingPoint && m == "Min")
  327. {
  328. #>
  329. else
  330. {
  331. // Normally NaN < anything is false, as is anything < NaN
  332. // However, this leads to some irksome outcomes in Min and Max.
  333. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  334. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  335. // ordering where NaN is smaller than every value, including
  336. // negative infinity.
  337. // Not testing for NaN therefore isn't an option, but since we
  338. // can't find a smaller value, we can short-circuit.
  339. if (<#=t#>.IsNaN(x))
  340. {
  341. return x;
  342. }
  343. }
  344. <#
  345. }
  346. #>
  347. }
  348. }
  349. finally
  350. {
  351. await e.DisposeAsync();
  352. }
  353. return value;
  354. <#
  355. }
  356. else
  357. {
  358. #>
  359. <#=t#> value = null;
  360. var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false);
  361. try // REVIEW: Can use `await using` if we get pattern bind (HAS_AWAIT_USING_PATTERN_BIND)
  362. {
  363. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  364. // so we don't have to keep testing for nullity.
  365. do
  366. {
  367. if (!await e.MoveNextAsync())
  368. {
  369. return value;
  370. }
  371. value = <#=overload.invoke#>;
  372. }
  373. while (!value.HasValue);
  374. // Keep hold of the wrapped value, and do comparisons on that, rather than
  375. // using the lifted operation each time.
  376. var valueVal = value.GetValueOrDefault();
  377. <#
  378. if (isInteger && m == "Max")
  379. {
  380. #>
  381. if (valueVal >= 0)
  382. {
  383. // We can fast-path this case where we know HasValue will
  384. // never affect the outcome, without constantly checking
  385. // if we're in such a state. Similar fast-paths could
  386. // be done for other cases, but as all-positive or mostly-
  387. // positive integer values are quite common in real-world
  388. // uses, it's only been done for int? and long?.
  389. while (await e.MoveNextAsync())
  390. {
  391. var cur = <#=overload.invoke#>;
  392. var x = cur.GetValueOrDefault();
  393. if (x <#=comparison#> valueVal)
  394. {
  395. valueVal = x;
  396. value = cur;
  397. }
  398. }
  399. }
  400. else
  401. {
  402. while (await e.MoveNextAsync())
  403. {
  404. var cur = <#=overload.invoke#>;
  405. var x = cur.GetValueOrDefault();
  406. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  407. // unless nulls either never happen or always happen.
  408. if (cur.HasValue & x <#=comparison#> valueVal)
  409. {
  410. valueVal = x;
  411. value = cur;
  412. }
  413. }
  414. }
  415. <#
  416. }
  417. else if (isFloatingPoint && m == "Min")
  418. {
  419. #>
  420. while (await e.MoveNextAsync())
  421. {
  422. var cur = <#=overload.invoke#>;
  423. if (cur.HasValue)
  424. {
  425. var x = cur.GetValueOrDefault();
  426. if (x <#=comparison#> valueVal)
  427. {
  428. valueVal = x;
  429. value = cur;
  430. }
  431. else
  432. {
  433. // Normally NaN < anything is false, as is anything < NaN
  434. // However, this leads to some irksome outcomes in Min and Max.
  435. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  436. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  437. // ordering where NaN is smaller than every value, including
  438. // negative infinity.
  439. // Not testing for NaN therefore isn't an option, but since we
  440. // can't find a smaller value, we can short-circuit.
  441. if (<#=t.TrimEnd('?')#>.IsNaN(x))
  442. {
  443. return cur;
  444. }
  445. }
  446. }
  447. }
  448. <#
  449. }
  450. else
  451. {
  452. if (isFloatingPoint && m == "Max")
  453. {
  454. #>
  455. // NaN is ordered less than all other values. We need to do explicit checks
  456. // to ensure this, but once we've found a value that is not NaN we need no
  457. // longer worry about it, so first loop until such a value is found (or not,
  458. // as the case may be).
  459. while (<#=t.TrimEnd('?')#>.IsNaN(valueVal))
  460. {
  461. if (!await e.MoveNextAsync())
  462. {
  463. return value;
  464. }
  465. var cur = <#=overload.invoke#>;
  466. if (cur.HasValue)
  467. {
  468. valueVal = (value = cur).GetValueOrDefault();
  469. }
  470. }
  471. <#
  472. }
  473. #>
  474. while (await e.MoveNextAsync())
  475. {
  476. var cur = <#=overload.invoke#>;
  477. var x = cur.GetValueOrDefault();
  478. <#
  479. if (shortCircuit)
  480. {
  481. #>
  482. if (cur.HasValue && x <#=comparison#> valueVal)
  483. <#
  484. }
  485. else
  486. {
  487. #>
  488. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  489. // unless nulls either never happen or always happen.
  490. if (cur.HasValue & x <#=comparison#> valueVal)
  491. <#
  492. }
  493. #>
  494. {
  495. valueVal = x;
  496. value = cur;
  497. }
  498. }
  499. <#
  500. }
  501. #>
  502. }
  503. finally
  504. {
  505. await e.DisposeAsync();
  506. }
  507. return value;
  508. <#
  509. }
  510. #>
  511. }
  512. }
  513. <#
  514. if (isDeepCancellation)
  515. {
  516. #>
  517. #endif
  518. <#
  519. }
  520. }
  521. #>
  522. <#
  523. }
  524. }
  525. #>
  526. }
  527. }