MinBy.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 AsyncEnumerable
  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 new ArgumentNullException(nameof(source));
  15. if (keySelector == null)
  16. throw new ArgumentNullException(nameof(keySelector));
  17. return source.MinBy(keySelector, 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 new ArgumentNullException(nameof(source));
  23. if (keySelector == null)
  24. throw new ArgumentNullException(nameof(keySelector));
  25. return MinBy(source, keySelector, Comparer<TKey>.Default, 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 new ArgumentNullException(nameof(source));
  31. if (keySelector == null)
  32. throw new ArgumentNullException(nameof(keySelector));
  33. if (comparer == null)
  34. throw new ArgumentNullException(nameof(comparer));
  35. return source.MinBy(keySelector, comparer, CancellationToken.None);
  36. }
  37. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
  38. {
  39. if (source == null)
  40. throw new ArgumentNullException(nameof(source));
  41. if (keySelector == null)
  42. throw new ArgumentNullException(nameof(keySelector));
  43. if (comparer == null)
  44. throw new ArgumentNullException(nameof(comparer));
  45. return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
  46. }
  47. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector)
  48. {
  49. if (source == null)
  50. throw new ArgumentNullException(nameof(source));
  51. if (keySelector == null)
  52. throw new ArgumentNullException(nameof(keySelector));
  53. return source.MinBy(keySelector, CancellationToken.None);
  54. }
  55. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, CancellationToken cancellationToken)
  56. {
  57. if (source == null)
  58. throw new ArgumentNullException(nameof(source));
  59. if (keySelector == null)
  60. throw new ArgumentNullException(nameof(keySelector));
  61. return MinBy(source, keySelector, Comparer<TKey>.Default, cancellationToken);
  62. }
  63. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IComparer<TKey> comparer)
  64. {
  65. if (source == null)
  66. throw new ArgumentNullException(nameof(source));
  67. if (keySelector == null)
  68. throw new ArgumentNullException(nameof(keySelector));
  69. if (comparer == null)
  70. throw new ArgumentNullException(nameof(comparer));
  71. return source.MinBy(keySelector, comparer, CancellationToken.None);
  72. }
  73. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
  74. {
  75. if (source == null)
  76. throw new ArgumentNullException(nameof(source));
  77. if (keySelector == null)
  78. throw new ArgumentNullException(nameof(keySelector));
  79. if (comparer == null)
  80. throw new ArgumentNullException(nameof(comparer));
  81. return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
  82. }
  83. private static async Task<TSource> Min_<TSource>(IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
  84. {
  85. return (await MinBy(source, x => x, comparer, cancellationToken).ConfigureAwait(false)).First();
  86. }
  87. private static async Task<IList<TSource>> ExtremaBy<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare, CancellationToken cancellationToken)
  88. {
  89. var result = new List<TSource>();
  90. var e = source.GetAsyncEnumerator();
  91. try
  92. {
  93. if (!await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  94. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  95. var current = e.Current;
  96. var resKey = keySelector(current);
  97. result.Add(current);
  98. while (await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  99. {
  100. var cur = e.Current;
  101. var key = keySelector(cur);
  102. var cmp = compare(key, resKey);
  103. if (cmp == 0)
  104. {
  105. result.Add(cur);
  106. }
  107. else if (cmp > 0)
  108. {
  109. result = new List<TSource> { cur };
  110. resKey = key;
  111. }
  112. }
  113. }
  114. finally
  115. {
  116. await e.DisposeAsync().ConfigureAwait(false);
  117. }
  118. return result;
  119. }
  120. private static async Task<IList<TSource>> ExtremaBy<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, Func<TKey, TKey, int> compare, CancellationToken cancellationToken)
  121. {
  122. var result = new List<TSource>();
  123. var e = source.GetAsyncEnumerator();
  124. try
  125. {
  126. if (!await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  127. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  128. var current = e.Current;
  129. var resKey = await keySelector(current).ConfigureAwait(false);
  130. result.Add(current);
  131. while (await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  132. {
  133. var cur = e.Current;
  134. var key = await keySelector(cur).ConfigureAwait(false);
  135. var cmp = compare(key, resKey);
  136. if (cmp == 0)
  137. {
  138. result.Add(cur);
  139. }
  140. else if (cmp > 0)
  141. {
  142. result = new List<TSource> { cur };
  143. resKey = key;
  144. }
  145. }
  146. }
  147. finally
  148. {
  149. await e.DisposeAsync().ConfigureAwait(false);
  150. }
  151. return result;
  152. }
  153. }
  154. }