Min.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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> Min<TSource>(this IAsyncEnumerable<TSource> source)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. return Min(source, CancellationToken.None);
  16. }
  17. public static Task<TSource> Min<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<TResult> Min<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector)
  25. {
  26. if (source == null)
  27. throw new ArgumentNullException(nameof(source));
  28. if (selector == null)
  29. throw new ArgumentNullException(nameof(selector));
  30. return Min(source, selector, CancellationToken.None);
  31. }
  32. public static Task<TResult> Min<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
  33. {
  34. if (source == null)
  35. throw new ArgumentNullException(nameof(source));
  36. if (selector == null)
  37. throw new ArgumentNullException(nameof(selector));
  38. return source.Select(selector).Min(cancellationToken);
  39. }
  40. public static Task<TResult> Min<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<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 Min(source, selector, CancellationToken.None);
  47. }
  48. public static Task<TResult> Min<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<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).Min(cancellationToken);
  55. }
  56. private static async Task<TSource> MinCore<TSource>(IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
  57. {
  58. var e = source.GetAsyncEnumerator();
  59. try
  60. {
  61. if (!await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  62. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  63. var min = e.Current;
  64. while (await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  65. {
  66. var cur = e.Current;
  67. if (comparer.Compare(cur, min) < 0)
  68. {
  69. min = cur;
  70. }
  71. }
  72. return min;
  73. }
  74. finally
  75. {
  76. await e.DisposeAsync().ConfigureAwait(false);
  77. }
  78. }
  79. }
  80. }