MinBy.cs 11 KB

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