1
0

Average.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. private static async Task<double> AverageCore(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  12. {
  13. var e = source.GetAsyncEnumerator(cancellationToken);
  14. try
  15. {
  16. if (!await e.MoveNextAsync().ConfigureAwait(false))
  17. {
  18. throw Error.NoElements();
  19. }
  20. long sum = e.Current;
  21. long count = 1;
  22. checked
  23. {
  24. while (await e.MoveNextAsync().ConfigureAwait(false))
  25. {
  26. sum += e.Current;
  27. ++count;
  28. }
  29. }
  30. return (double)sum / count;
  31. }
  32. finally
  33. {
  34. await e.DisposeAsync().ConfigureAwait(false);
  35. }
  36. }
  37. private static async Task<double?> AverageCore(IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  38. {
  39. var e = source.GetAsyncEnumerator(cancellationToken);
  40. try
  41. {
  42. while (await e.MoveNextAsync().ConfigureAwait(false))
  43. {
  44. var v = e.Current;
  45. if (v.HasValue)
  46. {
  47. long sum = v.GetValueOrDefault();
  48. long count = 1;
  49. checked
  50. {
  51. while (await e.MoveNextAsync().ConfigureAwait(false))
  52. {
  53. v = e.Current;
  54. if (v.HasValue)
  55. {
  56. sum += v.GetValueOrDefault();
  57. ++count;
  58. }
  59. }
  60. }
  61. return (double)sum / count;
  62. }
  63. }
  64. }
  65. finally
  66. {
  67. await e.DisposeAsync().ConfigureAwait(false);
  68. }
  69. return null;
  70. }
  71. private static async Task<double> AverageCore(IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  72. {
  73. var e = source.GetAsyncEnumerator(cancellationToken);
  74. try
  75. {
  76. if (!await e.MoveNextAsync().ConfigureAwait(false))
  77. {
  78. throw Error.NoElements();
  79. }
  80. var sum = e.Current;
  81. long count = 1;
  82. checked
  83. {
  84. while (await e.MoveNextAsync().ConfigureAwait(false))
  85. {
  86. sum += e.Current;
  87. ++count;
  88. }
  89. }
  90. return (double)sum / count;
  91. }
  92. finally
  93. {
  94. await e.DisposeAsync().ConfigureAwait(false);
  95. }
  96. }
  97. private static async Task<double?> AverageCore(IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  98. {
  99. var e = source.GetAsyncEnumerator(cancellationToken);
  100. try
  101. {
  102. while (await e.MoveNextAsync().ConfigureAwait(false))
  103. {
  104. var v = e.Current;
  105. if (v.HasValue)
  106. {
  107. var sum = v.GetValueOrDefault();
  108. long count = 1;
  109. checked
  110. {
  111. while (await e.MoveNextAsync().ConfigureAwait(false))
  112. {
  113. v = e.Current;
  114. if (v.HasValue)
  115. {
  116. sum += v.GetValueOrDefault();
  117. ++count;
  118. }
  119. }
  120. }
  121. return (double)sum / count;
  122. }
  123. }
  124. }
  125. finally
  126. {
  127. await e.DisposeAsync().ConfigureAwait(false);
  128. }
  129. return null;
  130. }
  131. private static async Task<double> AverageCore(IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  132. {
  133. var e = source.GetAsyncEnumerator(cancellationToken);
  134. try
  135. {
  136. if (!await e.MoveNextAsync().ConfigureAwait(false))
  137. {
  138. throw Error.NoElements();
  139. }
  140. var sum = e.Current;
  141. long count = 1;
  142. while (await e.MoveNextAsync().ConfigureAwait(false))
  143. {
  144. // There is an opportunity to short-circuit here, in that if e.Current is
  145. // ever NaN then the result will always be NaN. Assuming that this case is
  146. // rare enough that not checking is the better approach generally.
  147. sum += e.Current;
  148. ++count;
  149. }
  150. return sum / count;
  151. }
  152. finally
  153. {
  154. await e.DisposeAsync().ConfigureAwait(false);
  155. }
  156. }
  157. private static async Task<double?> AverageCore(IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  158. {
  159. var e = source.GetAsyncEnumerator(cancellationToken);
  160. try
  161. {
  162. while (await e.MoveNextAsync().ConfigureAwait(false))
  163. {
  164. var v = e.Current;
  165. if (v.HasValue)
  166. {
  167. var sum = v.GetValueOrDefault();
  168. long count = 1;
  169. checked
  170. {
  171. while (await e.MoveNextAsync().ConfigureAwait(false))
  172. {
  173. v = e.Current;
  174. if (v.HasValue)
  175. {
  176. sum += v.GetValueOrDefault();
  177. ++count;
  178. }
  179. }
  180. }
  181. return sum / count;
  182. }
  183. }
  184. }
  185. finally
  186. {
  187. await e.DisposeAsync().ConfigureAwait(false);
  188. }
  189. return null;
  190. }
  191. private static async Task<float> AverageCore(IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  192. {
  193. var e = source.GetAsyncEnumerator(cancellationToken);
  194. try
  195. {
  196. if (!await e.MoveNextAsync().ConfigureAwait(false))
  197. {
  198. throw Error.NoElements();
  199. }
  200. double sum = e.Current;
  201. long count = 1;
  202. while (await e.MoveNextAsync().ConfigureAwait(false))
  203. {
  204. sum += e.Current;
  205. ++count;
  206. }
  207. return (float)(sum / count);
  208. }
  209. finally
  210. {
  211. await e.DisposeAsync().ConfigureAwait(false);
  212. }
  213. }
  214. private static async Task<float?> AverageCore(IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  215. {
  216. var e = source.GetAsyncEnumerator(cancellationToken);
  217. try
  218. {
  219. while (await e.MoveNextAsync().ConfigureAwait(false))
  220. {
  221. var v = e.Current;
  222. if (v.HasValue)
  223. {
  224. double sum = v.GetValueOrDefault();
  225. long count = 1;
  226. checked
  227. {
  228. while (await e.MoveNextAsync().ConfigureAwait(false))
  229. {
  230. v = e.Current;
  231. if (v.HasValue)
  232. {
  233. sum += v.GetValueOrDefault();
  234. ++count;
  235. }
  236. }
  237. }
  238. return (float)(sum / count);
  239. }
  240. }
  241. }
  242. finally
  243. {
  244. await e.DisposeAsync().ConfigureAwait(false);
  245. }
  246. return null;
  247. }
  248. private static async Task<decimal> AverageCore(IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  249. {
  250. var e = source.GetAsyncEnumerator(cancellationToken);
  251. try
  252. {
  253. if (!await e.MoveNextAsync().ConfigureAwait(false))
  254. {
  255. throw Error.NoElements();
  256. }
  257. var sum = e.Current;
  258. long count = 1;
  259. while (await e.MoveNextAsync().ConfigureAwait(false))
  260. {
  261. sum += e.Current;
  262. ++count;
  263. }
  264. return sum / count;
  265. }
  266. finally
  267. {
  268. await e.DisposeAsync().ConfigureAwait(false);
  269. }
  270. }
  271. private static async Task<decimal?> AverageCore(IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  272. {
  273. var e = source.GetAsyncEnumerator(cancellationToken);
  274. try
  275. {
  276. while (await e.MoveNextAsync().ConfigureAwait(false))
  277. {
  278. var v = e.Current;
  279. if (v.HasValue)
  280. {
  281. var sum = v.GetValueOrDefault();
  282. long count = 1;
  283. while (await e.MoveNextAsync().ConfigureAwait(false))
  284. {
  285. v = e.Current;
  286. if (v.HasValue)
  287. {
  288. sum += v.GetValueOrDefault();
  289. ++count;
  290. }
  291. }
  292. return sum / count;
  293. }
  294. }
  295. }
  296. finally
  297. {
  298. await e.DisposeAsync().ConfigureAwait(false);
  299. }
  300. return null;
  301. }
  302. }
  303. }