Count.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. /// <summary>
  13. /// Returns an async-enumerable sequence containing an <see cref="int" /> that represents the total number of elements in an async-enumerable sequence.
  14. /// </summary>
  15. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  16. /// <param name="source">An async-enumerable sequence that contains elements to be counted.</param>
  17. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  18. /// <returns>An async-enumerable sequence containing a single element with the number of elements in the input sequence.</returns>
  19. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  20. /// <exception cref="OverflowException">(Asynchronous) The number of elements in the source sequence is larger than <see cref="long.MaxValue"/>.</exception>
  21. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  22. public static ValueTask<int> CountAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  23. {
  24. if (source == null)
  25. throw Error.ArgumentNull(nameof(source));
  26. return source switch
  27. {
  28. ICollection<TSource> collection => new ValueTask<int>(collection.Count),
  29. IAsyncIListProvider<TSource> listProv => listProv.GetCountAsync(onlyIfCheap: false, cancellationToken),
  30. ICollection collection => new ValueTask<int>(collection.Count),
  31. _ => Core(source, cancellationToken),
  32. };
  33. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  34. {
  35. var count = 0;
  36. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  37. {
  38. checked
  39. {
  40. count++;
  41. }
  42. }
  43. return count;
  44. }
  45. }
  46. /// <summary>
  47. /// Returns an async-enumerable sequence containing an <see cref="int" /> that represents how many elements in the specified async-enumerable sequence satisfy a condition.
  48. /// </summary>
  49. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  50. /// <param name="source">An async-enumerable sequence that contains elements to be counted.</param>
  51. /// <param name="predicate">A function to test each element for a condition.</param>
  52. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  53. /// <returns>An async-enumerable sequence containing a single element with a number that represents how many elements in the input sequence satisfy the condition in the predicate function.</returns>
  54. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
  55. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  56. public static ValueTask<int> CountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
  57. {
  58. if (source == null)
  59. throw Error.ArgumentNull(nameof(source));
  60. if (predicate == null)
  61. throw Error.ArgumentNull(nameof(predicate));
  62. return Core(source, predicate, cancellationToken);
  63. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  64. {
  65. var count = 0;
  66. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  67. {
  68. if (predicate(item))
  69. {
  70. checked
  71. {
  72. count++;
  73. }
  74. }
  75. }
  76. return count;
  77. }
  78. }
  79. internal static ValueTask<int> CountAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  80. {
  81. if (source == null)
  82. throw Error.ArgumentNull(nameof(source));
  83. if (predicate == null)
  84. throw Error.ArgumentNull(nameof(predicate));
  85. return Core(source, predicate, cancellationToken);
  86. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  87. {
  88. var count = 0;
  89. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  90. {
  91. if (await predicate(item).ConfigureAwait(false))
  92. {
  93. checked
  94. {
  95. count++;
  96. }
  97. }
  98. }
  99. return count;
  100. }
  101. }
  102. #if !NO_DEEP_CANCELLATION
  103. internal static ValueTask<int> CountAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  104. {
  105. if (source == null)
  106. throw Error.ArgumentNull(nameof(source));
  107. if (predicate == null)
  108. throw Error.ArgumentNull(nameof(predicate));
  109. return Core(source, predicate, cancellationToken);
  110. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  111. {
  112. var count = 0;
  113. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  114. {
  115. if (await predicate(item, cancellationToken).ConfigureAwait(false))
  116. {
  117. checked
  118. {
  119. count++;
  120. }
  121. }
  122. }
  123. return count;
  124. }
  125. }
  126. #endif
  127. }
  128. }