MinBy.cs 7.6 KB

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