Sum.Generated.tt 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 os = new[]
  19. {
  20. new { type = "int", zero = "0", @checked = true },
  21. new { type = "long", zero = "0L", @checked = true },
  22. new { type = "float", zero = "0.0f", @checked = false },
  23. new { type = "double", zero = "0.0", @checked = false },
  24. new { type = "decimal", zero = "0m", @checked = false },
  25. new { type = "int?", zero = "0", @checked = true },
  26. new { type = "long?", zero = "0L", @checked = true },
  27. new { type = "float?", zero = "0.0f", @checked = false },
  28. new { type = "double?", zero = "0.0", @checked = false },
  29. new { type = "decimal?", zero = "0m", @checked = false },
  30. };
  31. foreach (var o in os)
  32. {
  33. var n = o.type.EndsWith("?") ? ".GetValueOrDefault()" : "";
  34. var typeStr = o.type;
  35. if (o.type.EndsWith("?")) {
  36. typeStr = "Nullable{" + o.type.Substring(0, 1).ToUpper() + o.type.Substring(1, o.type.Length - 2) + "}";
  37. }
  38. #>
  39. /// <summary>
  40. /// Computes the sum of a sequence of <see cref="<#=typeStr#>" /> values.
  41. /// </summary>
  42. /// <param name="source">A sequence of <see cref="<#=typeStr#>" /> values to calculate the sum of.</param>
  43. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  44. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  45. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  46. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  47. public static ValueTask<<#=o.type#>> SumAsync(this IAsyncEnumerable<<#=o.type#>> source, CancellationToken cancellationToken = default)
  48. {
  49. if (source == null)
  50. throw Error.ArgumentNull(nameof(source));
  51. return Core(source, cancellationToken);
  52. static async ValueTask<<#=o.type#>> Core(IAsyncEnumerable<<#=o.type#>> source, CancellationToken cancellationToken)
  53. {
  54. var sum = <#=o.zero#>;
  55. await foreach (<#=o.type#> value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  56. {
  57. <#
  58. if (o.@checked)
  59. {
  60. #>
  61. checked
  62. {
  63. sum += value<#=n#>;
  64. }
  65. <#
  66. }
  67. else
  68. {
  69. #>
  70. sum += value<#=n#>;
  71. <#
  72. }
  73. #>
  74. }
  75. return sum;
  76. }
  77. }
  78. /// <summary>
  79. /// Computes the sum of a sequence of <see cref="<#=typeStr#>" /> values that are obtained by invoking a transform function on each element of the input sequence.
  80. /// </summary>
  81. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  82. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  83. /// <param name="selector">A transform function to apply to each element.</param>
  84. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  85. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  86. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  87. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  88. public static ValueTask<<#=o.type#>> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken = default)
  89. {
  90. if (source == null)
  91. throw Error.ArgumentNull(nameof(source));
  92. if (selector == null)
  93. throw Error.ArgumentNull(nameof(selector));
  94. return Core(source, selector, cancellationToken);
  95. static async ValueTask<<#=o.type#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken)
  96. {
  97. var sum = <#=o.zero#>;
  98. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  99. {
  100. var value = selector(item);
  101. <#
  102. if (o.@checked)
  103. {
  104. #>
  105. checked
  106. {
  107. sum += value<#=n#>;
  108. }
  109. <#
  110. }
  111. else
  112. {
  113. #>
  114. sum += value<#=n#>;
  115. <#
  116. }
  117. #>
  118. }
  119. return sum;
  120. }
  121. }
  122. internal static ValueTask<<#=o.type#>> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  123. {
  124. if (source == null)
  125. throw Error.ArgumentNull(nameof(source));
  126. if (selector == null)
  127. throw Error.ArgumentNull(nameof(selector));
  128. return Core(source, selector, cancellationToken);
  129. static async ValueTask<<#=o.type#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  130. {
  131. var sum = <#=o.zero#>;
  132. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  133. {
  134. var value = await selector(item).ConfigureAwait(false);
  135. <#
  136. if (o.@checked)
  137. {
  138. #>
  139. checked
  140. {
  141. sum += value<#=n#>;
  142. }
  143. <#
  144. }
  145. else
  146. {
  147. #>
  148. sum += value<#=n#>;
  149. <#
  150. }
  151. #>
  152. }
  153. return sum;
  154. }
  155. }
  156. #if !NO_DEEP_CANCELLATION
  157. internal static ValueTask<<#=o.type#>> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  158. {
  159. if (source == null)
  160. throw Error.ArgumentNull(nameof(source));
  161. if (selector == null)
  162. throw Error.ArgumentNull(nameof(selector));
  163. return Core(source, selector, cancellationToken);
  164. static async ValueTask<<#=o.type#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  165. {
  166. var sum = <#=o.zero#>;
  167. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  168. {
  169. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  170. <#
  171. if (o.@checked)
  172. {
  173. #>
  174. checked
  175. {
  176. sum += value<#=n#>;
  177. }
  178. <#
  179. }
  180. else
  181. {
  182. #>
  183. sum += value<#=n#>;
  184. <#
  185. }
  186. #>
  187. }
  188. return sum;
  189. }
  190. }
  191. #endif
  192. <#
  193. }
  194. #>
  195. }
  196. }