LongCount.cs 6.6 KB

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