1
0

Max.cs 5.1 KB

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