Average.Generated.tt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. 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. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  48. /// <summary>
  49. /// Computes the average of an async-enumerable sequence of <see cref="<#=typeStr#>" /> values.
  50. /// </summary>
  51. /// <param name="source">A sequence of <see cref="<#=typeStr#>" /> values to calculate the average of.</param>
  52. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  53. /// <returns>An async-enumerable sequence containing a single element with the average of the sequence of values.</returns>
  54. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  55. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  56. public static ValueTask<<#=o.res#>> AverageAsync(this IAsyncEnumerable<<#=o.type#>> source, CancellationToken cancellationToken = default)
  57. {
  58. if (source == null)
  59. throw Error.ArgumentNull(nameof(source));
  60. return Core(source, cancellationToken);
  61. static async ValueTask<<#=o.res#>> Core(IAsyncEnumerable<<#=o.type#>> source, CancellationToken cancellationToken)
  62. {
  63. <#
  64. if (isNullable)
  65. {
  66. #>
  67. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  68. {
  69. while (await e.MoveNextAsync())
  70. {
  71. var v = e.Current;
  72. if (v.HasValue)
  73. {
  74. <#=o.sum#> sum = v.GetValueOrDefault();
  75. long count = 1;
  76. checked
  77. {
  78. while (await e.MoveNextAsync())
  79. {
  80. v = e.Current;
  81. if (v.HasValue)
  82. {
  83. sum += v.GetValueOrDefault();
  84. ++count;
  85. }
  86. }
  87. }
  88. return <#=res#>;
  89. }
  90. }
  91. }
  92. return null;
  93. <#
  94. }
  95. else
  96. {
  97. #>
  98. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  99. {
  100. if (!await e.MoveNextAsync())
  101. {
  102. throw Error.NoElements();
  103. }
  104. <#=o.sum#> sum = e.Current;
  105. long count = 1;
  106. checked
  107. {
  108. while (await e.MoveNextAsync())
  109. {
  110. sum += e.Current;
  111. ++count;
  112. }
  113. }
  114. return <#=res#>;
  115. }
  116. <#
  117. }
  118. #>
  119. }
  120. }
  121. /// <summary>
  122. /// 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.
  123. /// </summary>
  124. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  125. /// <param name="source">A sequence of values to calculate the average of.</param>
  126. /// <param name="selector">A transform function to apply to each element.</param>
  127. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  128. /// <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>
  129. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  130. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  131. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  132. public static ValueTask<<#=o.res#>> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=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.res#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken)
  140. {
  141. <#
  142. if (isNullable)
  143. {
  144. #>
  145. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  146. {
  147. while (await e.MoveNextAsync())
  148. {
  149. var v = selector(e.Current);
  150. if (v.HasValue)
  151. {
  152. <#=o.sum#> sum = v.GetValueOrDefault();
  153. long count = 1;
  154. checked
  155. {
  156. while (await e.MoveNextAsync())
  157. {
  158. v = selector(e.Current);
  159. if (v.HasValue)
  160. {
  161. sum += v.GetValueOrDefault();
  162. ++count;
  163. }
  164. }
  165. }
  166. return <#=res#>;
  167. }
  168. }
  169. }
  170. return null;
  171. <#
  172. }
  173. else
  174. {
  175. #>
  176. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  177. {
  178. if (!await e.MoveNextAsync())
  179. {
  180. throw Error.NoElements();
  181. }
  182. <#=o.sum#> sum = selector(e.Current);
  183. long count = 1;
  184. checked
  185. {
  186. while (await e.MoveNextAsync())
  187. {
  188. sum += selector(e.Current);
  189. ++count;
  190. }
  191. }
  192. return <#=res#>;
  193. }
  194. <#
  195. }
  196. #>
  197. }
  198. }
  199. #endif
  200. /// <summary>
  201. /// Computes the average of an async-enumerable sequence of <see cref="int"/> values that are obtained by invoking an asynchronous transform function on each element of the source sequence and awaiting the result.
  202. /// </summary>
  203. /// <typeparam name="TSource">The type of elements in the source sequence.</typeparam>
  204. /// <param name="source">An async-enumerable sequence of values to compute the average of.</param>
  205. /// <param name="selector">A transform function to invoke and await on each element of the source sequence.</param>
  206. /// <param name="cancellationToken">An optional cancellation token for cancelling the sequence at any time.</param>
  207. /// <returns>A ValueTask containing the average of the sequence of values.</returns>
  208. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is <see langword="null"/>.</exception>
  209. /// <exception cref="InvalidOperationException">The source sequence is empty.</exception>
  210. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  211. [GenerateAsyncOverload]
  212. [Obsolete("Use Select then AverageAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its AverageAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use AverageAsync on the resulting sequence.")]
  213. private static ValueTask<<#=o.res#>> AverageAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  214. {
  215. if (source == null)
  216. throw Error.ArgumentNull(nameof(source));
  217. if (selector == null)
  218. throw Error.ArgumentNull(nameof(selector));
  219. return Core(source, selector, cancellationToken);
  220. static async ValueTask<<#=o.res#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  221. {
  222. <#
  223. if (isNullable)
  224. {
  225. #>
  226. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  227. {
  228. while (await e.MoveNextAsync())
  229. {
  230. var v = await selector(e.Current).ConfigureAwait(false);
  231. if (v.HasValue)
  232. {
  233. <#=o.sum#> sum = v.GetValueOrDefault();
  234. long count = 1;
  235. checked
  236. {
  237. while (await e.MoveNextAsync())
  238. {
  239. v = await selector(e.Current).ConfigureAwait(false);
  240. if (v.HasValue)
  241. {
  242. sum += v.GetValueOrDefault();
  243. ++count;
  244. }
  245. }
  246. }
  247. return <#=res#>;
  248. }
  249. }
  250. }
  251. return null;
  252. <#
  253. }
  254. else
  255. {
  256. #>
  257. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  258. {
  259. if (!await e.MoveNextAsync())
  260. {
  261. throw Error.NoElements();
  262. }
  263. <#=o.sum#> sum = await selector(e.Current).ConfigureAwait(false);
  264. long count = 1;
  265. checked
  266. {
  267. while (await e.MoveNextAsync())
  268. {
  269. sum += await selector(e.Current).ConfigureAwait(false);
  270. ++count;
  271. }
  272. }
  273. return <#=res#>;
  274. }
  275. <#
  276. }
  277. #>
  278. }
  279. }
  280. #if !NO_DEEP_CANCELLATION
  281. [GenerateAsyncOverload]
  282. [Obsolete("Use Select then AverageAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its AverageAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use AverageAsync on the resulting sequence.")]
  283. private static ValueTask<<#=o.res#>> AverageAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  284. {
  285. if (source == null)
  286. throw Error.ArgumentNull(nameof(source));
  287. if (selector == null)
  288. throw Error.ArgumentNull(nameof(selector));
  289. return Core(source, selector, cancellationToken);
  290. static async ValueTask<<#=o.res#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  291. {
  292. <#
  293. if (isNullable)
  294. {
  295. #>
  296. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  297. {
  298. while (await e.MoveNextAsync())
  299. {
  300. var v = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  301. if (v.HasValue)
  302. {
  303. <#=o.sum#> sum = v.GetValueOrDefault();
  304. long count = 1;
  305. checked
  306. {
  307. while (await e.MoveNextAsync())
  308. {
  309. v = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  310. if (v.HasValue)
  311. {
  312. sum += v.GetValueOrDefault();
  313. ++count;
  314. }
  315. }
  316. }
  317. return <#=res#>;
  318. }
  319. }
  320. }
  321. return null;
  322. <#
  323. }
  324. else
  325. {
  326. #>
  327. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  328. {
  329. if (!await e.MoveNextAsync())
  330. {
  331. throw Error.NoElements();
  332. }
  333. <#=o.sum#> sum = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  334. long count = 1;
  335. checked
  336. {
  337. while (await e.MoveNextAsync())
  338. {
  339. sum += await selector(e.Current, cancellationToken).ConfigureAwait(false);
  340. ++count;
  341. }
  342. }
  343. return <#=res#>;
  344. }
  345. <#
  346. }
  347. #>
  348. }
  349. }
  350. #endif
  351. <#
  352. }
  353. #>
  354. }
  355. }