Max.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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<TSource> Max<TSource>(this IAsyncEnumerable<TSource> source)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. return Max(source, CancellationToken.None);
  16. }
  17. public static Task<TSource> Max<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  18. {
  19. if (source == null)
  20. throw new ArgumentNullException(nameof(source));
  21. var comparer = Comparer<TSource>.Default;
  22. return source.Aggregate((x, y) => comparer.Compare(x, y) >= 0 ? x : y, cancellationToken);
  23. }
  24. public static Task<TSource> Max<TSource>(this IAsyncEnumerable<TSource> source, IComparer<TSource> comparer)
  25. {
  26. if (source == null)
  27. throw new ArgumentNullException(nameof(source));
  28. if (comparer == null)
  29. throw new ArgumentNullException(nameof(comparer));
  30. return source.Max(comparer, CancellationToken.None);
  31. }
  32. public static Task<TSource> Max<TSource>(this IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
  33. {
  34. if (source == null)
  35. throw new ArgumentNullException(nameof(source));
  36. if (comparer == null)
  37. throw new ArgumentNullException(nameof(comparer));
  38. return Max_(source, comparer, cancellationToken);
  39. }
  40. public static Task<TResult> Max<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector)
  41. {
  42. if (source == null)
  43. throw new ArgumentNullException(nameof(source));
  44. if (selector == null)
  45. throw new ArgumentNullException(nameof(selector));
  46. return Max(source, selector, CancellationToken.None);
  47. }
  48. public static Task<TResult> Max<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
  49. {
  50. if (source == null)
  51. throw new ArgumentNullException(nameof(source));
  52. if (selector == null)
  53. throw new ArgumentNullException(nameof(selector));
  54. return source.Select(selector)
  55. .Max(cancellationToken);
  56. }
  57. public static Task<TResult> Max<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TResult>> selector)
  58. {
  59. if (source == null)
  60. throw new ArgumentNullException(nameof(source));
  61. if (selector == null)
  62. throw new ArgumentNullException(nameof(selector));
  63. return Max(source, selector, CancellationToken.None);
  64. }
  65. public static Task<TResult> Max<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TResult>> selector, CancellationToken cancellationToken)
  66. {
  67. if (source == null)
  68. throw new ArgumentNullException(nameof(source));
  69. if (selector == null)
  70. throw new ArgumentNullException(nameof(selector));
  71. return source.Select(selector)
  72. .Max(cancellationToken);
  73. }
  74. public static Task<IList<TSource>> MaxBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  75. {
  76. if (source == null)
  77. throw new ArgumentNullException(nameof(source));
  78. if (keySelector == null)
  79. throw new ArgumentNullException(nameof(keySelector));
  80. return source.MaxBy(keySelector, CancellationToken.None);
  81. }
  82. public static Task<IList<TSource>> MaxBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  83. {
  84. if (source == null)
  85. throw new ArgumentNullException(nameof(source));
  86. if (keySelector == null)
  87. throw new ArgumentNullException(nameof(keySelector));
  88. if (comparer == null)
  89. throw new ArgumentNullException(nameof(comparer));
  90. return source.MaxBy(keySelector, comparer, CancellationToken.None);
  91. }
  92. public static Task<IList<TSource>> MaxBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
  93. {
  94. if (source == null)
  95. throw new ArgumentNullException(nameof(source));
  96. if (keySelector == null)
  97. throw new ArgumentNullException(nameof(keySelector));
  98. return MaxBy(source, keySelector, Comparer<TKey>.Default, cancellationToken);
  99. }
  100. public static Task<IList<TSource>> MaxBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
  101. {
  102. if (source == null)
  103. throw new ArgumentNullException(nameof(source));
  104. if (keySelector == null)
  105. throw new ArgumentNullException(nameof(keySelector));
  106. if (comparer == null)
  107. throw new ArgumentNullException(nameof(comparer));
  108. return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue), cancellationToken);
  109. }
  110. private static async Task<TSource> Max_<TSource>(IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
  111. {
  112. return (await MaxBy(source, x => x, comparer, cancellationToken)
  113. .ConfigureAwait(false)).First();
  114. }
  115. }
  116. }