Average.tt 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. <#
  8. var types = new[]
  9. {
  10. (typeof(int), "double"),
  11. (typeof(long), "double"),
  12. (typeof(float),"float"),
  13. (typeof(double),"double"),
  14. (typeof(decimal),"decimal"),
  15. (typeof(int?),"double?"),
  16. (typeof(long?),"double?"),
  17. (typeof(float?),"float?"),
  18. (typeof(double?),"double?"),
  19. (typeof(decimal?),"decimal?"),
  20. };
  21. Func<Type, bool> IsNullable = x => x.IsGenericType;
  22. Func<Type, Type> ElementType = x => IsNullable(x) ? x.GetGenericArguments()[0] : x;
  23. Func<Type, string> TypeName = x => IsNullable(x) ? x.GetGenericArguments()[0].Name + "?" : x.Name;
  24. Func<Type, string> WithSuffix = x => IsNullable(x) ? ".GetValueOrDefault()" : "";
  25. Func<Type, string> CalcResult = x => { var e = ElementType(x); return (e == typeof(int) || e == typeof(long)) ? "(double)sum / count" : (e == typeof(float)) ? "(float)(sum / count)" : "sum / count"; };
  26. #>
  27. using System;
  28. using System.Threading;
  29. using Cysharp.Threading.Tasks.Internal;
  30. namespace Cysharp.Threading.Tasks.Linq
  31. {
  32. public static partial class UniTaskAsyncEnumerable
  33. {
  34. <# foreach(var (t, ret) in types) { #>
  35. public static UniTask<<#= ret #>> AverageAsync(this IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken = default)
  36. {
  37. Error.ThrowArgumentNullException(source, nameof(source));
  38. return Average.AverageAsync(source, cancellationToken);
  39. }
  40. public static UniTask<<#= ret #>> AverageAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, <#= TypeName(t) #>> selector, CancellationToken cancellationToken = default)
  41. {
  42. Error.ThrowArgumentNullException(source, nameof(source));
  43. Error.ThrowArgumentNullException(source, nameof(selector));
  44. return Average.AverageAsync(source, selector, cancellationToken);
  45. }
  46. public static UniTask<<#= ret #>> AverageAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken = default)
  47. {
  48. Error.ThrowArgumentNullException(source, nameof(source));
  49. Error.ThrowArgumentNullException(source, nameof(selector));
  50. return Average.AverageAwaitAsync(source, selector, cancellationToken);
  51. }
  52. public static UniTask<<#= ret #>> AverageAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken = default)
  53. {
  54. Error.ThrowArgumentNullException(source, nameof(source));
  55. Error.ThrowArgumentNullException(source, nameof(selector));
  56. return Average.AverageAwaitWithCancellationAsync(source, selector, cancellationToken);
  57. }
  58. <# } #>
  59. }
  60. internal static class Average
  61. {
  62. <# foreach(var (t, ret) in types) { #>
  63. public static async UniTask<<#= ret #>> AverageAsync(IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken)
  64. {
  65. long count = 0;
  66. <#= TypeName(t) #> sum = 0;
  67. var e = source.GetAsyncEnumerator(cancellationToken);
  68. try
  69. {
  70. while (await e.MoveNextAsync())
  71. {
  72. <# if (IsNullable(t)) { #>
  73. var v = e.Current;
  74. if (v.HasValue)
  75. {
  76. checked
  77. {
  78. sum += v.Value;
  79. count++;
  80. }
  81. }
  82. <# } else { #>
  83. checked
  84. {
  85. sum += e.Current;
  86. count++;
  87. }
  88. <# } #>
  89. }
  90. }
  91. finally
  92. {
  93. if (e != null)
  94. {
  95. await e.DisposeAsync();
  96. }
  97. }
  98. return <#= CalcResult(t) #>;
  99. }
  100. public static async UniTask<<#= ret #>> AverageAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, <#= TypeName(t) #>> selector, CancellationToken cancellationToken)
  101. {
  102. long count = 0;
  103. <#= TypeName(t) #> sum = 0;
  104. var e = source.GetAsyncEnumerator(cancellationToken);
  105. try
  106. {
  107. while (await e.MoveNextAsync())
  108. {
  109. <# if (IsNullable(t)) { #>
  110. var v = selector(e.Current);
  111. if (v.HasValue)
  112. {
  113. checked
  114. {
  115. sum += v.Value;
  116. count++;
  117. }
  118. }
  119. <# } else { #>
  120. checked
  121. {
  122. sum += selector(e.Current);
  123. count++;
  124. }
  125. <# } #>
  126. }
  127. }
  128. finally
  129. {
  130. if (e != null)
  131. {
  132. await e.DisposeAsync();
  133. }
  134. }
  135. return <#= CalcResult(t) #>;
  136. }
  137. public static async UniTask<<#= ret #>> AverageAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken)
  138. {
  139. long count = 0;
  140. <#= TypeName(t) #> sum = 0;
  141. var e = source.GetAsyncEnumerator(cancellationToken);
  142. try
  143. {
  144. while (await e.MoveNextAsync())
  145. {
  146. <# if (IsNullable(t)) { #>
  147. var v = await selector(e.Current);
  148. if (v.HasValue)
  149. {
  150. checked
  151. {
  152. sum += v.Value;
  153. count++;
  154. }
  155. }
  156. <# } else { #>
  157. checked
  158. {
  159. sum += await selector(e.Current);
  160. count++;
  161. }
  162. <# } #>
  163. }
  164. }
  165. finally
  166. {
  167. if (e != null)
  168. {
  169. await e.DisposeAsync();
  170. }
  171. }
  172. return <#= CalcResult(t) #>;
  173. }
  174. public static async UniTask<<#= ret #>> AverageAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken)
  175. {
  176. long count = 0;
  177. <#= TypeName(t) #> sum = 0;
  178. var e = source.GetAsyncEnumerator(cancellationToken);
  179. try
  180. {
  181. while (await e.MoveNextAsync())
  182. {
  183. <# if (IsNullable(t)) { #>
  184. var v = await selector(e.Current, cancellationToken);
  185. if (v.HasValue)
  186. {
  187. checked
  188. {
  189. sum += v.Value;
  190. count++;
  191. }
  192. }
  193. <# } else { #>
  194. checked
  195. {
  196. sum += await selector(e.Current, cancellationToken);
  197. count++;
  198. }
  199. <# } #>
  200. }
  201. }
  202. finally
  203. {
  204. if (e != null)
  205. {
  206. await e.DisposeAsync();
  207. }
  208. }
  209. return <#= CalcResult(t) #>;
  210. }
  211. <# } #>
  212. }
  213. }