Count.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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;
  5. using System.Collections.Generic;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerable
  11. {
  12. public static ValueTask<int> CountAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  13. {
  14. if (source == null)
  15. throw Error.ArgumentNull(nameof(source));
  16. return source switch
  17. {
  18. ICollection<TSource> collection => new ValueTask<int>(collection.Count),
  19. IAsyncIListProvider<TSource> listProv => listProv.GetCountAsync(onlyIfCheap: false, cancellationToken),
  20. ICollection collection => new ValueTask<int>(collection.Count),
  21. _ => Core(source, cancellationToken),
  22. };
  23. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  24. {
  25. var count = 0;
  26. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  27. {
  28. checked
  29. {
  30. count++;
  31. }
  32. }
  33. return count;
  34. }
  35. }
  36. public static ValueTask<int> CountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
  37. {
  38. if (source == null)
  39. throw Error.ArgumentNull(nameof(source));
  40. if (predicate == null)
  41. throw Error.ArgumentNull(nameof(predicate));
  42. return Core(source, predicate, cancellationToken);
  43. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  44. {
  45. var count = 0;
  46. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  47. {
  48. if (predicate(item))
  49. {
  50. checked
  51. {
  52. count++;
  53. }
  54. }
  55. }
  56. return count;
  57. }
  58. }
  59. internal static ValueTask<int> CountAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  60. {
  61. if (source == null)
  62. throw Error.ArgumentNull(nameof(source));
  63. if (predicate == null)
  64. throw Error.ArgumentNull(nameof(predicate));
  65. return Core(source, predicate, cancellationToken);
  66. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  67. {
  68. var count = 0;
  69. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  70. {
  71. if (await predicate(item).ConfigureAwait(false))
  72. {
  73. checked
  74. {
  75. count++;
  76. }
  77. }
  78. }
  79. return count;
  80. }
  81. }
  82. #if !NO_DEEP_CANCELLATION
  83. internal static ValueTask<int> CountAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  84. {
  85. if (source == null)
  86. throw Error.ArgumentNull(nameof(source));
  87. if (predicate == null)
  88. throw Error.ArgumentNull(nameof(predicate));
  89. return Core(source, predicate, cancellationToken);
  90. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  91. {
  92. var count = 0;
  93. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  94. {
  95. if (await predicate(item, cancellationToken).ConfigureAwait(false))
  96. {
  97. checked
  98. {
  99. count++;
  100. }
  101. }
  102. }
  103. return count;
  104. }
  105. }
  106. #endif
  107. }
  108. }