Average.Generated.tt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  122. #if INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
  123. /// <summary>
  124. /// 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.
  125. /// </summary>
  126. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  127. /// <param name="source">A sequence of values to calculate the average of.</param>
  128. /// <param name="selector">A transform function to apply to each element.</param>
  129. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  130. /// <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>
  131. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  132. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  133. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  134. public static ValueTask<<#=o.res#>> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken = default)
  135. {
  136. if (source == null)
  137. throw Error.ArgumentNull(nameof(source));
  138. if (selector == null)
  139. throw Error.ArgumentNull(nameof(selector));
  140. return Core(source, selector, cancellationToken);
  141. static async ValueTask<<#=o.res#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken)
  142. {
  143. <#
  144. if (isNullable)
  145. {
  146. #>
  147. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  148. {
  149. while (await e.MoveNextAsync())
  150. {
  151. var v = selector(e.Current);
  152. if (v.HasValue)
  153. {
  154. <#=o.sum#> sum = v.GetValueOrDefault();
  155. long count = 1;
  156. checked
  157. {
  158. while (await e.MoveNextAsync())
  159. {
  160. v = selector(e.Current);
  161. if (v.HasValue)
  162. {
  163. sum += v.GetValueOrDefault();
  164. ++count;
  165. }
  166. }
  167. }
  168. return <#=res#>;
  169. }
  170. }
  171. }
  172. return null;
  173. <#
  174. }
  175. else
  176. {
  177. #>
  178. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  179. {
  180. if (!await e.MoveNextAsync())
  181. {
  182. throw Error.NoElements();
  183. }
  184. <#=o.sum#> sum = selector(e.Current);
  185. long count = 1;
  186. checked
  187. {
  188. while (await e.MoveNextAsync())
  189. {
  190. sum += selector(e.Current);
  191. ++count;
  192. }
  193. }
  194. return <#=res#>;
  195. }
  196. <#
  197. }
  198. #>
  199. }
  200. }
  201. #endif
  202. /// <summary>
  203. /// 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.
  204. /// </summary>
  205. /// <typeparam name="TSource">The type of elements in the source sequence.</typeparam>
  206. /// <param name="source">An async-enumerable sequence of values to compute the average of.</param>
  207. /// <param name="selector">A transform function to invoke and await on each element of the source sequence.</param>
  208. /// <param name="cancellationToken">An optional cancellation token for cancelling the sequence at any time.</param>
  209. /// <returns>A ValueTask containing the average of the sequence of values.</returns>
  210. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is <see langword="null"/>.</exception>
  211. /// <exception cref="InvalidOperationException">The source sequence is empty.</exception>
  212. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  213. [GenerateAsyncOverload]
  214. [Obsolete("Use AverageAsync in System.Interactive.Async. 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. This functionality has moved to System.Interactive.Async, but the methods that take ValueTask-returning selectors are now overloads of AverageAsync, because this AverageAwaitAsync method did not conform to current .NET naming guidelines.")]
  215. private static ValueTask<<#=o.res#>> AverageAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  216. {
  217. if (source == null)
  218. throw Error.ArgumentNull(nameof(source));
  219. if (selector == null)
  220. throw Error.ArgumentNull(nameof(selector));
  221. return Core(source, selector, cancellationToken);
  222. static async ValueTask<<#=o.res#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  223. {
  224. <#
  225. if (isNullable)
  226. {
  227. #>
  228. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  229. {
  230. while (await e.MoveNextAsync())
  231. {
  232. var v = await selector(e.Current).ConfigureAwait(false);
  233. if (v.HasValue)
  234. {
  235. <#=o.sum#> sum = v.GetValueOrDefault();
  236. long count = 1;
  237. checked
  238. {
  239. while (await e.MoveNextAsync())
  240. {
  241. v = await selector(e.Current).ConfigureAwait(false);
  242. if (v.HasValue)
  243. {
  244. sum += v.GetValueOrDefault();
  245. ++count;
  246. }
  247. }
  248. }
  249. return <#=res#>;
  250. }
  251. }
  252. }
  253. return null;
  254. <#
  255. }
  256. else
  257. {
  258. #>
  259. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  260. {
  261. if (!await e.MoveNextAsync())
  262. {
  263. throw Error.NoElements();
  264. }
  265. <#=o.sum#> sum = await selector(e.Current).ConfigureAwait(false);
  266. long count = 1;
  267. checked
  268. {
  269. while (await e.MoveNextAsync())
  270. {
  271. sum += await selector(e.Current).ConfigureAwait(false);
  272. ++count;
  273. }
  274. }
  275. return <#=res#>;
  276. }
  277. <#
  278. }
  279. #>
  280. }
  281. }
  282. #if !NO_DEEP_CANCELLATION
  283. [GenerateAsyncOverload]
  284. [Obsolete("Use AverageAsync in System.Interactive.Async. 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. This functionality has moved to System.Interactive.Async, but the methods that take cancellable ValueTask-returning selectors are now overloads of AverageAsync, because this AverageAwaitWithCancellationAsyncCore method did not conform to current .NET naming guidelines.")]
  285. private static ValueTask<<#=o.res#>> AverageAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  286. {
  287. if (source == null)
  288. throw Error.ArgumentNull(nameof(source));
  289. if (selector == null)
  290. throw Error.ArgumentNull(nameof(selector));
  291. return Core(source, selector, cancellationToken);
  292. static async ValueTask<<#=o.res#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  293. {
  294. <#
  295. if (isNullable)
  296. {
  297. #>
  298. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  299. {
  300. while (await e.MoveNextAsync())
  301. {
  302. var v = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  303. if (v.HasValue)
  304. {
  305. <#=o.sum#> sum = v.GetValueOrDefault();
  306. long count = 1;
  307. checked
  308. {
  309. while (await e.MoveNextAsync())
  310. {
  311. v = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  312. if (v.HasValue)
  313. {
  314. sum += v.GetValueOrDefault();
  315. ++count;
  316. }
  317. }
  318. }
  319. return <#=res#>;
  320. }
  321. }
  322. }
  323. return null;
  324. <#
  325. }
  326. else
  327. {
  328. #>
  329. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  330. {
  331. if (!await e.MoveNextAsync())
  332. {
  333. throw Error.NoElements();
  334. }
  335. <#=o.sum#> sum = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  336. long count = 1;
  337. checked
  338. {
  339. while (await e.MoveNextAsync())
  340. {
  341. sum += await selector(e.Current, cancellationToken).ConfigureAwait(false);
  342. ++count;
  343. }
  344. }
  345. return <#=res#>;
  346. }
  347. <#
  348. }
  349. #>
  350. }
  351. }
  352. #endif
  353. <#
  354. }
  355. #>
  356. }
  357. }