VCCC  2024.05
VisualCamp Common C++ library
clamp.hpp
Go to the documentation of this file.
1 //
2 // Created by YongGyu Lee on 5/8/24.
3 //
4 
5 #ifndef VCCC_ALGORITHM_RANGES_CLAMP_HPP_
6 #define VCCC_ALGORITHM_RANGES_CLAMP_HPP_
7 
8 #include <utility>
9 
17 
18 namespace vccc {
19 namespace ranges {
20 namespace detail {
21 
22 struct clamp_niebloid {
23  template<typename T, typename Proj = identity, typename Comp = ranges::less, std::enable_if_t<conjunction<
24  projectable<const T*, Proj>,
25  indirect_strict_weak_order<Comp, projected<const T*, Proj>>
26  >::value, int> = 0>
27  constexpr const T& operator()(const T& v, const T& lo, const T& hi, Comp comp = {}, Proj proj = {}) const {
28  auto&& pv = vccc::invoke(proj, v);
29 
30  if (vccc::invoke(comp, std::forward<decltype(pv)>(pv), vccc::invoke(proj, lo)))
31  return lo;
32 
33  if (vccc::invoke(comp, vccc::invoke(proj, hi), std::forward<decltype(pv)>(pv)))
34  return hi;
35 
36  return v;
37  }
38 };
39 
40 } // namespace detail
41 
44 
59 VCCC_INLINE_OR_STATIC constexpr detail::clamp_niebloid clamp{};
60 
62 
63 } // namespace ranges
64 } // namespace vccc
65 
66 #endif // VCCC_ALGORITHM_RANGES_CLAMP_HPP_
constexpr VCCC_INLINE_OR_STATIC detail::clamp_niebloid clamp
clamps a value between a pair of boundary values
Definition: clamp.hpp:59
constexpr invoke_result_t< F, Args... > invoke(F &&f, Args &&... args) noexcept(is_nothrow_invocable< F, Args... >::value)
Definition: invoke.hpp:38
#define VCCC_INLINE_OR_STATIC
Definition: inline_or_static.hpp:9
Definition: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35