LongCount.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. public static Task<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  12. {
  13. if (source == null)
  14. throw Error.ArgumentNull(nameof(source));
  15. return Core(source, cancellationToken);
  16. static async Task<long> Core(IAsyncEnumerable<TSource> _source, CancellationToken _cancellationToken)
  17. {
  18. var count = 0L;
  19. #if CSHARP8
  20. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  21. {
  22. checked
  23. {
  24. count++;
  25. }
  26. }
  27. #else
  28. var e = _source.GetAsyncEnumerator(_cancellationToken);
  29. try
  30. {
  31. while (await e.MoveNextAsync().ConfigureAwait(false))
  32. {
  33. checked
  34. {
  35. count++;
  36. }
  37. }
  38. }
  39. finally
  40. {
  41. await e.DisposeAsync().ConfigureAwait(false);
  42. }
  43. #endif
  44. return count;
  45. }
  46. }
  47. public static Task<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
  48. {
  49. if (source == null)
  50. throw Error.ArgumentNull(nameof(source));
  51. if (predicate == null)
  52. throw Error.ArgumentNull(nameof(predicate));
  53. return Core(source, predicate, cancellationToken);
  54. static async Task<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, bool> _predicate, CancellationToken _cancellationToken)
  55. {
  56. var count = 0L;
  57. #if CSHARP8
  58. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  59. {
  60. if (_predicate(item))
  61. {
  62. checked
  63. {
  64. count++;
  65. }
  66. }
  67. }
  68. #else
  69. var e = _source.GetAsyncEnumerator(_cancellationToken);
  70. try
  71. {
  72. while (await e.MoveNextAsync().ConfigureAwait(false))
  73. {
  74. if (_predicate(e.Current))
  75. {
  76. checked
  77. {
  78. count++;
  79. }
  80. }
  81. }
  82. }
  83. finally
  84. {
  85. await e.DisposeAsync().ConfigureAwait(false);
  86. }
  87. #endif
  88. return count;
  89. }
  90. }
  91. public static Task<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  92. {
  93. if (source == null)
  94. throw Error.ArgumentNull(nameof(source));
  95. if (predicate == null)
  96. throw Error.ArgumentNull(nameof(predicate));
  97. return Core(source, predicate, cancellationToken);
  98. static async Task<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  99. {
  100. var count = 0L;
  101. #if CSHARP8
  102. await foreach (TSource 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. #else
  113. var e = _source.GetAsyncEnumerator(_cancellationToken);
  114. try
  115. {
  116. while (await e.MoveNextAsync().ConfigureAwait(false))
  117. {
  118. if (await _predicate(e.Current).ConfigureAwait(false))
  119. {
  120. checked
  121. {
  122. count++;
  123. }
  124. }
  125. }
  126. }
  127. finally
  128. {
  129. await e.DisposeAsync().ConfigureAwait(false);
  130. }
  131. #endif
  132. return count;
  133. }
  134. }
  135. #if !NO_DEEP_CANCELLATION
  136. public static Task<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  137. {
  138. if (source == null)
  139. throw Error.ArgumentNull(nameof(source));
  140. if (predicate == null)
  141. throw Error.ArgumentNull(nameof(predicate));
  142. return Core(source, predicate, cancellationToken);
  143. static async Task<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  144. {
  145. var count = 0L;
  146. #if CSHARP8
  147. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  148. {
  149. if (await _predicate(item, _cancellationToken).ConfigureAwait(false))
  150. {
  151. checked
  152. {
  153. count++;
  154. }
  155. }
  156. }
  157. #else
  158. var e = _source.GetAsyncEnumerator(_cancellationToken);
  159. try
  160. {
  161. while (await e.MoveNextAsync().ConfigureAwait(false))
  162. {
  163. if (await _predicate(e.Current, _cancellationToken).ConfigureAwait(false))
  164. {
  165. checked
  166. {
  167. count++;
  168. }
  169. }
  170. }
  171. }
  172. finally
  173. {
  174. await e.DisposeAsync().ConfigureAwait(false);
  175. }
  176. #endif
  177. return count;
  178. }
  179. }
  180. #endif
  181. }
  182. }