MinBy.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. comparer ??= Comparer<TKey>.Default;
  66. return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
  67. }
  68. private static ValueTask<IList<TSource>> MinByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey>? comparer, CancellationToken cancellationToken)
  69. {
  70. comparer ??= Comparer<TKey>.Default;
  71. return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
  72. }
  73. #if !NO_DEEP_CANCELLATION
  74. private static ValueTask<IList<TSource>> MinByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey>? comparer, CancellationToken cancellationToken)
  75. {
  76. comparer ??= Comparer<TKey>.Default;
  77. return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
  78. }
  79. #endif
  80. private static async ValueTask<IList<TSource>> ExtremaBy<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare, CancellationToken cancellationToken)
  81. {
  82. var result = new List<TSource>();
  83. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  84. {
  85. if (!await e.MoveNextAsync())
  86. throw Error.NoElements();
  87. var current = e.Current;
  88. var resKey = keySelector(current);
  89. result.Add(current);
  90. while (await e.MoveNextAsync())
  91. {
  92. var cur = e.Current;
  93. var key = keySelector(cur);
  94. var cmp = compare(key, resKey);
  95. if (cmp == 0)
  96. {
  97. result.Add(cur);
  98. }
  99. else if (cmp > 0)
  100. {
  101. result = new List<TSource> { cur };
  102. resKey = key;
  103. }
  104. }
  105. }
  106. return result;
  107. }
  108. private static async ValueTask<IList<TSource>> ExtremaBy<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TKey, TKey, int> compare, CancellationToken cancellationToken)
  109. {
  110. var result = new List<TSource>();
  111. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  112. {
  113. if (!await e.MoveNextAsync())
  114. throw Error.NoElements();
  115. var current = e.Current;
  116. var resKey = await keySelector(current).ConfigureAwait(false);
  117. result.Add(current);
  118. while (await e.MoveNextAsync())
  119. {
  120. var cur = e.Current;
  121. var key = await keySelector(cur).ConfigureAwait(false);
  122. var cmp = compare(key, resKey);
  123. if (cmp == 0)
  124. {
  125. result.Add(cur);
  126. }
  127. else if (cmp > 0)
  128. {
  129. result = new List<TSource> { cur };
  130. resKey = key;
  131. }
  132. }
  133. }
  134. return result;
  135. }
  136. #if !NO_DEEP_CANCELLATION
  137. 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)
  138. {
  139. var result = new List<TSource>();
  140. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  141. {
  142. if (!await e.MoveNextAsync())
  143. throw Error.NoElements();
  144. var current = e.Current;
  145. var resKey = await keySelector(current, cancellationToken).ConfigureAwait(false);
  146. result.Add(current);
  147. while (await e.MoveNextAsync())
  148. {
  149. var cur = e.Current;
  150. var key = await keySelector(cur, cancellationToken).ConfigureAwait(false);
  151. var cmp = compare(key, resKey);
  152. if (cmp == 0)
  153. {
  154. result.Add(cur);
  155. }
  156. else if (cmp > 0)
  157. {
  158. result = new List<TSource> { cur };
  159. resKey = key;
  160. }
  161. }
  162. }
  163. return result;
  164. }
  165. #endif
  166. }
  167. }