MinBy.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  93. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  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. finally
  117. {
  118. await e.DisposeAsync();
  119. }
  120. return result;
  121. }
  122. private static async ValueTask<IList<TSource>> ExtremaBy<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<TKey, TKey, int> compare, CancellationToken cancellationToken)
  123. {
  124. var result = new List<TSource>();
  125. var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  126. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  127. {
  128. if (!await e.MoveNextAsync())
  129. throw Error.NoElements();
  130. var current = e.Current;
  131. var resKey = await keySelector(current).ConfigureAwait(false);
  132. result.Add(current);
  133. while (await e.MoveNextAsync())
  134. {
  135. var cur = e.Current;
  136. var key = await keySelector(cur).ConfigureAwait(false);
  137. var cmp = compare(key, resKey);
  138. if (cmp == 0)
  139. {
  140. result.Add(cur);
  141. }
  142. else if (cmp > 0)
  143. {
  144. result = new List<TSource> { cur };
  145. resKey = key;
  146. }
  147. }
  148. }
  149. finally
  150. {
  151. await e.DisposeAsync();
  152. }
  153. return result;
  154. }
  155. #if !NO_DEEP_CANCELLATION
  156. 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)
  157. {
  158. var result = new List<TSource>();
  159. var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  160. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  161. {
  162. if (!await e.MoveNextAsync())
  163. throw Error.NoElements();
  164. var current = e.Current;
  165. var resKey = await keySelector(current, cancellationToken).ConfigureAwait(false);
  166. result.Add(current);
  167. while (await e.MoveNextAsync())
  168. {
  169. var cur = e.Current;
  170. var key = await keySelector(cur, cancellationToken).ConfigureAwait(false);
  171. var cmp = compare(key, resKey);
  172. if (cmp == 0)
  173. {
  174. result.Add(cur);
  175. }
  176. else if (cmp > 0)
  177. {
  178. result = new List<TSource> { cur };
  179. resKey = key;
  180. }
  181. }
  182. }
  183. finally
  184. {
  185. await e.DisposeAsync();
  186. }
  187. return result;
  188. }
  189. #endif
  190. }
  191. }