1
0

Count.cs 5.4 KB

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