Select.Opt.Generated.tt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. <#@ template debug="false" hostspecific="false" language="C#" #>
  5. <#@ assembly name="System.Core" #>
  6. <#@ import namespace="System.Linq" #>
  7. <#@ import namespace="System.Text" #>
  8. <#@ import namespace="System.Collections.Generic" #>
  9. <#@ output extension=".cs" #>
  10. <#
  11. int maxCombine = 4;
  12. #>
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace System.Linq
  16. {
  17. public static partial class AsyncEnumerable
  18. {
  19. <#
  20. for (var i = 2; i <= maxCombine; i++)
  21. {
  22. Func<int, string> getInputType = j => j == 1 ? "TSource" : "TMiddle" + (j - 1);
  23. Func<int, string> getOutputType = j => j == i ? "TResult" : "TMiddle" + j;
  24. Func<int, string> getSelectorType = j => "Func<" + getInputType(j) + ", " + getOutputType(j) + ">";
  25. var types = string.Join(", ", Enumerable.Range(1, i - 1).Select(j => "TMiddle" + j));
  26. var allSelectors = string.Join(", ", Enumerable.Range(1, i).Select(j => getSelectorType(j) + " selector" + j));
  27. var applyAll = Enumerable.Range(1, i).Reverse().Aggregate("", (s, j) => s + "_selector" + j + "(") + "x" + new string(')', i);
  28. #>
  29. private sealed class CombinedSelectors<#=i#><TSource, <#=types#>, TResult> : ICombinedSelectors<TSource, TResult>
  30. {
  31. <#
  32. for (var j = 1; j <= i; j++)
  33. {
  34. var type = getSelectorType(j);
  35. #>
  36. private readonly <#=type#> _selector<#=j#>;
  37. <#
  38. }
  39. #>
  40. public CombinedSelectors<#=i#>(<#=allSelectors#>)
  41. {
  42. <#
  43. for (var j = 1; j <= i; j++)
  44. {
  45. #>
  46. _selector<#=j#> = selector<#=j#>;
  47. <#
  48. }
  49. #>
  50. }
  51. public ICombinedSelectors<TSource, TNewResult> Combine<TNewResult>(Func<TResult, TNewResult> selector) =>
  52. <#
  53. if (i == maxCombine)
  54. {
  55. #>
  56. new CombinedSelectors2<TSource, TResult, TNewResult>(this.Invoke, selector);
  57. <#
  58. }
  59. else
  60. {
  61. #>
  62. new CombinedSelectors<#=i + 1#><TSource, <#=types#>, TResult, TNewResult>(
  63. <#
  64. for (var j = 1; j <= i; j++)
  65. {
  66. #>
  67. _selector<#=j#>,
  68. <#
  69. }
  70. #>
  71. selector
  72. );
  73. <#
  74. }
  75. #>
  76. public TResult Invoke(TSource x) => <#=applyAll#>;
  77. }
  78. <#
  79. }
  80. #>
  81. <#
  82. for (var i = 2; i <= maxCombine; i++)
  83. {
  84. Func<int, string> getInputType = j => j == 1 ? "TSource" : "TMiddle" + (j - 1);
  85. Func<int, string> getOutputType = j => "ValueTask<" + (j == i ? "TResult" : "TMiddle" + j) + ">";
  86. Func<int, string> getSelectorType = j => "Func<" + getInputType(j) + ", " + getOutputType(j) + ">";
  87. var types = string.Join(", ", Enumerable.Range(1, i - 1).Select(j => "TMiddle" + j));
  88. var allSelectors = string.Join(", ", Enumerable.Range(1, i).Select(j => getSelectorType(j) + " selector" + j));
  89. var applyAll = Enumerable.Range(1, i).Reverse().Aggregate("", (s, j) => s + "await _selector" + j + "(") + "x" + string.Join("", Enumerable.Repeat(").ConfigureAwait(false)", i));
  90. #>
  91. private sealed class CombinedAsyncSelectors<#=i#><TSource, <#=types#>, TResult> : ICombinedAsyncSelectors<TSource, TResult>
  92. {
  93. <#
  94. for (var j = 1; j <= i; j++)
  95. {
  96. var type = getSelectorType(j);
  97. #>
  98. private readonly <#=type#> _selector<#=j#>;
  99. <#
  100. }
  101. #>
  102. public CombinedAsyncSelectors<#=i#>(<#=allSelectors#>)
  103. {
  104. <#
  105. for (var j = 1; j <= i; j++)
  106. {
  107. #>
  108. _selector<#=j#> = selector<#=j#>;
  109. <#
  110. }
  111. #>
  112. }
  113. public ICombinedAsyncSelectors<TSource, TNewResult> Combine<TNewResult>(Func<TResult, ValueTask<TNewResult>> selector) =>
  114. <#
  115. if (i == maxCombine)
  116. {
  117. #>
  118. new CombinedAsyncSelectors2<TSource, TResult, TNewResult>(this.Invoke, selector);
  119. <#
  120. }
  121. else
  122. {
  123. #>
  124. new CombinedAsyncSelectors<#=i + 1#><TSource, <#=types#>, TResult, TNewResult>(
  125. <#
  126. for (var j = 1; j <= i; j++)
  127. {
  128. #>
  129. _selector<#=j#>,
  130. <#
  131. }
  132. #>
  133. selector
  134. );
  135. <#
  136. }
  137. #>
  138. public async ValueTask<TResult> Invoke(TSource x) => <#=applyAll#>;
  139. }
  140. <#
  141. }
  142. #>
  143. #if !NO_DEEP_CANCELLATION
  144. <#
  145. for (var i = 2; i <= maxCombine; i++)
  146. {
  147. Func<int, string> getInputType = j => j == 1 ? "TSource" : "TMiddle" + (j - 1);
  148. Func<int, string> getOutputType = j => "ValueTask<" + (j == i ? "TResult" : "TMiddle" + j) + ">";
  149. Func<int, string> getSelectorType = j => "Func<" + getInputType(j) + ", CancellationToken, " + getOutputType(j) + ">";
  150. var types = string.Join(", ", Enumerable.Range(1, i - 1).Select(j => "TMiddle" + j));
  151. var allSelectors = string.Join(", ", Enumerable.Range(1, i).Select(j => getSelectorType(j) + " selector" + j));
  152. var applyAll = Enumerable.Range(1, i).Reverse().Aggregate("", (s, j) => s + "await _selector" + j + "(") + "x" + string.Join("", Enumerable.Repeat(", ct).ConfigureAwait(false)", i));
  153. #>
  154. private sealed class CombinedAsyncSelectorsWithCancellation<#=i#><TSource, <#=types#>, TResult> : ICombinedAsyncSelectorsWithCancellation<TSource, TResult>
  155. {
  156. <#
  157. for (var j = 1; j <= i; j++)
  158. {
  159. var type = getSelectorType(j);
  160. #>
  161. private readonly <#=type#> _selector<#=j#>;
  162. <#
  163. }
  164. #>
  165. public CombinedAsyncSelectorsWithCancellation<#=i#>(<#=allSelectors#>)
  166. {
  167. <#
  168. for (var j = 1; j <= i; j++)
  169. {
  170. #>
  171. _selector<#=j#> = selector<#=j#>;
  172. <#
  173. }
  174. #>
  175. }
  176. public ICombinedAsyncSelectorsWithCancellation<TSource, TNewResult> Combine<TNewResult>(Func<TResult, CancellationToken, ValueTask<TNewResult>> selector) =>
  177. <#
  178. if (i == maxCombine)
  179. {
  180. #>
  181. new CombinedAsyncSelectorsWithCancellation2<TSource, TResult, TNewResult>(this.Invoke, selector);
  182. <#
  183. }
  184. else
  185. {
  186. #>
  187. new CombinedAsyncSelectorsWithCancellation<#=i + 1#><TSource, <#=types#>, TResult, TNewResult>(
  188. <#
  189. for (var j = 1; j <= i; j++)
  190. {
  191. #>
  192. _selector<#=j#>,
  193. <#
  194. }
  195. #>
  196. selector
  197. );
  198. <#
  199. }
  200. #>
  201. public async ValueTask<TResult> Invoke(TSource x, CancellationToken ct) => <#=applyAll#>;
  202. }
  203. <#
  204. }
  205. #>
  206. #endif
  207. }
  208. }