Count.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Cysharp.Threading.Tasks.Internal;
  2. using System;
  3. using System.Threading;
  4. namespace Cysharp.Threading.Tasks.Linq
  5. {
  6. public static partial class UniTaskAsyncEnumerable
  7. {
  8. public static UniTask<Int32> CountAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. return Count.CountAsync(source, cancellationToken);
  12. }
  13. public static UniTask<Int32> CountAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
  14. {
  15. Error.ThrowArgumentNullException(source, nameof(source));
  16. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  17. return Count.CountAsync(source, predicate, cancellationToken);
  18. }
  19. public static UniTask<Int32> CountAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
  20. {
  21. Error.ThrowArgumentNullException(source, nameof(source));
  22. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  23. return Count.CountAwaitAsync(source, predicate, cancellationToken);
  24. }
  25. public static UniTask<Int32> CountAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
  26. {
  27. Error.ThrowArgumentNullException(source, nameof(source));
  28. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  29. return Count.CountAwaitWithCancellationAsync(source, predicate, cancellationToken);
  30. }
  31. }
  32. internal static class Count
  33. {
  34. internal static async UniTask<int> CountAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  35. {
  36. var count = 0;
  37. var e = source.GetAsyncEnumerator(cancellationToken);
  38. try
  39. {
  40. while (await e.MoveNextAsync())
  41. {
  42. checked { count++; }
  43. }
  44. }
  45. finally
  46. {
  47. if (e != null)
  48. {
  49. await e.DisposeAsync();
  50. }
  51. }
  52. return count;
  53. }
  54. internal static async UniTask<int> CountAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken)
  55. {
  56. var count = 0;
  57. var e = source.GetAsyncEnumerator(cancellationToken);
  58. try
  59. {
  60. while (await e.MoveNextAsync())
  61. {
  62. if (predicate(e.Current))
  63. {
  64. checked { count++; }
  65. }
  66. }
  67. }
  68. finally
  69. {
  70. if (e != null)
  71. {
  72. await e.DisposeAsync();
  73. }
  74. }
  75. return count;
  76. }
  77. internal static async UniTask<int> CountAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
  78. {
  79. var count = 0;
  80. var e = source.GetAsyncEnumerator(cancellationToken);
  81. try
  82. {
  83. while (await e.MoveNextAsync())
  84. {
  85. if (await predicate(e.Current))
  86. {
  87. checked { count++; }
  88. }
  89. }
  90. }
  91. finally
  92. {
  93. if (e != null)
  94. {
  95. await e.DisposeAsync();
  96. }
  97. }
  98. return count;
  99. }
  100. internal static async UniTask<int> CountAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
  101. {
  102. var count = 0;
  103. var e = source.GetAsyncEnumerator(cancellationToken);
  104. try
  105. {
  106. while (await e.MoveNextAsync())
  107. {
  108. if (await predicate(e.Current, cancellationToken))
  109. {
  110. checked { count++; }
  111. }
  112. }
  113. }
  114. finally
  115. {
  116. if (e != null)
  117. {
  118. await e.DisposeAsync();
  119. }
  120. }
  121. return count;
  122. }
  123. }
  124. }