LongCount.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #if REFERENCE_ASSEMBLY
  10. public static partial class AsyncEnumerableDeprecated
  11. #else
  12. public static partial class AsyncEnumerable
  13. #endif
  14. {
  15. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  16. // 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)
  17. /// <summary>
  18. /// Returns an async-enumerable sequence containing an <see cref="long" /> that represents the total number of elements in an async-enumerable sequence.
  19. /// </summary>
  20. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  21. /// <param name="source">An async-enumerable sequence that contains elements to be counted.</param>
  22. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  23. /// <returns>An async-enumerable sequence containing a single element with the number of elements in the input sequence.</returns>
  24. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  25. /// <exception cref="OverflowException">(Asynchronous) The number of elements in the source sequence is larger than <see cref="long.MaxValue"/>.</exception>
  26. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  27. public static ValueTask<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  28. {
  29. if (source == null)
  30. throw Error.ArgumentNull(nameof(source));
  31. return Core(source, cancellationToken);
  32. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  33. {
  34. var count = 0L;
  35. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  36. {
  37. checked
  38. {
  39. count++;
  40. }
  41. }
  42. return count;
  43. }
  44. }
  45. // 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)
  46. /// <summary>
  47. /// Returns an async-enumerable sequence containing an <see cref="long" /> 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<long> LongCountAsync<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<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  64. {
  65. var count = 0L;
  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. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  80. /// <summary>
  81. /// 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.
  82. /// </summary>
  83. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  84. /// <param name="source">An async-enumerable sequence that contains elements to be counted.</param>
  85. /// <param name="predicate">An asynchronous predicate to test each element for a condition.</param>
  86. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  87. /// <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>
  88. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
  89. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  90. [GenerateAsyncOverload]
  91. [Obsolete("Use LongCountAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the LongCountAwaitAsync functionality now exists as overloads of LongCountAsync.")]
  92. private static ValueTask<long> LongCountAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  93. {
  94. if (source == null)
  95. throw Error.ArgumentNull(nameof(source));
  96. if (predicate == null)
  97. throw Error.ArgumentNull(nameof(predicate));
  98. return Core(source, predicate, cancellationToken);
  99. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  100. {
  101. var count = 0L;
  102. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  103. {
  104. if (await predicate(item).ConfigureAwait(false))
  105. {
  106. checked
  107. {
  108. count++;
  109. }
  110. }
  111. }
  112. return count;
  113. }
  114. }
  115. #if !NO_DEEP_CANCELLATION
  116. [GenerateAsyncOverload]
  117. [Obsolete("Use LongCountAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the LongCountAwaitWithCancellationAsync functionality now exists as overloads of LongCountAsync.")]
  118. private static ValueTask<long> LongCountAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  119. {
  120. if (source == null)
  121. throw Error.ArgumentNull(nameof(source));
  122. if (predicate == null)
  123. throw Error.ArgumentNull(nameof(predicate));
  124. return Core(source, predicate, cancellationToken);
  125. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  126. {
  127. var count = 0L;
  128. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  129. {
  130. if (await predicate(item, cancellationToken).ConfigureAwait(false))
  131. {
  132. checked
  133. {
  134. count++;
  135. }
  136. }
  137. }
  138. return count;
  139. }
  140. }
  141. #endif
  142. }
  143. }