Count.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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<int> Count<TSource>(this IAsyncEnumerable<TSource> source)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. return CountCore(source, CancellationToken.None);
  16. }
  17. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  18. {
  19. if (source == null)
  20. throw new ArgumentNullException(nameof(source));
  21. return CountCore(source, cancellationToken);
  22. }
  23. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  24. {
  25. if (source == null)
  26. throw new ArgumentNullException(nameof(source));
  27. if (predicate == null)
  28. throw new ArgumentNullException(nameof(predicate));
  29. return CountCore(source, predicate, CancellationToken.None);
  30. }
  31. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  32. {
  33. if (source == null)
  34. throw new ArgumentNullException(nameof(source));
  35. if (predicate == null)
  36. throw new ArgumentNullException(nameof(predicate));
  37. return CountCore(source, predicate, cancellationToken);
  38. }
  39. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
  40. {
  41. if (source == null)
  42. throw new ArgumentNullException(nameof(source));
  43. if (predicate == null)
  44. throw new ArgumentNullException(nameof(predicate));
  45. return CountCore(source, predicate, CancellationToken.None);
  46. }
  47. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  48. {
  49. if (source == null)
  50. throw new ArgumentNullException(nameof(source));
  51. if (predicate == null)
  52. throw new ArgumentNullException(nameof(predicate));
  53. return CountCore(source, predicate, cancellationToken);
  54. }
  55. private static Task<int> CountCore<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  56. {
  57. if (source is ICollection<TSource> collection)
  58. {
  59. return Task.FromResult(collection.Count);
  60. }
  61. if (source is IAsyncIListProvider<TSource> listProv)
  62. {
  63. return listProv.GetCountAsync(onlyIfCheap: false, cancellationToken);
  64. }
  65. return Core();
  66. async Task<int> Core()
  67. {
  68. var count = 0;
  69. var e = source.GetAsyncEnumerator(cancellationToken);
  70. try
  71. {
  72. while (await e.MoveNextAsync().ConfigureAwait(false))
  73. {
  74. checked
  75. {
  76. count++;
  77. }
  78. }
  79. }
  80. finally
  81. {
  82. await e.DisposeAsync().ConfigureAwait(false);
  83. }
  84. return count;
  85. }
  86. }
  87. private static async Task<int> CountCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  88. {
  89. var count = 0;
  90. var e = source.GetAsyncEnumerator(cancellationToken);
  91. try
  92. {
  93. while (await e.MoveNextAsync().ConfigureAwait(false))
  94. {
  95. if (predicate(e.Current))
  96. {
  97. checked
  98. {
  99. count++;
  100. }
  101. }
  102. }
  103. }
  104. finally
  105. {
  106. await e.DisposeAsync().ConfigureAwait(false);
  107. }
  108. return count;
  109. }
  110. private static async Task<int> CountCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  111. {
  112. var count = 0;
  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. return count;
  132. }
  133. }
  134. }