MinBy.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 AsyncEnumerableEx
  10. {
  11. public static Task<IList<TSource>> MinByAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken = default)
  12. {
  13. if (source == null)
  14. throw Error.ArgumentNull(nameof(source));
  15. if (keySelector == null)
  16. throw Error.ArgumentNull(nameof(keySelector));
  17. return MinByCore(source, keySelector, comparer: null, cancellationToken);
  18. }
  19. public static Task<IList<TSource>> MinByAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken = default)
  20. {
  21. if (source == null)
  22. throw Error.ArgumentNull(nameof(source));
  23. if (keySelector == null)
  24. throw Error.ArgumentNull(nameof(keySelector));
  25. return MinByCore(source, keySelector, comparer, cancellationToken);
  26. }
  27. public static Task<IList<TSource>> MinByAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, CancellationToken cancellationToken = default)
  28. {
  29. if (source == null)
  30. throw Error.ArgumentNull(nameof(source));
  31. if (keySelector == null)
  32. throw Error.ArgumentNull(nameof(keySelector));
  33. return MinByCore<TSource, TKey>(source, keySelector, comparer: null, cancellationToken);
  34. }
  35. #if !NO_DEEP_CANCELLATION
  36. public static Task<IList<TSource>> MinByAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, CancellationToken cancellationToken = default)
  37. {
  38. if (source == null)
  39. throw Error.ArgumentNull(nameof(source));
  40. if (keySelector == null)
  41. throw Error.ArgumentNull(nameof(keySelector));
  42. return MinByCore<TSource, TKey>(source, keySelector, comparer: null, cancellationToken);
  43. }
  44. #endif
  45. public static Task<IList<TSource>> MinByAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken = default)
  46. {
  47. if (source == null)
  48. throw Error.ArgumentNull(nameof(source));
  49. if (keySelector == null)
  50. throw Error.ArgumentNull(nameof(keySelector));
  51. return MinByCore(source, keySelector, comparer, cancellationToken);
  52. }
  53. #if !NO_DEEP_CANCELLATION
  54. public static Task<IList<TSource>> MinByAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken = default)
  55. {
  56. if (source == null)
  57. throw Error.ArgumentNull(nameof(source));
  58. if (keySelector == null)
  59. throw Error.ArgumentNull(nameof(keySelector));
  60. return MinByCore(source, keySelector, comparer, cancellationToken);
  61. }
  62. #endif
  63. private static Task<IList<TSource>> MinByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
  64. {
  65. if (comparer == null)
  66. {
  67. comparer = Comparer<TKey>.Default;
  68. }
  69. return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
  70. }
  71. private static Task<IList<TSource>> MinByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
  72. {
  73. if (comparer == null)
  74. {
  75. comparer = Comparer<TKey>.Default;
  76. }
  77. return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
  78. }
  79. #if !NO_DEEP_CANCELLATION
  80. private static Task<IList<TSource>> MinByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
  81. {
  82. if (comparer == null)
  83. {
  84. comparer = Comparer<TKey>.Default;
  85. }
  86. return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
  87. }
  88. #endif
  89. private static async Task<IList<TSource>> ExtremaBy<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare, CancellationToken cancellationToken)
  90. {
  91. var result = new List<TSource>();
  92. #if USE_AWAIT_USING
  93. await using (var e = source.GetAsyncEnumerator(cancellationToken).ConfigureAwait(false))
  94. {
  95. if (!await e.MoveNextAsync())
  96. throw Error.NoElements();
  97. var current = e.Current;
  98. var resKey = keySelector(current);
  99. result.Add(current);
  100. while (await e.MoveNextAsync())
  101. {
  102. var cur = e.Current;
  103. var key = keySelector(cur);
  104. var cmp = compare(key, resKey);
  105. if (cmp == 0)
  106. {
  107. result.Add(cur);
  108. }
  109. else if (cmp > 0)
  110. {
  111. result = new List<TSource> { cur };
  112. resKey = key;
  113. }
  114. }
  115. }
  116. #else
  117. var e = source.GetAsyncEnumerator(cancellationToken);
  118. try
  119. {
  120. if (!await e.MoveNextAsync().ConfigureAwait(false))
  121. throw Error.NoElements();
  122. var current = e.Current;
  123. var resKey = keySelector(current);
  124. result.Add(current);
  125. while (await e.MoveNextAsync().ConfigureAwait(false))
  126. {
  127. var cur = e.Current;
  128. var key = keySelector(cur);
  129. var cmp = compare(key, resKey);
  130. if (cmp == 0)
  131. {
  132. result.Add(cur);
  133. }
  134. else if (cmp > 0)
  135. {
  136. result = new List<TSource> { cur };
  137. resKey = key;
  138. }
  139. }
  140. }
  141. finally
  142. {
  143. await e.DisposeAsync().ConfigureAwait(false);
  144. }
  145. #endif
  146. return result;
  147. }
  148. private static async Task<IList<TSource>> ExtremaBy<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TKey, TKey, int> compare, CancellationToken cancellationToken)
  149. {
  150. var result = new List<TSource>();
  151. #if USE_AWAIT_USING
  152. await using (var e = source.GetAsyncEnumerator(cancellationToken).ConfigureAwait(false))
  153. {
  154. if (!await e.MoveNextAsync())
  155. throw Error.NoElements();
  156. var current = e.Current;
  157. var resKey = await keySelector(current).ConfigureAwait(false);
  158. result.Add(current);
  159. while (await e.MoveNextAsync())
  160. {
  161. var cur = e.Current;
  162. var key = await keySelector(cur).ConfigureAwait(false);
  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. #else
  176. var e = source.GetAsyncEnumerator(cancellationToken);
  177. try
  178. {
  179. if (!await e.MoveNextAsync().ConfigureAwait(false))
  180. throw Error.NoElements();
  181. var current = e.Current;
  182. var resKey = await keySelector(current).ConfigureAwait(false);
  183. result.Add(current);
  184. while (await e.MoveNextAsync().ConfigureAwait(false))
  185. {
  186. var cur = e.Current;
  187. var key = await keySelector(cur).ConfigureAwait(false);
  188. var cmp = compare(key, resKey);
  189. if (cmp == 0)
  190. {
  191. result.Add(cur);
  192. }
  193. else if (cmp > 0)
  194. {
  195. result = new List<TSource> { cur };
  196. resKey = key;
  197. }
  198. }
  199. }
  200. finally
  201. {
  202. await e.DisposeAsync().ConfigureAwait(false);
  203. }
  204. #endif
  205. return result;
  206. }
  207. #if !NO_DEEP_CANCELLATION
  208. private static async Task<IList<TSource>> ExtremaBy<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TKey, TKey, int> compare, CancellationToken cancellationToken)
  209. {
  210. var result = new List<TSource>();
  211. #if USE_AWAIT_USING
  212. await using (var e = source.GetAsyncEnumerator(cancellationToken).ConfigureAwait(false))
  213. {
  214. if (!await e.MoveNextAsync())
  215. throw Error.NoElements();
  216. var current = e.Current;
  217. var resKey = await keySelector(current, cancellationToken).ConfigureAwait(false);
  218. result.Add(current);
  219. while (await e.MoveNextAsync())
  220. {
  221. var cur = e.Current;
  222. var key = await keySelector(cur, cancellationToken).ConfigureAwait(false);
  223. var cmp = compare(key, resKey);
  224. if (cmp == 0)
  225. {
  226. result.Add(cur);
  227. }
  228. else if (cmp > 0)
  229. {
  230. result = new List<TSource> { cur };
  231. resKey = key;
  232. }
  233. }
  234. }
  235. #else
  236. var e = source.GetAsyncEnumerator(cancellationToken);
  237. try
  238. {
  239. if (!await e.MoveNextAsync().ConfigureAwait(false))
  240. throw Error.NoElements();
  241. var current = e.Current;
  242. var resKey = await keySelector(current, cancellationToken).ConfigureAwait(false);
  243. result.Add(current);
  244. while (await e.MoveNextAsync().ConfigureAwait(false))
  245. {
  246. var cur = e.Current;
  247. var key = await keySelector(cur, cancellationToken).ConfigureAwait(false);
  248. var cmp = compare(key, resKey);
  249. if (cmp == 0)
  250. {
  251. result.Add(cur);
  252. }
  253. else if (cmp > 0)
  254. {
  255. result = new List<TSource> { cur };
  256. resKey = key;
  257. }
  258. }
  259. }
  260. finally
  261. {
  262. await e.DisposeAsync().ConfigureAwait(false);
  263. }
  264. #endif
  265. return result;
  266. }
  267. #endif
  268. }
  269. }