Sum.Generated.tt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 nonNullType = o.type.EndsWith("?") ? o.type.TrimEnd('?') : o.type;
  35. #>
  36. public static Task<<#=o.type#>> SumAsync(this IAsyncEnumerable<<#=o.type#>> source)
  37. {
  38. if (source == null)
  39. throw Error.ArgumentNull(nameof(source));
  40. return SumCore(source, CancellationToken.None);
  41. }
  42. public static Task<<#=o.type#>> SumAsync(this IAsyncEnumerable<<#=o.type#>> source, CancellationToken cancellationToken)
  43. {
  44. if (source == null)
  45. throw Error.ArgumentNull(nameof(source));
  46. return SumCore(source, cancellationToken);
  47. }
  48. public static Task<<#=o.type#>> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector)
  49. {
  50. if (source == null)
  51. throw Error.ArgumentNull(nameof(source));
  52. if (selector == null)
  53. throw Error.ArgumentNull(nameof(selector));
  54. return SumCore(source, selector, CancellationToken.None);
  55. }
  56. public static Task<<#=o.type#>> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken)
  57. {
  58. if (source == null)
  59. throw Error.ArgumentNull(nameof(source));
  60. if (selector == null)
  61. throw Error.ArgumentNull(nameof(selector));
  62. return SumCore(source, selector, cancellationToken);
  63. }
  64. public static Task<<#=o.type#>> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector)
  65. {
  66. if (source == null)
  67. throw Error.ArgumentNull(nameof(source));
  68. if (selector == null)
  69. throw Error.ArgumentNull(nameof(selector));
  70. return SumCore(source, selector, CancellationToken.None);
  71. }
  72. public static Task<<#=o.type#>> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  73. {
  74. if (source == null)
  75. throw Error.ArgumentNull(nameof(source));
  76. if (selector == null)
  77. throw Error.ArgumentNull(nameof(selector));
  78. return SumCore(source, selector, cancellationToken);
  79. }
  80. #if !NO_DEEP_CANCELLATION
  81. public static Task<<#=o.type#>> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  82. {
  83. if (source == null)
  84. throw Error.ArgumentNull(nameof(source));
  85. if (selector == null)
  86. throw Error.ArgumentNull(nameof(selector));
  87. return SumCore(source, selector, cancellationToken);
  88. }
  89. #endif
  90. private static async Task<<#=o.type#>> SumCore(IAsyncEnumerable<<#=o.type#>> source, CancellationToken cancellationToken)
  91. {
  92. <#=nonNullType#> sum = <#=o.zero#>;
  93. IAsyncEnumerator<<#=o.type#>> e = source.GetAsyncEnumerator(cancellationToken);
  94. try
  95. {
  96. while (await e.MoveNextAsync().ConfigureAwait(false))
  97. {
  98. <#
  99. if (o.@checked)
  100. {
  101. #>
  102. checked
  103. {
  104. sum += e.Current<#=n#>;
  105. }
  106. <#
  107. }
  108. else
  109. {
  110. #>
  111. sum += e.Current<#=n#>;
  112. <#
  113. }
  114. #>
  115. }
  116. }
  117. finally
  118. {
  119. await e.DisposeAsync().ConfigureAwait(false);
  120. }
  121. return sum;
  122. }
  123. private static async Task<<#=o.type#>> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken)
  124. {
  125. <#=nonNullType#> sum = <#=o.zero#>;
  126. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  127. try
  128. {
  129. while (await e.MoveNextAsync().ConfigureAwait(false))
  130. {
  131. <#=o.type#> value = selector(e.Current);
  132. <#
  133. if (o.@checked)
  134. {
  135. #>
  136. checked
  137. {
  138. sum += value<#=n#>;
  139. }
  140. <#
  141. }
  142. else
  143. {
  144. #>
  145. sum += value<#=n#>;
  146. <#
  147. }
  148. #>
  149. }
  150. }
  151. finally
  152. {
  153. await e.DisposeAsync().ConfigureAwait(false);
  154. }
  155. return sum;
  156. }
  157. private static async Task<<#=o.type#>> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  158. {
  159. <#=nonNullType#> sum = <#=o.zero#>;
  160. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  161. try
  162. {
  163. while (await e.MoveNextAsync().ConfigureAwait(false))
  164. {
  165. <#=o.type#> value = await selector(e.Current).ConfigureAwait(false);
  166. <#
  167. if (o.@checked)
  168. {
  169. #>
  170. checked
  171. {
  172. sum += value<#=n#>;
  173. }
  174. <#
  175. }
  176. else
  177. {
  178. #>
  179. sum += value<#=n#>;
  180. <#
  181. }
  182. #>
  183. }
  184. }
  185. finally
  186. {
  187. await e.DisposeAsync().ConfigureAwait(false);
  188. }
  189. return sum;
  190. }
  191. #if !NO_DEEP_CANCELLATION
  192. private static async Task<<#=o.type#>> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  193. {
  194. <#=nonNullType#> sum = <#=o.zero#>;
  195. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  196. try
  197. {
  198. while (await e.MoveNextAsync().ConfigureAwait(false))
  199. {
  200. <#=o.type#> value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  201. <#
  202. if (o.@checked)
  203. {
  204. #>
  205. checked
  206. {
  207. sum += value<#=n#>;
  208. }
  209. <#
  210. }
  211. else
  212. {
  213. #>
  214. sum += value<#=n#>;
  215. <#
  216. }
  217. #>
  218. }
  219. }
  220. finally
  221. {
  222. await e.DisposeAsync().ConfigureAwait(false);
  223. }
  224. return sum;
  225. }
  226. #endif
  227. <#
  228. }
  229. #>
  230. }
  231. }