Average.Generated.tt 16 KB

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