Min.cs 14 KB

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