Count.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. if (source is ICollection<TSource> collection)
  18. {
  19. return Task.FromResult(collection.Count);
  20. }
  21. if (source is IIListProvider<TSource> listProv)
  22. {
  23. return listProv.GetCountAsync(false, cancellationToken);
  24. }
  25. return source.Aggregate(0, (c, _) => checked(c + 1), cancellationToken);
  26. }
  27. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  28. {
  29. if (source == null)
  30. throw new ArgumentNullException(nameof(source));
  31. if (predicate == null)
  32. throw new ArgumentNullException(nameof(predicate));
  33. return source.Where(predicate)
  34. .Aggregate(0, (c, _) => checked(c + 1), cancellationToken);
  35. }
  36. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  37. {
  38. if (source == null)
  39. throw new ArgumentNullException(nameof(source));
  40. if (predicate == null)
  41. throw new ArgumentNullException(nameof(predicate));
  42. return source.Where(predicate)
  43. .Aggregate(0, (c, _) => checked(c + 1), cancellationToken);
  44. }
  45. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source)
  46. {
  47. if (source == null)
  48. throw new ArgumentNullException(nameof(source));
  49. return Count(source, CancellationToken.None);
  50. }
  51. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  52. {
  53. if (source == null)
  54. throw new ArgumentNullException(nameof(source));
  55. if (predicate == null)
  56. throw new ArgumentNullException(nameof(predicate));
  57. return Count(source, predicate, CancellationToken.None);
  58. }
  59. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
  60. {
  61. if (source == null)
  62. throw new ArgumentNullException(nameof(source));
  63. if (predicate == null)
  64. throw new ArgumentNullException(nameof(predicate));
  65. return Count(source, predicate, CancellationToken.None);
  66. }
  67. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  68. {
  69. if (source == null)
  70. throw new ArgumentNullException(nameof(source));
  71. return source.Aggregate(0L, (c, _) => checked(c + 1), cancellationToken);
  72. }
  73. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  74. {
  75. if (source == null)
  76. throw new ArgumentNullException(nameof(source));
  77. if (predicate == null)
  78. throw new ArgumentNullException(nameof(predicate));
  79. return source.Where(predicate)
  80. .Aggregate(0L, (c, _) => checked(c + 1), cancellationToken);
  81. }
  82. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  83. {
  84. if (source == null)
  85. throw new ArgumentNullException(nameof(source));
  86. if (predicate == null)
  87. throw new ArgumentNullException(nameof(predicate));
  88. return source.Where(predicate)
  89. .Aggregate(0L, (c, _) => checked(c + 1), cancellationToken);
  90. }
  91. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source)
  92. {
  93. if (source == null)
  94. throw new ArgumentNullException(nameof(source));
  95. return LongCount(source, CancellationToken.None);
  96. }
  97. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  98. {
  99. if (source == null)
  100. throw new ArgumentNullException(nameof(source));
  101. if (predicate == null)
  102. throw new ArgumentNullException(nameof(predicate));
  103. return LongCount(source, predicate, CancellationToken.None);
  104. }
  105. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
  106. {
  107. if (source == null)
  108. throw new ArgumentNullException(nameof(source));
  109. if (predicate == null)
  110. throw new ArgumentNullException(nameof(predicate));
  111. return LongCount(source, predicate, CancellationToken.None);
  112. }
  113. }
  114. }