MinBy.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 ValueTask<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 ValueTask<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 ValueTask<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 ValueTask<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 ValueTask<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 ValueTask<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 ValueTask<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 ValueTask<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 ValueTask<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 ValueTask<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. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  93. {
  94. if (!await e.MoveNextAsync())
  95. throw Error.NoElements();
  96. var current = e.Current;
  97. var resKey = keySelector(current);
  98. result.Add(current);
  99. while (await e.MoveNextAsync())
  100. {
  101. var cur = e.Current;
  102. var key = keySelector(cur);
  103. var cmp = compare(key, resKey);
  104. if (cmp == 0)
  105. {
  106. result.Add(cur);
  107. }
  108. else if (cmp > 0)
  109. {
  110. result = new List<TSource> { cur };
  111. resKey = key;
  112. }
  113. }
  114. }
  115. return result;
  116. }
  117. private static async ValueTask<IList<TSource>> ExtremaBy<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TKey, TKey, int> compare, CancellationToken cancellationToken)
  118. {
  119. var result = new List<TSource>();
  120. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  121. {
  122. if (!await e.MoveNextAsync())
  123. throw Error.NoElements();
  124. var current = e.Current;
  125. var resKey = await keySelector(current).ConfigureAwait(false);
  126. result.Add(current);
  127. while (await e.MoveNextAsync())
  128. {
  129. var cur = e.Current;
  130. var key = await keySelector(cur).ConfigureAwait(false);
  131. var cmp = compare(key, resKey);
  132. if (cmp == 0)
  133. {
  134. result.Add(cur);
  135. }
  136. else if (cmp > 0)
  137. {
  138. result = new List<TSource> { cur };
  139. resKey = key;
  140. }
  141. }
  142. }
  143. return result;
  144. }
  145. #if !NO_DEEP_CANCELLATION
  146. private static async ValueTask<IList<TSource>> ExtremaBy<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TKey, TKey, int> compare, CancellationToken cancellationToken)
  147. {
  148. var result = new List<TSource>();
  149. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  150. {
  151. if (!await e.MoveNextAsync())
  152. throw Error.NoElements();
  153. var current = e.Current;
  154. var resKey = await keySelector(current, cancellationToken).ConfigureAwait(false);
  155. result.Add(current);
  156. while (await e.MoveNextAsync())
  157. {
  158. var cur = e.Current;
  159. var key = await keySelector(cur, cancellationToken).ConfigureAwait(false);
  160. var cmp = compare(key, resKey);
  161. if (cmp == 0)
  162. {
  163. result.Add(cur);
  164. }
  165. else if (cmp > 0)
  166. {
  167. result = new List<TSource> { cur };
  168. resKey = key;
  169. }
  170. }
  171. }
  172. return result;
  173. }
  174. #endif
  175. }
  176. }