1
0

Sum.Generated.tt 7.4 KB

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