[libc++][ranges] Implement ranges::count{, _if}
Reviewed By: var-const, Mordante, ldionne, #libc
Spies: tcanens, libcxx-commits, mgorny
Differential Revision: https://reviews.llvm.org/D121523
NOKEYCHECK=True
GitOrigin-RevId: 1306b1025c507ec8c406a975558007e3e94e5cbd
diff --git a/include/algorithm b/include/algorithm
index d86cfe7..401e918 100644
--- a/include/algorithm
+++ b/include/algorithm
@@ -145,6 +145,26 @@
constexpr ranges::binary_transform_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>, O>
transform(R1&& r1, R2&& r2, O result,
F binary_op, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20
+
+ template<input_iterator I, sentinel_for<I> S, class T, class Proj = identity>
+ requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
+ constexpr iter_difference_t<I>
+ count(I first, S last, const T& value, Proj proj = {}); // since C++20
+
+ template<input_range R, class T, class Proj = identity>
+ requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
+ constexpr range_difference_t<R>
+ count(R&& r, const T& value, Proj proj = {}); // since C++20
+
+ template<input_iterator I, sentinel_for<I> S, class Proj = identity,
+ indirect_unary_predicate<projected<I, Proj>> Pred>
+ constexpr iter_difference_t<I>
+ count_if(I first, S last, Pred pred, Proj proj = {}); // since C++20
+
+ template<input_range R, class Proj = identity,
+ indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
+ constexpr range_difference_t<R>
+ count_if(R&& r, Pred pred, Proj proj = {}); // since C++20
}
constexpr bool // constexpr in C++20
@@ -862,6 +882,8 @@
#include <__algorithm/pop_heap.h>
#include <__algorithm/prev_permutation.h>
#include <__algorithm/push_heap.h>
+#include <__algorithm/ranges_count.h>
+#include <__algorithm/ranges_count_if.h>
#include <__algorithm/ranges_find.h>
#include <__algorithm/ranges_find_if.h>
#include <__algorithm/ranges_find_if_not.h>