LongCount.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  20. {
  21. checked
  22. {
  23. count++;
  24. }
  25. }
  26. return count;
  27. }
  28. }
  29. public static Task<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
  30. {
  31. if (source == null)
  32. throw Error.ArgumentNull(nameof(source));
  33. if (predicate == null)
  34. throw Error.ArgumentNull(nameof(predicate));
  35. return Core(source, predicate, cancellationToken);
  36. static async Task<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, bool> _predicate, CancellationToken _cancellationToken)
  37. {
  38. var count = 0L;
  39. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  40. {
  41. if (_predicate(item))
  42. {
  43. checked
  44. {
  45. count++;
  46. }
  47. }
  48. }
  49. return count;
  50. }
  51. }
  52. public static Task<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<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 Task<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  60. {
  61. var count = 0L;
  62. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  63. {
  64. if (await _predicate(item).ConfigureAwait(false))
  65. {
  66. checked
  67. {
  68. count++;
  69. }
  70. }
  71. }
  72. return count;
  73. }
  74. }
  75. #if !NO_DEEP_CANCELLATION
  76. public static Task<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  77. {
  78. if (source == null)
  79. throw Error.ArgumentNull(nameof(source));
  80. if (predicate == null)
  81. throw Error.ArgumentNull(nameof(predicate));
  82. return Core(source, predicate, cancellationToken);
  83. static async Task<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  84. {
  85. var count = 0L;
  86. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  87. {
  88. if (await _predicate(item, _cancellationToken).ConfigureAwait(false))
  89. {
  90. checked
  91. {
  92. count++;
  93. }
  94. }
  95. }
  96. return count;
  97. }
  98. }
  99. #endif
  100. }
  101. }