MaxBy.cs 12 KB

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