LongCount.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  12. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.longcountasync?view=net-9.0-pp#system-linq-asyncenumerable-longcountasync-1(system-collections-generic-iasyncenumerable((-0))-system-threading-cancellationtoken)
  13. /// <summary>
  14. /// Returns an async-enumerable sequence containing an <see cref="long" /> that represents the total number of elements in an async-enumerable sequence.
  15. /// </summary>
  16. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  17. /// <param name="source">An async-enumerable sequence that contains elements to be counted.</param>
  18. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  19. /// <returns>An async-enumerable sequence containing a single element with the number of elements in the input sequence.</returns>
  20. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  21. /// <exception cref="OverflowException">(Asynchronous) The number of elements in the source sequence is larger than <see cref="long.MaxValue"/>.</exception>
  22. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  23. public static ValueTask<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  24. {
  25. if (source == null)
  26. throw Error.ArgumentNull(nameof(source));
  27. return Core(source, cancellationToken);
  28. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  29. {
  30. var count = 0L;
  31. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  32. {
  33. checked
  34. {
  35. count++;
  36. }
  37. }
  38. return count;
  39. }
  40. }
  41. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.longcountasync?view=net-9.0-pp#system-linq-asyncenumerable-longcountasync-1(system-collections-generic-iasyncenumerable((-0))-system-func((-0-system-boolean))-system-threading-cancellationtoken)
  42. /// <summary>
  43. /// Returns an async-enumerable sequence containing an <see cref="long" /> that represents how many elements in the specified async-enumerable sequence satisfy a condition.
  44. /// </summary>
  45. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  46. /// <param name="source">An async-enumerable sequence that contains elements to be counted.</param>
  47. /// <param name="predicate">A function to test each element for a condition.</param>
  48. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  49. /// <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>
  50. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
  51. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  52. public static ValueTask<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
  53. {
  54. if (source == null)
  55. throw Error.ArgumentNull(nameof(source));
  56. if (predicate == null)
  57. throw Error.ArgumentNull(nameof(predicate));
  58. return Core(source, predicate, cancellationToken);
  59. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  60. {
  61. var count = 0L;
  62. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  63. {
  64. if (predicate(item))
  65. {
  66. checked
  67. {
  68. count++;
  69. }
  70. }
  71. }
  72. return count;
  73. }
  74. }
  75. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  76. /// <summary>
  77. /// Returns an async-enumerable sequence containing a <see cref="long" /> that represents the number of elements in the specified async-enumerable sequence that satisfy a condition.
  78. /// </summary>
  79. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  80. /// <param name="source">An async-enumerable sequence that contains elements to be counted.</param>
  81. /// <param name="predicate">An asynchronous predicate to test each element for a condition.</param>
  82. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  83. /// <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>
  84. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
  85. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  86. [GenerateAsyncOverload]
  87. [Obsolete("Use LongCountAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the LongCountAwaitAsync functionality now exists as overloads of LongCountAsync.")]
  88. private static ValueTask<long> LongCountAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  89. {
  90. if (source == null)
  91. throw Error.ArgumentNull(nameof(source));
  92. if (predicate == null)
  93. throw Error.ArgumentNull(nameof(predicate));
  94. return Core(source, predicate, cancellationToken);
  95. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  96. {
  97. var count = 0L;
  98. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  99. {
  100. if (await predicate(item).ConfigureAwait(false))
  101. {
  102. checked
  103. {
  104. count++;
  105. }
  106. }
  107. }
  108. return count;
  109. }
  110. }
  111. #if !NO_DEEP_CANCELLATION
  112. [GenerateAsyncOverload]
  113. [Obsolete("Use LongCountAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the LongCountAwaitWithCancellationAsync functionality now exists as overloads of LongCountAsync.")]
  114. private static ValueTask<long> LongCountAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  115. {
  116. if (source == null)
  117. throw Error.ArgumentNull(nameof(source));
  118. if (predicate == null)
  119. throw Error.ArgumentNull(nameof(predicate));
  120. return Core(source, predicate, cancellationToken);
  121. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  122. {
  123. var count = 0L;
  124. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  125. {
  126. if (await predicate(item, cancellationToken).ConfigureAwait(false))
  127. {
  128. checked
  129. {
  130. count++;
  131. }
  132. }
  133. }
  134. return count;
  135. }
  136. }
  137. #endif
  138. }
  139. }