VCCC  2024.05
VisualCamp Common C++ library
count_if.hpp
Go to the documentation of this file.
1 //
2 // Created by yonggyulee on 1/18/24.
3 //
4 
5 #ifndef VCCC_ALGORITHM_RANGES_COUNT_IF_HPP
6 #define VCCC_ALGORITHM_RANGES_COUNT_IF_HPP
7 
8 #include <functional>
9 #include <type_traits>
10 
17 #include "vccc/__ranges/begin.hpp"
18 #include "vccc/__ranges/end.hpp"
21 
22 namespace vccc {
23 namespace ranges {
24 namespace detail {
25 
26 struct count_if_niebloid {
27  template<typename I, typename S, typename Proj = identity, typename Pred, std::enable_if_t<
28  algo_check_unary_input_iterator<indirect_unary_predicate, I, S, Proj, Pred>
29  ::value, int> = 0>
30  constexpr iter_difference_t<I> operator()(I first, S last, Pred pred, Proj proj = {}) const {
31  using namespace vccc::rel_ops;
32 
33  iter_difference_t<I> counter = 0;
34 
35  for (; first != last; ++first) {
36  if (vccc::invoke(pred, vccc::invoke(proj, *first))) {
37  ++counter;
38  }
39  }
40 
41  return counter;
42  }
43 
44  template<typename R, typename Proj = identity, typename Pred, std::enable_if_t<
45  algo_check_unary_input_range<indirect_unary_predicate, R, Proj, Pred>
46  ::value, int> = 0>
47  constexpr range_difference_t<R>
48  operator()(R&& r, Pred pred, Proj proj = {}) const {
49  return (*this)(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
50  }
51 };
52 
53 } // namespace detail
54 
57 
58 VCCC_INLINE_OR_STATIC constexpr detail::count_if_niebloid count_if{};
59 
61 
62 } // namespace ranges
63 } // namespace vccc
64 
65 #endif // VCCC_ALGORITHM_RANGES_COUNT_IF_HPP
constexpr VCCC_INLINE_OR_STATIC detail::count_if_niebloid count_if
Definition: count_if.hpp:58
constexpr invoke_result_t< F, Args... > invoke(F &&f, Args &&... args) noexcept(is_nothrow_invocable< F, Args... >::value)
Definition: invoke.hpp:38
constexpr VCCC_INLINE_OR_STATIC detail::begin_niebloid begin
returns an iterator to the beginning of a range
Definition: begin.hpp:116
constexpr VCCC_INLINE_OR_STATIC detail::end_niebloid end
returns a sentinel indicating the end of a range
Definition: end.hpp:120
#define VCCC_INLINE_OR_STATIC
Definition: inline_or_static.hpp:9
Definition: cxx20_rel_ops.hpp:17
Definition: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 0 > first
Definition: key_value.hpp:34
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35