Min.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 Task<TSource> Min<TSource>(this IAsyncEnumerable<TSource> source)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. return Min(source, CancellationToken.None);
  16. }
  17. public static Task<TSource> Min<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  18. {
  19. if (source == null)
  20. throw new ArgumentNullException(nameof(source));
  21. var comparer = Comparer<TSource>.Default;
  22. return source.Aggregate((x, y) => comparer.Compare(x, y) <= 0 ? x : y, cancellationToken);
  23. }
  24. public static Task<TSource> Min<TSource>(this IAsyncEnumerable<TSource> source, IComparer<TSource> comparer)
  25. {
  26. if (source == null)
  27. throw new ArgumentNullException(nameof(source));
  28. if (comparer == null)
  29. throw new ArgumentNullException(nameof(comparer));
  30. return source.Min(comparer, CancellationToken.None);
  31. }
  32. public static Task<TSource> Min<TSource>(this IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
  33. {
  34. if (source == null)
  35. throw new ArgumentNullException(nameof(source));
  36. if (comparer == null)
  37. throw new ArgumentNullException(nameof(comparer));
  38. return Min_(source, comparer, cancellationToken);
  39. }
  40. public static Task<TResult> Min<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector)
  41. {
  42. if (source == null)
  43. throw new ArgumentNullException(nameof(source));
  44. if (selector == null)
  45. throw new ArgumentNullException(nameof(selector));
  46. return Min(source, selector, CancellationToken.None);
  47. }
  48. public static Task<TResult> Min<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
  49. {
  50. if (source == null)
  51. throw new ArgumentNullException(nameof(source));
  52. if (selector == null)
  53. throw new ArgumentNullException(nameof(selector));
  54. return source.Select(selector).Min(cancellationToken);
  55. }
  56. public static Task<TResult> Min<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TResult>> selector)
  57. {
  58. if (source == null)
  59. throw new ArgumentNullException(nameof(source));
  60. if (selector == null)
  61. throw new ArgumentNullException(nameof(selector));
  62. return Min(source, selector, CancellationToken.None);
  63. }
  64. public static Task<TResult> Min<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TResult>> selector, CancellationToken cancellationToken)
  65. {
  66. if (source == null)
  67. throw new ArgumentNullException(nameof(source));
  68. if (selector == null)
  69. throw new ArgumentNullException(nameof(selector));
  70. return source.Select(selector).Min(cancellationToken);
  71. }
  72. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  73. {
  74. if (source == null)
  75. throw new ArgumentNullException(nameof(source));
  76. if (keySelector == null)
  77. throw new ArgumentNullException(nameof(keySelector));
  78. return source.MinBy(keySelector, CancellationToken.None);
  79. }
  80. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  81. {
  82. if (source == null)
  83. throw new ArgumentNullException(nameof(source));
  84. if (keySelector == null)
  85. throw new ArgumentNullException(nameof(keySelector));
  86. if (comparer == null)
  87. throw new ArgumentNullException(nameof(comparer));
  88. return source.MinBy(keySelector, comparer, CancellationToken.None);
  89. }
  90. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
  91. {
  92. if (source == null)
  93. throw new ArgumentNullException(nameof(source));
  94. if (keySelector == null)
  95. throw new ArgumentNullException(nameof(keySelector));
  96. return MinBy(source, keySelector, Comparer<TKey>.Default, cancellationToken);
  97. }
  98. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
  99. {
  100. if (source == null)
  101. throw new ArgumentNullException(nameof(source));
  102. if (keySelector == null)
  103. throw new ArgumentNullException(nameof(keySelector));
  104. if (comparer == null)
  105. throw new ArgumentNullException(nameof(comparer));
  106. return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
  107. }
  108. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector)
  109. {
  110. if (source == null)
  111. throw new ArgumentNullException(nameof(source));
  112. if (keySelector == null)
  113. throw new ArgumentNullException(nameof(keySelector));
  114. return source.MinBy(keySelector, CancellationToken.None);
  115. }
  116. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IComparer<TKey> comparer)
  117. {
  118. if (source == null)
  119. throw new ArgumentNullException(nameof(source));
  120. if (keySelector == null)
  121. throw new ArgumentNullException(nameof(keySelector));
  122. if (comparer == null)
  123. throw new ArgumentNullException(nameof(comparer));
  124. return source.MinBy(keySelector, comparer, CancellationToken.None);
  125. }
  126. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, CancellationToken cancellationToken)
  127. {
  128. if (source == null)
  129. throw new ArgumentNullException(nameof(source));
  130. if (keySelector == null)
  131. throw new ArgumentNullException(nameof(keySelector));
  132. return MinBy(source, keySelector, Comparer<TKey>.Default, cancellationToken);
  133. }
  134. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
  135. {
  136. if (source == null)
  137. throw new ArgumentNullException(nameof(source));
  138. if (keySelector == null)
  139. throw new ArgumentNullException(nameof(keySelector));
  140. if (comparer == null)
  141. throw new ArgumentNullException(nameof(comparer));
  142. return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
  143. }
  144. private static async Task<TSource> Min_<TSource>(IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
  145. {
  146. return (await MinBy(source, x => x, comparer, cancellationToken).ConfigureAwait(false)).First();
  147. }
  148. private static async Task<IList<TSource>> ExtremaBy<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare, CancellationToken cancellationToken)
  149. {
  150. var result = new List<TSource>();
  151. var e = source.GetAsyncEnumerator();
  152. try
  153. {
  154. if (!await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  155. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  156. var current = e.Current;
  157. var resKey = keySelector(current);
  158. result.Add(current);
  159. while (await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  160. {
  161. var cur = e.Current;
  162. var key = keySelector(cur);
  163. var cmp = compare(key, resKey);
  164. if (cmp == 0)
  165. {
  166. result.Add(cur);
  167. }
  168. else if (cmp > 0)
  169. {
  170. result = new List<TSource> { cur };
  171. resKey = key;
  172. }
  173. }
  174. }
  175. finally
  176. {
  177. await e.DisposeAsync().ConfigureAwait(false);
  178. }
  179. return result;
  180. }
  181. private static async Task<IList<TSource>> ExtremaBy<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, Func<TKey, TKey, int> compare, CancellationToken cancellationToken)
  182. {
  183. var result = new List<TSource>();
  184. var e = source.GetAsyncEnumerator();
  185. try
  186. {
  187. if (!await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  188. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  189. var current = e.Current;
  190. var resKey = await keySelector(current).ConfigureAwait(false);
  191. result.Add(current);
  192. while (await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  193. {
  194. var cur = e.Current;
  195. var key = await keySelector(cur).ConfigureAwait(false);
  196. var cmp = compare(key, resKey);
  197. if (cmp == 0)
  198. {
  199. result.Add(cur);
  200. }
  201. else if (cmp > 0)
  202. {
  203. result = new List<TSource> { cur };
  204. resKey = key;
  205. }
  206. }
  207. }
  208. finally
  209. {
  210. await e.DisposeAsync().ConfigureAwait(false);
  211. }
  212. return result;
  213. }
  214. }
  215. }