Count.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. if (source is ICollection<TSource> collection)
  18. {
  19. return Task.FromResult(collection.Count);
  20. }
  21. if (source is IIListProvider<TSource> listProv)
  22. {
  23. return listProv.GetCountAsync(false, cancellationToken);
  24. }
  25. return source.Aggregate(0, (c, _) => checked(c + 1), cancellationToken);
  26. }
  27. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  28. {
  29. if (source == null)
  30. throw new ArgumentNullException(nameof(source));
  31. if (predicate == null)
  32. throw new ArgumentNullException(nameof(predicate));
  33. return source.Where(predicate)
  34. .Aggregate(0, (c, _) => checked(c + 1), cancellationToken);
  35. }
  36. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source)
  37. {
  38. if (source == null)
  39. throw new ArgumentNullException(nameof(source));
  40. return Count(source, CancellationToken.None);
  41. }
  42. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  43. {
  44. if (source == null)
  45. throw new ArgumentNullException(nameof(source));
  46. if (predicate == null)
  47. throw new ArgumentNullException(nameof(predicate));
  48. return Count(source, predicate, CancellationToken.None);
  49. }
  50. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  51. {
  52. if (source == null)
  53. throw new ArgumentNullException(nameof(source));
  54. return source.Aggregate(0L, (c, _) => checked(c + 1), cancellationToken);
  55. }
  56. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  57. {
  58. if (source == null)
  59. throw new ArgumentNullException(nameof(source));
  60. if (predicate == null)
  61. throw new ArgumentNullException(nameof(predicate));
  62. return source.Where(predicate)
  63. .Aggregate(0L, (c, _) => checked(c + 1), cancellationToken);
  64. }
  65. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source)
  66. {
  67. if (source == null)
  68. throw new ArgumentNullException(nameof(source));
  69. return LongCount(source, CancellationToken.None);
  70. }
  71. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  72. {
  73. if (source == null)
  74. throw new ArgumentNullException(nameof(source));
  75. if (predicate == null)
  76. throw new ArgumentNullException(nameof(predicate));
  77. return LongCount(source, predicate, CancellationToken.None);
  78. }
  79. }
  80. }