Max.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 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. internal static ValueTask<TResult> MaxAwaitAsyncCore<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TResult>> selector, CancellationToken cancellationToken = default)
  156. {
  157. if (source == null)
  158. throw Error.ArgumentNull(nameof(source));
  159. if (selector == null)
  160. throw Error.ArgumentNull(nameof(selector));
  161. if (default(TResult)! == null) // NB: Null value is desired; JIT-time check.
  162. {
  163. return Core(source, selector, cancellationToken);
  164. static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TResult>> selector, CancellationToken cancellationToken)
  165. {
  166. var comparer = Comparer<TResult>.Default;
  167. TResult value;
  168. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  169. {
  170. do
  171. {
  172. if (!await e.MoveNextAsync())
  173. {
  174. return default!;
  175. }
  176. value = await selector(e.Current).ConfigureAwait(false);
  177. }
  178. while (value == null);
  179. while (await e.MoveNextAsync())
  180. {
  181. var x = await selector(e.Current).ConfigureAwait(false);
  182. if (x != null && comparer.Compare(x, value) > 0)
  183. {
  184. value = x;
  185. }
  186. }
  187. }
  188. return value;
  189. }
  190. }
  191. else
  192. {
  193. return Core(source, selector, cancellationToken);
  194. static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TResult>> selector, CancellationToken cancellationToken)
  195. {
  196. var comparer = Comparer<TResult>.Default;
  197. TResult value;
  198. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  199. {
  200. if (!await e.MoveNextAsync())
  201. {
  202. throw Error.NoElements();
  203. }
  204. value = await selector(e.Current).ConfigureAwait(false);
  205. while (await e.MoveNextAsync())
  206. {
  207. var x = await selector(e.Current).ConfigureAwait(false);
  208. if (comparer.Compare(x, value) > 0)
  209. {
  210. value = x;
  211. }
  212. }
  213. }
  214. return value;
  215. }
  216. }
  217. }
  218. #if !NO_DEEP_CANCELLATION
  219. internal static ValueTask<TResult> MaxAwaitWithCancellationAsyncCore<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TResult>> selector, CancellationToken cancellationToken = default)
  220. {
  221. if (source == null)
  222. throw Error.ArgumentNull(nameof(source));
  223. if (selector == null)
  224. throw Error.ArgumentNull(nameof(selector));
  225. if (default(TResult)! == null) // NB: Null value is desired; JIT-time check.
  226. {
  227. return Core(source, selector, cancellationToken);
  228. static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TResult>> selector, CancellationToken cancellationToken)
  229. {
  230. var comparer = Comparer<TResult>.Default;
  231. TResult value;
  232. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  233. {
  234. do
  235. {
  236. if (!await e.MoveNextAsync())
  237. {
  238. return default!;
  239. }
  240. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  241. }
  242. while (value == null);
  243. while (await e.MoveNextAsync())
  244. {
  245. var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  246. if (x != null && comparer.Compare(x, value) > 0)
  247. {
  248. value = x;
  249. }
  250. }
  251. }
  252. return value;
  253. }
  254. }
  255. else
  256. {
  257. return Core(source, selector, cancellationToken);
  258. static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TResult>> selector, CancellationToken cancellationToken)
  259. {
  260. var comparer = Comparer<TResult>.Default;
  261. TResult value;
  262. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  263. {
  264. if (!await e.MoveNextAsync())
  265. {
  266. throw Error.NoElements();
  267. }
  268. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  269. while (await e.MoveNextAsync())
  270. {
  271. var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  272. if (comparer.Compare(x, value) > 0)
  273. {
  274. value = x;
  275. }
  276. }
  277. }
  278. return value;
  279. }
  280. }
  281. }
  282. #endif
  283. }
  284. }