MinMax.Generated.tt 20 KB

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