MinMax.Generated.tt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 Apache 2.0 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. var ts = new[]
  19. {
  20. "int",
  21. "long",
  22. "float",
  23. "double",
  24. "decimal",
  25. "int?",
  26. "long?",
  27. "float?",
  28. "double?",
  29. "decimal?",
  30. };
  31. foreach (var m in new[] { "Max", "Min" })
  32. {
  33. foreach (var t in ts)
  34. {
  35. var n = t.EndsWith("?");
  36. #>
  37. public static Task<<#=t#>> <#=m#>(this IAsyncEnumerable<<#=t#>> source)
  38. {
  39. if (source == null)
  40. throw new ArgumentNullException(nameof(source));
  41. <#
  42. if (n)
  43. {
  44. #>
  45. return source.Aggregate(default(<#=t#>), new Func<<#=t#>, <#=t#>, <#=t#>>(Nullable<#=m#>), CancellationToken.None);
  46. <#
  47. }
  48. else
  49. {
  50. #>
  51. return source.Aggregate(new Func<<#=t#>, <#=t#>, <#=t#>>(Math.<#=m#>), CancellationToken.None);
  52. <#
  53. }
  54. #>
  55. }
  56. public static Task<<#=t#>> <#=m#>(this IAsyncEnumerable<<#=t#>> source, CancellationToken cancellationToken)
  57. {
  58. if (source == null)
  59. throw new ArgumentNullException(nameof(source));
  60. <#
  61. if (n)
  62. {
  63. #>
  64. return source.Aggregate(default(<#=t#>), new Func<<#=t#>, <#=t#>, <#=t#>>(Nullable<#=m#>), cancellationToken);
  65. <#
  66. }
  67. else
  68. {
  69. #>
  70. return source.Aggregate(new Func<<#=t#>, <#=t#>, <#=t#>>(Math.<#=m#>), cancellationToken);
  71. <#
  72. }
  73. #>
  74. }
  75. public static Task<<#=t#>> <#=m#><TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=t#>> selector)
  76. {
  77. if (source == null)
  78. throw new ArgumentNullException(nameof(source));
  79. if (selector == null)
  80. throw new ArgumentNullException(nameof(selector));
  81. return source.Select(selector).<#=m#>(CancellationToken.None);
  82. }
  83. public static Task<<#=t#>> <#=m#><TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=t#>> selector, CancellationToken cancellationToken)
  84. {
  85. if (source == null)
  86. throw new ArgumentNullException(nameof(source));
  87. if (selector == null)
  88. throw new ArgumentNullException(nameof(selector));
  89. return source.Select(selector).<#=m#>(cancellationToken);
  90. }
  91. public static Task<<#=t#>> <#=m#><TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<<#=t#>>> selector)
  92. {
  93. if (source == null)
  94. throw new ArgumentNullException(nameof(source));
  95. if (selector == null)
  96. throw new ArgumentNullException(nameof(selector));
  97. return source.Select(selector).<#=m#>(CancellationToken.None);
  98. }
  99. public static Task<<#=t#>> <#=m#><TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<<#=t#>>> selector, CancellationToken cancellationToken)
  100. {
  101. if (source == null)
  102. throw new ArgumentNullException(nameof(source));
  103. if (selector == null)
  104. throw new ArgumentNullException(nameof(selector));
  105. return source.Select(selector).<#=m#>(cancellationToken);
  106. }
  107. <#
  108. }
  109. }
  110. #>
  111. private static T? NullableMax<T>(T? x, T? y)
  112. where T : struct, IComparable<T>
  113. {
  114. if (!x.HasValue)
  115. return y;
  116. if (!y.HasValue)
  117. return x;
  118. if (x.Value.CompareTo(y.Value) >= 0)
  119. return x;
  120. return y;
  121. }
  122. private static T? NullableMin<T>(T? x, T? y)
  123. where T : struct, IComparable<T>
  124. {
  125. if (!x.HasValue)
  126. return y;
  127. if (!y.HasValue)
  128. return x;
  129. if (x.Value.CompareTo(y.Value) <= 0)
  130. return x;
  131. return y;
  132. }
  133. }
  134. }