Sum.Generated.tt 9.0 KB

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