VCCC  2024.05
VisualCamp Common C++ library
count.hpp
Go to the documentation of this file.
1 //
2 // Created by yonggyulee on 1/26/24.
3 //
4 
5 #ifndef VCCC_ALGORITHM_RANGES_COUNT_HPP
6 #define VCCC_ALGORITHM_RANGES_COUNT_HPP
7 
8 #include <functional>
9 #include <type_traits>
10 
18 #include "vccc/__ranges/begin.hpp"
19 #include "vccc/__ranges/end.hpp"
22 
23 namespace vccc {
24 namespace ranges {
25 namespace detail {
26 
27 struct count_niebloid {
28  template<typename I, typename S, typename T, typename Proj = identity, std::enable_if_t<
29  algo_check_binary_input_iterator<indirect_binary_predicate, I, S, const T*, Proj, ranges::equal_to>
30  ::value, int> = 0>
31  constexpr iter_difference_t<I> operator()(I first, S last, const T& value, Proj proj = {}) const {
32  using namespace vccc::rel_ops;
33 
34  iter_difference_t<I> counter = 0;
35 
36  for (; first != last; ++first) {
37  if (vccc::invoke(proj, *first) == value) {
38  ++counter;
39  }
40  }
41 
42  return counter;
43  }
44 
45  template<typename R, typename T, typename Proj = identity, std::enable_if_t<
47  constexpr range_difference_t<R>
48  operator()(R&& r, const T& value, Proj proj = {}) const {
49  return (*this)(ranges::begin(r), ranges::end(r), value, std::ref(proj));
50  }
51 };
52 
53 } // namespace detail
54 
57 
58 VCCC_INLINE_OR_STATIC constexpr detail::count_niebloid count{};
59 
61 
62 } // namespace ranges
63 } // namespace vccc
64 
65 #endif // VCCC_ALGORITHM_RANGES_COUNT_HPP
constexpr VCCC_INLINE_OR_STATIC detail::count_niebloid count
Definition: count.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