LongCount.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. async Task<long> Core(IAsyncEnumerable<TSource> _source, CancellationToken _cancellationToken)
  17. {
  18. var count = 0L;
  19. var e = _source.GetAsyncEnumerator(_cancellationToken);
  20. try
  21. {
  22. while (await e.MoveNextAsync().ConfigureAwait(false))
  23. {
  24. checked
  25. {
  26. count++;
  27. }
  28. }
  29. }
  30. finally
  31. {
  32. await e.DisposeAsync().ConfigureAwait(false);
  33. }
  34. return count;
  35. }
  36. }
  37. public static Task<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
  38. {
  39. if (source == null)
  40. throw Error.ArgumentNull(nameof(source));
  41. if (predicate == null)
  42. throw Error.ArgumentNull(nameof(predicate));
  43. return Core(source, predicate, cancellationToken);
  44. async Task<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, bool> _predicate, CancellationToken _cancellationToken)
  45. {
  46. var count = 0L;
  47. var e = _source.GetAsyncEnumerator(_cancellationToken);
  48. try
  49. {
  50. while (await e.MoveNextAsync().ConfigureAwait(false))
  51. {
  52. if (_predicate(e.Current))
  53. {
  54. checked
  55. {
  56. count++;
  57. }
  58. }
  59. }
  60. }
  61. finally
  62. {
  63. await e.DisposeAsync().ConfigureAwait(false);
  64. }
  65. return count;
  66. }
  67. }
  68. public static Task<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  69. {
  70. if (source == null)
  71. throw Error.ArgumentNull(nameof(source));
  72. if (predicate == null)
  73. throw Error.ArgumentNull(nameof(predicate));
  74. return Core(source, predicate, cancellationToken);
  75. async Task<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  76. {
  77. var count = 0L;
  78. var e = _source.GetAsyncEnumerator(_cancellationToken);
  79. try
  80. {
  81. while (await e.MoveNextAsync().ConfigureAwait(false))
  82. {
  83. if (await _predicate(e.Current).ConfigureAwait(false))
  84. {
  85. checked
  86. {
  87. count++;
  88. }
  89. }
  90. }
  91. }
  92. finally
  93. {
  94. await e.DisposeAsync().ConfigureAwait(false);
  95. }
  96. return count;
  97. }
  98. }
  99. #if !NO_DEEP_CANCELLATION
  100. public static Task<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  101. {
  102. if (source == null)
  103. throw Error.ArgumentNull(nameof(source));
  104. if (predicate == null)
  105. throw Error.ArgumentNull(nameof(predicate));
  106. return Core(source, predicate, cancellationToken);
  107. async Task<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  108. {
  109. var count = 0L;
  110. var e = _source.GetAsyncEnumerator(_cancellationToken);
  111. try
  112. {
  113. while (await e.MoveNextAsync().ConfigureAwait(false))
  114. {
  115. if (await _predicate(e.Current, _cancellationToken).ConfigureAwait(false))
  116. {
  117. checked
  118. {
  119. count++;
  120. }
  121. }
  122. }
  123. }
  124. finally
  125. {
  126. await e.DisposeAsync().ConfigureAwait(false);
  127. }
  128. return count;
  129. }
  130. }
  131. #endif
  132. }
  133. }