Average.Generated.tt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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", res = "double", sum = "long" },
  21. new { type = "long", res = "double", sum = "long" },
  22. new { type = "float", res = "float", sum = "double" },
  23. new { type = "double", res = "double", sum = "double" },
  24. new { type = "decimal", res = "decimal", sum = "decimal" },
  25. new { type = "int?", res = "double?", sum = "long" },
  26. new { type = "long?", res = "double?", sum = "long" },
  27. new { type = "float?", res = "float?", sum = "double" },
  28. new { type = "double?", res = "double?", sum = "double" },
  29. new { type = "decimal?", res = "decimal?", sum = "decimal" },
  30. };
  31. foreach (var o in os)
  32. {
  33. var isNullable = o.type.EndsWith("?");
  34. var t = o.type.TrimEnd('?');
  35. string res = "";
  36. if (t == "int" || t == "long")
  37. res = "(double)sum / count";
  38. else if (t == "double" || t == "decimal")
  39. res = "sum / count";
  40. else if (t == "float")
  41. res = "(float)(sum / count)";
  42. var typeStr = o.type;
  43. if (isNullable) {
  44. typeStr = "Nullable{" + o.type.Substring(0, 1).ToUpper() + o.type.Substring(1, o.type.Length - 2) + "}";
  45. }
  46. #>
  47. /// <summary>
  48. /// Computes the average of an async-enumerable sequence of <see cref="<#=typeStr#>" /> values.
  49. /// </summary>
  50. /// <param name="source">A sequence of <see cref="<#=typeStr#>" /> values to calculate the average of.</param>
  51. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  52. /// <returns>An async-enumerable sequence containing a single element with the average of the sequence of values.</returns>
  53. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  54. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  55. public static ValueTask<<#=o.res#>> AverageAsync(this IAsyncEnumerable<<#=o.type#>> source, CancellationToken cancellationToken = default)
  56. {
  57. if (source == null)
  58. throw Error.ArgumentNull(nameof(source));
  59. return Core(source, cancellationToken);
  60. static async ValueTask<<#=o.res#>> Core(IAsyncEnumerable<<#=o.type#>> source, CancellationToken cancellationToken)
  61. {
  62. <#
  63. if (isNullable)
  64. {
  65. #>
  66. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  67. {
  68. while (await e.MoveNextAsync())
  69. {
  70. var v = e.Current;
  71. if (v.HasValue)
  72. {
  73. <#=o.sum#> sum = v.GetValueOrDefault();
  74. long count = 1;
  75. checked
  76. {
  77. while (await e.MoveNextAsync())
  78. {
  79. v = e.Current;
  80. if (v.HasValue)
  81. {
  82. sum += v.GetValueOrDefault();
  83. ++count;
  84. }
  85. }
  86. }
  87. return <#=res#>;
  88. }
  89. }
  90. }
  91. return null;
  92. <#
  93. }
  94. else
  95. {
  96. #>
  97. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  98. {
  99. if (!await e.MoveNextAsync())
  100. {
  101. throw Error.NoElements();
  102. }
  103. <#=o.sum#> sum = e.Current;
  104. long count = 1;
  105. checked
  106. {
  107. while (await e.MoveNextAsync())
  108. {
  109. sum += e.Current;
  110. ++count;
  111. }
  112. }
  113. return <#=res#>;
  114. }
  115. <#
  116. }
  117. #>
  118. }
  119. }
  120. /// <summary>
  121. /// Computes the average of an async-enumerable sequence of <see cref="<#=typeStr#>" /> values that are obtained by invoking a transform function on each element of the input sequence.
  122. /// </summary>
  123. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  124. /// <param name="source">A sequence of values to calculate the average of.</param>
  125. /// <param name="selector">A transform function to apply to each element.</param>
  126. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  127. /// <returns>An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null.</returns>
  128. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  129. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  130. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  131. public static ValueTask<<#=o.res#>> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken = default)
  132. {
  133. if (source == null)
  134. throw Error.ArgumentNull(nameof(source));
  135. if (selector == null)
  136. throw Error.ArgumentNull(nameof(selector));
  137. return Core(source, selector, cancellationToken);
  138. static async ValueTask<<#=o.res#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken)
  139. {
  140. <#
  141. if (isNullable)
  142. {
  143. #>
  144. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  145. {
  146. while (await e.MoveNextAsync())
  147. {
  148. var v = selector(e.Current);
  149. if (v.HasValue)
  150. {
  151. <#=o.sum#> sum = v.GetValueOrDefault();
  152. long count = 1;
  153. checked
  154. {
  155. while (await e.MoveNextAsync())
  156. {
  157. v = selector(e.Current);
  158. if (v.HasValue)
  159. {
  160. sum += v.GetValueOrDefault();
  161. ++count;
  162. }
  163. }
  164. }
  165. return <#=res#>;
  166. }
  167. }
  168. }
  169. return null;
  170. <#
  171. }
  172. else
  173. {
  174. #>
  175. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  176. {
  177. if (!await e.MoveNextAsync())
  178. {
  179. throw Error.NoElements();
  180. }
  181. <#=o.sum#> sum = selector(e.Current);
  182. long count = 1;
  183. checked
  184. {
  185. while (await e.MoveNextAsync())
  186. {
  187. sum += selector(e.Current);
  188. ++count;
  189. }
  190. }
  191. return <#=res#>;
  192. }
  193. <#
  194. }
  195. #>
  196. }
  197. }
  198. internal static ValueTask<<#=o.res#>> AverageAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  199. {
  200. if (source == null)
  201. throw Error.ArgumentNull(nameof(source));
  202. if (selector == null)
  203. throw Error.ArgumentNull(nameof(selector));
  204. return Core(source, selector, cancellationToken);
  205. static async ValueTask<<#=o.res#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  206. {
  207. <#
  208. if (isNullable)
  209. {
  210. #>
  211. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  212. {
  213. while (await e.MoveNextAsync())
  214. {
  215. var v = await selector(e.Current).ConfigureAwait(false);
  216. if (v.HasValue)
  217. {
  218. <#=o.sum#> sum = v.GetValueOrDefault();
  219. long count = 1;
  220. checked
  221. {
  222. while (await e.MoveNextAsync())
  223. {
  224. v = await selector(e.Current).ConfigureAwait(false);
  225. if (v.HasValue)
  226. {
  227. sum += v.GetValueOrDefault();
  228. ++count;
  229. }
  230. }
  231. }
  232. return <#=res#>;
  233. }
  234. }
  235. }
  236. return null;
  237. <#
  238. }
  239. else
  240. {
  241. #>
  242. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  243. {
  244. if (!await e.MoveNextAsync())
  245. {
  246. throw Error.NoElements();
  247. }
  248. <#=o.sum#> sum = await selector(e.Current).ConfigureAwait(false);
  249. long count = 1;
  250. checked
  251. {
  252. while (await e.MoveNextAsync())
  253. {
  254. sum += await selector(e.Current).ConfigureAwait(false);
  255. ++count;
  256. }
  257. }
  258. return <#=res#>;
  259. }
  260. <#
  261. }
  262. #>
  263. }
  264. }
  265. #if !NO_DEEP_CANCELLATION
  266. internal static ValueTask<<#=o.res#>> AverageAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  267. {
  268. if (source == null)
  269. throw Error.ArgumentNull(nameof(source));
  270. if (selector == null)
  271. throw Error.ArgumentNull(nameof(selector));
  272. return Core(source, selector, cancellationToken);
  273. static async ValueTask<<#=o.res#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  274. {
  275. <#
  276. if (isNullable)
  277. {
  278. #>
  279. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  280. {
  281. while (await e.MoveNextAsync())
  282. {
  283. var v = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  284. if (v.HasValue)
  285. {
  286. <#=o.sum#> sum = v.GetValueOrDefault();
  287. long count = 1;
  288. checked
  289. {
  290. while (await e.MoveNextAsync())
  291. {
  292. v = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  293. if (v.HasValue)
  294. {
  295. sum += v.GetValueOrDefault();
  296. ++count;
  297. }
  298. }
  299. }
  300. return <#=res#>;
  301. }
  302. }
  303. }
  304. return null;
  305. <#
  306. }
  307. else
  308. {
  309. #>
  310. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  311. {
  312. if (!await e.MoveNextAsync())
  313. {
  314. throw Error.NoElements();
  315. }
  316. <#=o.sum#> sum = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  317. long count = 1;
  318. checked
  319. {
  320. while (await e.MoveNextAsync())
  321. {
  322. sum += await selector(e.Current, cancellationToken).ConfigureAwait(false);
  323. ++count;
  324. }
  325. }
  326. return <#=res#>;
  327. }
  328. <#
  329. }
  330. #>
  331. }
  332. }
  333. #endif
  334. <#
  335. }
  336. #>
  337. }
  338. }