1
0

MinMax.Generated.tt 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 isDeepCancellation = overload.selector.Contains("CancellationToken");
  260. var suffix = overload.invoke.StartsWith("await") ? "Await" : "";
  261. if (isDeepCancellation)
  262. {
  263. suffix += "WithCancellation";
  264. #>
  265. #if !NO_DEEP_CANCELLATION
  266. <#
  267. }
  268. #>
  269. public static ValueTask<<#=t#>> <#=m#><#=suffix#>Async<TSource>(this IAsyncEnumerable<TSource> source, <#=overload.selector#> selector, CancellationToken cancellationToken = default)
  270. {
  271. if (source == null)
  272. throw Error.ArgumentNull(nameof(source));
  273. if (selector == null)
  274. throw Error.ArgumentNull(nameof(selector));
  275. return Core(source, selector, cancellationToken);
  276. static async ValueTask<<#=t#>> Core(IAsyncEnumerable<TSource> _source, <#=overload.selector#> _selector, CancellationToken _cancellationToken)
  277. {
  278. <#
  279. if (!isNullable)
  280. {
  281. #>
  282. <#=t#> value;
  283. await using (var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false))
  284. {
  285. if (!await e.MoveNextAsync())
  286. {
  287. throw Error.NoElements();
  288. }
  289. value = <#=overload.invoke#>;
  290. <#
  291. if (isFloatingPoint && m == "Max")
  292. {
  293. #>
  294. // NaN is ordered less than all other values. We need to do explicit checks
  295. // to ensure this, but once we've found a value that is not NaN we need no
  296. // longer worry about it, so first loop until such a value is found (or not,
  297. // as the case may be).
  298. while (<#=t#>.IsNaN(value))
  299. {
  300. if (!await e.MoveNextAsync())
  301. {
  302. return value;
  303. }
  304. value = <#=overload.invoke#>;
  305. }
  306. <#
  307. }
  308. #>
  309. while (await e.MoveNextAsync())
  310. {
  311. var x = <#=overload.invoke#>;
  312. if (x <#=comparison#> value)
  313. {
  314. value = x;
  315. }
  316. <#
  317. if (isFloatingPoint && m == "Min")
  318. {
  319. #>
  320. else
  321. {
  322. // Normally NaN < anything is false, as is anything < NaN
  323. // However, this leads to some irksome outcomes in Min and Max.
  324. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  325. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  326. // ordering where NaN is smaller than every value, including
  327. // negative infinity.
  328. // Not testing for NaN therefore isn't an option, but since we
  329. // can't find a smaller value, we can short-circuit.
  330. if (<#=t#>.IsNaN(x))
  331. {
  332. return x;
  333. }
  334. }
  335. <#
  336. }
  337. #>
  338. }
  339. }
  340. return value;
  341. <#
  342. }
  343. else
  344. {
  345. #>
  346. <#=t#> value = null;
  347. await using (var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false))
  348. {
  349. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  350. // so we don't have to keep testing for nullity.
  351. do
  352. {
  353. if (!await e.MoveNextAsync())
  354. {
  355. return value;
  356. }
  357. value = <#=overload.invoke#>;
  358. }
  359. while (!value.HasValue);
  360. // Keep hold of the wrapped value, and do comparisons on that, rather than
  361. // using the lifted operation each time.
  362. var valueVal = value.GetValueOrDefault();
  363. <#
  364. if (isInteger && m == "Max")
  365. {
  366. #>
  367. if (valueVal >= 0)
  368. {
  369. // We can fast-path this case where we know HasValue will
  370. // never affect the outcome, without constantly checking
  371. // if we're in such a state. Similar fast-paths could
  372. // be done for other cases, but as all-positive or mostly-
  373. // positive integer values are quite common in real-world
  374. // uses, it's only been done for int? and long?.
  375. while (await e.MoveNextAsync())
  376. {
  377. var cur = <#=overload.invoke#>;
  378. var x = cur.GetValueOrDefault();
  379. if (x <#=comparison#> valueVal)
  380. {
  381. valueVal = x;
  382. value = cur;
  383. }
  384. }
  385. }
  386. else
  387. {
  388. while (await e.MoveNextAsync())
  389. {
  390. var cur = <#=overload.invoke#>;
  391. var x = cur.GetValueOrDefault();
  392. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  393. // unless nulls either never happen or always happen.
  394. if (cur.HasValue & x <#=comparison#> valueVal)
  395. {
  396. valueVal = x;
  397. value = cur;
  398. }
  399. }
  400. }
  401. <#
  402. }
  403. else if (isFloatingPoint && m == "Min")
  404. {
  405. #>
  406. while (await e.MoveNextAsync())
  407. {
  408. var cur = <#=overload.invoke#>;
  409. if (cur.HasValue)
  410. {
  411. var x = cur.GetValueOrDefault();
  412. if (x <#=comparison#> valueVal)
  413. {
  414. valueVal = x;
  415. value = cur;
  416. }
  417. else
  418. {
  419. // Normally NaN < anything is false, as is anything < NaN
  420. // However, this leads to some irksome outcomes in Min and Max.
  421. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  422. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  423. // ordering where NaN is smaller than every value, including
  424. // negative infinity.
  425. // Not testing for NaN therefore isn't an option, but since we
  426. // can't find a smaller value, we can short-circuit.
  427. if (<#=t.TrimEnd('?')#>.IsNaN(x))
  428. {
  429. return cur;
  430. }
  431. }
  432. }
  433. }
  434. <#
  435. }
  436. else
  437. {
  438. if (isFloatingPoint && m == "Max")
  439. {
  440. #>
  441. // NaN is ordered less than all other values. We need to do explicit checks
  442. // to ensure this, but once we've found a value that is not NaN we need no
  443. // longer worry about it, so first loop until such a value is found (or not,
  444. // as the case may be).
  445. while (<#=t.TrimEnd('?')#>.IsNaN(valueVal))
  446. {
  447. if (!await e.MoveNextAsync())
  448. {
  449. return value;
  450. }
  451. var cur = <#=overload.invoke#>;
  452. if (cur.HasValue)
  453. {
  454. valueVal = (value = cur).GetValueOrDefault();
  455. }
  456. }
  457. <#
  458. }
  459. #>
  460. while (await e.MoveNextAsync())
  461. {
  462. var cur = <#=overload.invoke#>;
  463. var x = cur.GetValueOrDefault();
  464. <#
  465. if (shortCircuit)
  466. {
  467. #>
  468. if (cur.HasValue && x <#=comparison#> valueVal)
  469. <#
  470. }
  471. else
  472. {
  473. #>
  474. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  475. // unless nulls either never happen or always happen.
  476. if (cur.HasValue & x <#=comparison#> valueVal)
  477. <#
  478. }
  479. #>
  480. {
  481. valueVal = x;
  482. value = cur;
  483. }
  484. }
  485. <#
  486. }
  487. #>
  488. }
  489. return value;
  490. <#
  491. }
  492. #>
  493. }
  494. }
  495. <#
  496. if (isDeepCancellation)
  497. {
  498. #>
  499. #endif
  500. <#
  501. }
  502. }
  503. #>
  504. <#
  505. }
  506. }
  507. #>
  508. }
  509. }