MaxBy.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. /// <summary>
  12. /// Returns the elements in an async-enumerable sequence with the maximum key value.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <typeparam name="TKey">The type of the key computed for each element in the source sequence.</typeparam>
  16. /// <param name="source">An async-enumerable sequence to get the maximum elements for.</param>
  17. /// <param name="keySelector">Key selector function.</param>
  18. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  19. /// <returns>A ValueTask containing a list of zero or more elements that have a maximum key value.</returns>
  20. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  21. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  22. public static ValueTask<IList<TSource>> MaxByAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken = default)
  23. {
  24. if (source == null)
  25. throw Error.ArgumentNull(nameof(source));
  26. if (keySelector == null)
  27. throw Error.ArgumentNull(nameof(keySelector));
  28. return MaxByCore(source, keySelector, comparer: null, cancellationToken);
  29. }
  30. /// <summary>
  31. /// Returns the elements in an async-enumerable sequence with the maximum key value according to the specified comparer.
  32. /// </summary>
  33. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  34. /// <typeparam name="TKey">The type of the key computed for each element in the source sequence.</typeparam>
  35. /// <param name="source">An async-enumerable sequence to get the maximum elements for.</param>
  36. /// <param name="keySelector">Key selector function.</param>
  37. /// <param name="comparer">Comparer used to compare key values.</param>
  38. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  39. /// <returns>A ValueTask containing a list of zero or more elements that have a maximum key value.</returns>
  40. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="comparer"/> is null.</exception>
  41. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  42. public static ValueTask<IList<TSource>> MaxByAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey>? comparer, CancellationToken cancellationToken = default)
  43. {
  44. if (source == null)
  45. throw Error.ArgumentNull(nameof(source));
  46. if (keySelector == null)
  47. throw Error.ArgumentNull(nameof(keySelector));
  48. return MaxByCore(source, keySelector, comparer, cancellationToken);
  49. }
  50. /// <summary>
  51. /// Returns the elements in an async-enumerable sequence with the maximum key value.
  52. /// </summary>
  53. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  54. /// <typeparam name="TKey">The type of the key computed for each element in the source sequence.</typeparam>
  55. /// <param name="source">An async-enumerable sequence to get the maximum elements for.</param>
  56. /// <param name="keySelector">Key selector function returning a key possibly asynchronously.</param>
  57. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  58. /// <returns>A ValueTask containing a list of zero or more elements that have a maximum key value.</returns>
  59. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  60. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  61. public static ValueTask<IList<TSource>> MaxByAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, CancellationToken cancellationToken = default)
  62. {
  63. if (source == null)
  64. throw Error.ArgumentNull(nameof(source));
  65. if (keySelector == null)
  66. throw Error.ArgumentNull(nameof(keySelector));
  67. return MaxByCore<TSource, TKey>(source, keySelector, comparer: null, cancellationToken);
  68. }
  69. #if !NO_DEEP_CANCELLATION
  70. /// <summary>
  71. /// Returns the elements in an async-enumerable sequence with the maximum key value.
  72. /// </summary>
  73. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  74. /// <typeparam name="TKey">The type of the key computed for each element in the source sequence.</typeparam>
  75. /// <param name="source">An async-enumerable sequence to get the maximum elements for.</param>
  76. /// <param name="keySelector">Key selector function returning a key possibly asynchronously and supporting cancellation.</param>
  77. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  78. /// <returns>A ValueTask containing a list of zero or more elements that have a maximum key value.</returns>
  79. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  80. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  81. public static ValueTask<IList<TSource>> MaxByAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, CancellationToken cancellationToken = default)
  82. {
  83. if (source == null)
  84. throw Error.ArgumentNull(nameof(source));
  85. if (keySelector == null)
  86. throw Error.ArgumentNull(nameof(keySelector));
  87. return MaxByCore<TSource, TKey>(source, keySelector, comparer: null, cancellationToken);
  88. }
  89. #endif
  90. /// <summary>
  91. /// Returns the elements in an async-enumerable sequence with the maximum key value according to the specified comparer.
  92. /// </summary>
  93. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  94. /// <typeparam name="TKey">The type of the key computed for each element in the source sequence.</typeparam>
  95. /// <param name="source">An async-enumerable sequence to get the maximum elements for.</param>
  96. /// <param name="keySelector">Key selector function returning a key possibly asynchronously.</param>
  97. /// <param name="comparer">Comparer used to compare key values.</param>
  98. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  99. /// <returns>A ValueTask containing a list of zero or more elements that have a maximum key value.</returns>
  100. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="comparer"/> is null.</exception>
  101. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  102. public static ValueTask<IList<TSource>> MaxByAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey>? comparer, CancellationToken cancellationToken = default)
  103. {
  104. if (source == null)
  105. throw Error.ArgumentNull(nameof(source));
  106. if (keySelector == null)
  107. throw Error.ArgumentNull(nameof(keySelector));
  108. return MaxByCore(source, keySelector, comparer, cancellationToken);
  109. }
  110. #if !NO_DEEP_CANCELLATION
  111. /// <summary>
  112. /// Returns the elements in an async-enumerable sequence with the maximum key value according to the specified comparer.
  113. /// </summary>
  114. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  115. /// <typeparam name="TKey">The type of the key computed for each element in the source sequence.</typeparam>
  116. /// <param name="source">An async-enumerable sequence to get the maximum elements for.</param>
  117. /// <param name="keySelector">Key selector function returning a key possibly asynchronously and supporting cancellation.</param>
  118. /// <param name="comparer">Comparer used to compare key values.</param>
  119. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  120. /// <returns>A ValueTask containing a list of zero or more elements that have a maximum key value.</returns>
  121. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="comparer"/> is null.</exception>
  122. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  123. public static ValueTask<IList<TSource>> MaxByAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey>? comparer, CancellationToken cancellationToken = default)
  124. {
  125. if (source == null)
  126. throw Error.ArgumentNull(nameof(source));
  127. if (keySelector == null)
  128. throw Error.ArgumentNull(nameof(keySelector));
  129. return MaxByCore(source, keySelector, comparer, cancellationToken);
  130. }
  131. #endif
  132. private static ValueTask<IList<TSource>> MaxByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey>? comparer, CancellationToken cancellationToken)
  133. {
  134. comparer ??= Comparer<TKey>.Default;
  135. return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue), cancellationToken);
  136. }
  137. private static ValueTask<IList<TSource>> MaxByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey>? comparer, CancellationToken cancellationToken)
  138. {
  139. comparer ??= Comparer<TKey>.Default;
  140. return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue), cancellationToken);
  141. }
  142. #if !NO_DEEP_CANCELLATION
  143. private static ValueTask<IList<TSource>> MaxByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey>? comparer, CancellationToken cancellationToken)
  144. {
  145. comparer ??= Comparer<TKey>.Default;
  146. return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue), cancellationToken);
  147. }
  148. #endif
  149. }
  150. }