Sum.Generated.tt 6.4 KB

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