Max.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. /// <summary>
  12. /// Returns the maximum element in an async-enumerable sequence.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <param name="source">An async-enumerable sequence to determine the maximum element of.</param>
  16. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  17. /// <returns>A ValueTask containing a single element with the maximum element in the source sequence.</returns>
  18. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  19. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  20. public static ValueTask<TSource> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  21. {
  22. if (source == null)
  23. throw Error.ArgumentNull(nameof(source));
  24. if (default(TSource)! == null) // NB: Null value is desired; JIT-time check.
  25. {
  26. return Core(source, cancellationToken);
  27. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  28. {
  29. var comparer = Comparer<TSource>.Default;
  30. TSource value;
  31. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  32. {
  33. do
  34. {
  35. if (!await e.MoveNextAsync())
  36. {
  37. return default!;
  38. }
  39. value = e.Current;
  40. }
  41. while (value == null);
  42. while (await e.MoveNextAsync())
  43. {
  44. var x = e.Current;
  45. if (x != null && comparer.Compare(x, value) > 0)
  46. {
  47. value = x;
  48. }
  49. }
  50. }
  51. return value;
  52. }
  53. }
  54. else
  55. {
  56. return Core(source, cancellationToken);
  57. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  58. {
  59. var comparer = Comparer<TSource>.Default;
  60. TSource value;
  61. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  62. {
  63. if (!await e.MoveNextAsync())
  64. {
  65. throw Error.NoElements();
  66. }
  67. value = e.Current;
  68. while (await e.MoveNextAsync())
  69. {
  70. var x = e.Current;
  71. if (comparer.Compare(x, value) > 0)
  72. {
  73. value = x;
  74. }
  75. }
  76. }
  77. return value;
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// Invokes a transform function on each element of a sequence and returns the maximum value.
  83. /// </summary>
  84. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  85. /// <typeparam name="TResult">The type of the objects derived from the elements in the source sequence to determine the maximum of.</typeparam>
  86. /// <param name="source">An async-enumerable sequence to determine the minimum element of.</param>
  87. /// <param name="selector">A transform function to apply to each element.</param>
  88. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  89. /// <returns>A ValueTask containing a single element with the value that corresponds to the maximum element in the source sequence.</returns>
  90. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  91. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  92. public static ValueTask<TResult> MaxAsync<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken = default)
  93. {
  94. if (source == null)
  95. throw Error.ArgumentNull(nameof(source));
  96. if (selector == null)
  97. throw Error.ArgumentNull(nameof(selector));
  98. if (default(TResult)! == null) // NB: Null value is desired; JIT-time check.
  99. {
  100. return Core(source, selector, cancellationToken);
  101. static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
  102. {
  103. var comparer = Comparer<TResult>.Default;
  104. TResult value;
  105. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  106. {
  107. do
  108. {
  109. if (!await e.MoveNextAsync())
  110. {
  111. return default!;
  112. }
  113. value = selector(e.Current);
  114. }
  115. while (value == null);
  116. while (await e.MoveNextAsync())
  117. {
  118. var x = selector(e.Current);
  119. if (x != null && comparer.Compare(x, value) > 0)
  120. {
  121. value = x;
  122. }
  123. }
  124. }
  125. return value;
  126. }
  127. }
  128. else
  129. {
  130. return Core(source, selector, cancellationToken);
  131. static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
  132. {
  133. var comparer = Comparer<TResult>.Default;
  134. TResult value;
  135. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  136. {
  137. if (!await e.MoveNextAsync())
  138. {
  139. throw Error.NoElements();
  140. }
  141. value = selector(e.Current);
  142. while (await e.MoveNextAsync())
  143. {
  144. var x = selector(e.Current);
  145. if (comparer.Compare(x, value) > 0)
  146. {
  147. value = x;
  148. }
  149. }
  150. }
  151. return value;
  152. }
  153. }
  154. }
  155. /// <summary>
  156. /// Invokes and awaits a transform function on each element of a sequence and returns the maximum value.
  157. /// </summary>
  158. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  159. /// <typeparam name="TResult">The type of the objects derived from the elements in the source sequence to determine the maximum of.</typeparam>
  160. /// <param name="source">An async-enumerable sequence to determine the minimum element of.</param>
  161. /// <param name="selector">An asynchronous transform function to invoke and await on each element.</param>
  162. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  163. /// <returns>A ValueTask containing a single element with the value that corresponds to the maximum element in the source sequence.</returns>
  164. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  165. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  166. [GenerateAsyncOverload]
  167. private static ValueTask<TResult> MaxAwaitAsyncCore<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TResult>> selector, CancellationToken cancellationToken = default)
  168. {
  169. if (source == null)
  170. throw Error.ArgumentNull(nameof(source));
  171. if (selector == null)
  172. throw Error.ArgumentNull(nameof(selector));
  173. if (default(TResult)! == null) // NB: Null value is desired; JIT-time check.
  174. {
  175. return Core(source, selector, cancellationToken);
  176. static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TResult>> selector, CancellationToken cancellationToken)
  177. {
  178. var comparer = Comparer<TResult>.Default;
  179. TResult value;
  180. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  181. {
  182. do
  183. {
  184. if (!await e.MoveNextAsync())
  185. {
  186. return default!;
  187. }
  188. value = await selector(e.Current).ConfigureAwait(false);
  189. }
  190. while (value == null);
  191. while (await e.MoveNextAsync())
  192. {
  193. var x = await selector(e.Current).ConfigureAwait(false);
  194. if (x != null && comparer.Compare(x, value) > 0)
  195. {
  196. value = x;
  197. }
  198. }
  199. }
  200. return value;
  201. }
  202. }
  203. else
  204. {
  205. return Core(source, selector, cancellationToken);
  206. static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TResult>> selector, CancellationToken cancellationToken)
  207. {
  208. var comparer = Comparer<TResult>.Default;
  209. TResult value;
  210. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  211. {
  212. if (!await e.MoveNextAsync())
  213. {
  214. throw Error.NoElements();
  215. }
  216. value = await selector(e.Current).ConfigureAwait(false);
  217. while (await e.MoveNextAsync())
  218. {
  219. var x = await selector(e.Current).ConfigureAwait(false);
  220. if (comparer.Compare(x, value) > 0)
  221. {
  222. value = x;
  223. }
  224. }
  225. }
  226. return value;
  227. }
  228. }
  229. }
  230. #if !NO_DEEP_CANCELLATION
  231. [GenerateAsyncOverload]
  232. private static ValueTask<TResult> MaxAwaitWithCancellationAsyncCore<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TResult>> selector, CancellationToken cancellationToken = default)
  233. {
  234. if (source == null)
  235. throw Error.ArgumentNull(nameof(source));
  236. if (selector == null)
  237. throw Error.ArgumentNull(nameof(selector));
  238. if (default(TResult)! == null) // NB: Null value is desired; JIT-time check.
  239. {
  240. return Core(source, selector, cancellationToken);
  241. static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TResult>> selector, CancellationToken cancellationToken)
  242. {
  243. var comparer = Comparer<TResult>.Default;
  244. TResult value;
  245. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  246. {
  247. do
  248. {
  249. if (!await e.MoveNextAsync())
  250. {
  251. return default!;
  252. }
  253. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  254. }
  255. while (value == null);
  256. while (await e.MoveNextAsync())
  257. {
  258. var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  259. if (x != null && comparer.Compare(x, value) > 0)
  260. {
  261. value = x;
  262. }
  263. }
  264. }
  265. return value;
  266. }
  267. }
  268. else
  269. {
  270. return Core(source, selector, cancellationToken);
  271. static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TResult>> selector, CancellationToken cancellationToken)
  272. {
  273. var comparer = Comparer<TResult>.Default;
  274. TResult value;
  275. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  276. {
  277. if (!await e.MoveNextAsync())
  278. {
  279. throw Error.NoElements();
  280. }
  281. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  282. while (await e.MoveNextAsync())
  283. {
  284. var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  285. if (comparer.Compare(x, value) > 0)
  286. {
  287. value = x;
  288. }
  289. }
  290. }
  291. return value;
  292. }
  293. }
  294. }
  295. #endif
  296. }
  297. }