Max.Generic.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. 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 Task<TSource> MaxCore<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  12. {
  13. var comparer = Comparer<TSource>.Default;
  14. if (default(TSource) == null)
  15. {
  16. return Core();
  17. async Task<TSource> Core()
  18. {
  19. var value = default(TSource);
  20. var e = source.GetAsyncEnumerator(cancellationToken);
  21. try
  22. {
  23. do
  24. {
  25. if (!await e.MoveNextAsync().ConfigureAwait(false))
  26. {
  27. return value;
  28. }
  29. value = e.Current;
  30. }
  31. while (value == null);
  32. while (await e.MoveNextAsync().ConfigureAwait(false))
  33. {
  34. var x = e.Current;
  35. if (x != null && comparer.Compare(x, value) > 0)
  36. {
  37. value = x;
  38. }
  39. }
  40. }
  41. finally
  42. {
  43. await e.DisposeAsync().ConfigureAwait(false);
  44. }
  45. return value;
  46. }
  47. }
  48. else
  49. {
  50. return Core();
  51. async Task<TSource> Core()
  52. {
  53. var value = default(TSource);
  54. var e = source.GetAsyncEnumerator(cancellationToken);
  55. try
  56. {
  57. if (!await e.MoveNextAsync().ConfigureAwait(false))
  58. {
  59. throw Error.NoElements();
  60. }
  61. value = e.Current;
  62. while (await e.MoveNextAsync().ConfigureAwait(false))
  63. {
  64. var x = e.Current;
  65. if (comparer.Compare(x, value) > 0)
  66. {
  67. value = x;
  68. }
  69. }
  70. }
  71. finally
  72. {
  73. await e.DisposeAsync().ConfigureAwait(false);
  74. }
  75. return value;
  76. }
  77. }
  78. }
  79. private static Task<TResult> MaxCore<TSource, TResult>(IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
  80. {
  81. var comparer = Comparer<TResult>.Default;
  82. if (default(TResult) == null)
  83. {
  84. return Core();
  85. async Task<TResult> Core()
  86. {
  87. var value = default(TResult);
  88. var e = source.GetAsyncEnumerator(cancellationToken);
  89. try
  90. {
  91. do
  92. {
  93. if (!await e.MoveNextAsync().ConfigureAwait(false))
  94. {
  95. return value;
  96. }
  97. value = selector(e.Current);
  98. }
  99. while (value == null);
  100. while (await e.MoveNextAsync().ConfigureAwait(false))
  101. {
  102. var x = selector(e.Current);
  103. if (x != null && comparer.Compare(x, value) > 0)
  104. {
  105. value = x;
  106. }
  107. }
  108. }
  109. finally
  110. {
  111. await e.DisposeAsync().ConfigureAwait(false);
  112. }
  113. return value;
  114. }
  115. }
  116. else
  117. {
  118. return Core();
  119. async Task<TResult> Core()
  120. {
  121. var value = default(TResult);
  122. var e = source.GetAsyncEnumerator(cancellationToken);
  123. try
  124. {
  125. if (!await e.MoveNextAsync().ConfigureAwait(false))
  126. {
  127. throw Error.NoElements();
  128. }
  129. value = selector(e.Current);
  130. while (await e.MoveNextAsync().ConfigureAwait(false))
  131. {
  132. var x = selector(e.Current);
  133. if (comparer.Compare(x, value) > 0)
  134. {
  135. value = x;
  136. }
  137. }
  138. }
  139. finally
  140. {
  141. await e.DisposeAsync().ConfigureAwait(false);
  142. }
  143. return value;
  144. }
  145. }
  146. }
  147. private static Task<TResult> MaxCore<TSource, TResult>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TResult>> selector, CancellationToken cancellationToken)
  148. {
  149. var comparer = Comparer<TResult>.Default;
  150. if (default(TResult) == null)
  151. {
  152. return Core();
  153. async Task<TResult> Core()
  154. {
  155. var value = default(TResult);
  156. var e = source.GetAsyncEnumerator(cancellationToken);
  157. try
  158. {
  159. do
  160. {
  161. if (!await e.MoveNextAsync().ConfigureAwait(false))
  162. {
  163. return value;
  164. }
  165. value = await selector(e.Current).ConfigureAwait(false);
  166. }
  167. while (value == null);
  168. while (await e.MoveNextAsync().ConfigureAwait(false))
  169. {
  170. var x = await selector(e.Current).ConfigureAwait(false);
  171. if (x != null && comparer.Compare(x, value) > 0)
  172. {
  173. value = x;
  174. }
  175. }
  176. }
  177. finally
  178. {
  179. await e.DisposeAsync().ConfigureAwait(false);
  180. }
  181. return value;
  182. }
  183. }
  184. else
  185. {
  186. return Core();
  187. async Task<TResult> Core()
  188. {
  189. var value = default(TResult);
  190. var e = source.GetAsyncEnumerator(cancellationToken);
  191. try
  192. {
  193. if (!await e.MoveNextAsync().ConfigureAwait(false))
  194. {
  195. throw Error.NoElements();
  196. }
  197. value = await selector(e.Current).ConfigureAwait(false);
  198. while (await e.MoveNextAsync().ConfigureAwait(false))
  199. {
  200. var x = await selector(e.Current).ConfigureAwait(false);
  201. if (comparer.Compare(x, value) > 0)
  202. {
  203. value = x;
  204. }
  205. }
  206. }
  207. finally
  208. {
  209. await e.DisposeAsync().ConfigureAwait(false);
  210. }
  211. return value;
  212. }
  213. }
  214. }
  215. #if !NO_DEEP_CANCELLATION
  216. private static Task<TResult> MaxCore<TSource, TResult>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TResult>> selector, CancellationToken cancellationToken)
  217. {
  218. var comparer = Comparer<TResult>.Default;
  219. if (default(TResult) == null)
  220. {
  221. return Core();
  222. async Task<TResult> Core()
  223. {
  224. var value = default(TResult);
  225. var e = source.GetAsyncEnumerator(cancellationToken);
  226. try
  227. {
  228. do
  229. {
  230. if (!await e.MoveNextAsync().ConfigureAwait(false))
  231. {
  232. return value;
  233. }
  234. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  235. }
  236. while (value == null);
  237. while (await e.MoveNextAsync().ConfigureAwait(false))
  238. {
  239. var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  240. if (x != null && comparer.Compare(x, value) > 0)
  241. {
  242. value = x;
  243. }
  244. }
  245. }
  246. finally
  247. {
  248. await e.DisposeAsync().ConfigureAwait(false);
  249. }
  250. return value;
  251. }
  252. }
  253. else
  254. {
  255. return Core();
  256. async Task<TResult> Core()
  257. {
  258. var value = default(TResult);
  259. var e = source.GetAsyncEnumerator(cancellationToken);
  260. try
  261. {
  262. if (!await e.MoveNextAsync().ConfigureAwait(false))
  263. {
  264. throw Error.NoElements();
  265. }
  266. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  267. while (await e.MoveNextAsync().ConfigureAwait(false))
  268. {
  269. var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  270. if (comparer.Compare(x, value) > 0)
  271. {
  272. value = x;
  273. }
  274. }
  275. }
  276. finally
  277. {
  278. await e.DisposeAsync().ConfigureAwait(false);
  279. }
  280. return value;
  281. }
  282. }
  283. }
  284. #endif
  285. }
  286. }