MinMax.Generated.tt 19 KB

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