Max.Generic.cs 9.0 KB

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