emlabcpp
modern opinionated embedded C++ library
min_max.h
Go to the documentation of this file.
1 #pragma once
23 
24 #include <array>
25 #include <functional>
26 
27 #ifdef EMLABCPP_USE_NLOHMANN_JSON
28 #include <nlohmann/json.hpp>
29 #endif
30 
31 namespace emlabcpp
32 {
33 
35 template < typename T >
36 struct min_max : std::array< T, 2 >
37 {
38  using value_type = T;
39 
40  [[nodiscard]] constexpr T& min()
41  {
42  return this->operator[]( 0 );
43  }
44 
45  [[nodiscard]] constexpr T const& min() const
46  {
47  return this->operator[]( 0 );
48  }
49 
50  [[nodiscard]] constexpr T& max()
51  {
52  return this->operator[]( 1 );
53  }
54 
55  [[nodiscard]] constexpr T const& max() const
56  {
57  return this->operator[]( 1 );
58  }
59 
60  constexpr min_max() = default;
61 
62  constexpr min_max( T min, T max )
63  : std::array< T, 2 >{ std::move( min ), std::move( max ) }
64  {
65  }
66 };
67 
68 template < typename T, typename Compare >
69 constexpr T const& clamp( T const& x, min_max< T > const& mm, Compare&& comp )
70 {
71  return comp( x, mm.min() ) ? mm.min() : comp( mm.max(), x ) ? mm.max() : x;
72 }
73 
74 template < typename T >
75 constexpr T const& clamp( T const& x, min_max< T > const& mm )
76 {
77  return clamp( x, mm, std::less{} );
78 }
79 
80 template < typename T, typename... Args >
81 requires( std::same_as< Args, min_max< T > > && ... )
82 constexpr min_max< T > intersection( min_max< T > const& head, Args const&... args )
83 {
84  min_max< T > res{ head };
85  auto f = [&]( min_max< T > const& other ) {
86  res.min() = std::max( res.min(), other.min() );
87  res.max() = std::min( res.max(), other.max() );
88  };
89  ( f( args ), ... );
90 
91  return res;
92 }
93 
94 template < typename T >
95 constexpr min_max< T > expand( min_max< T > const& mm, T const& val )
96 {
97  if ( val < mm.min() )
98  return { val, mm.max() };
99  else if ( val > mm.max() )
100  return { mm.min(), val };
101  else
102  return mm;
103 }
104 
105 template < typename T >
106 constexpr bool contains( min_max< T > const& mm, T const& val )
107 {
108  return mm.min() <= val && val <= mm.max();
109 }
110 
111 } // namespace emlabcpp
112 
113 namespace std
114 {
115 template < typename T >
116 struct tuple_size< emlabcpp::min_max< T > > : std::integral_constant< std::size_t, 2 >
117 {
118 };
119 
120 template < std::size_t I, typename T >
121 struct tuple_element< I, emlabcpp::min_max< T > >
122 {
123  using type = T;
124 };
125 } // namespace std
126 
127 #ifdef EMLABCPP_USE_NLOHMANN_JSON
128 
129 template < typename T >
130 struct nlohmann::adl_serializer< emlabcpp::min_max< T > >
131 {
132  static void to_json( nlohmann::json& j, emlabcpp::min_max< T > const& mm )
133  {
134  j["min"] = mm.min();
135  j["max"] = mm.max();
136  }
137 
138  static emlabcpp::min_max< T > from_json( nlohmann::json const& j )
139  {
140  return emlabcpp::min_max< T >{ j["min"], j["max"] };
141  }
142 };
143 
144 #endif
MIT License.
Definition: impl.h:31
constexpr reference operator[](size_type const i) noexcept
Provides a reference to item at position i.
Definition: static_storage.h:136
Args const & args
Definition: min_max.h:83
constexpr min_max< T > expand(min_max< T > const &mm, T const &val)
Definition: min_max.h:95
constexpr T const & clamp(T const &x, min_max< T > const &mm, Compare &&comp)
Definition: min_max.h:69
T res
Definition: algorithm.h:505
requires(!range_container< Container >) const expr std
Returns index of an element in tuple 't', for which call to predicate f(x) holds true,...
Definition: algorithm.h:127
constexpr Derived max(vec_point_base< Derived, N > const &a, vec_point_base< Derived, N > const &b)
Definition: vec_point_base.h:229
constexpr Derived const & min(vec_point_base< Derived, N > const &a, vec_point_base< Derived, N > const &b)
Definition: vec_point_base.h:236
constexpr bool contains(Container const &cont, T const &item)
Checks if container cont contains at least one occurence of item, returns true/false.
Definition: algorithm.h:149
UnaryCallable && f
Definition: algorithm.h:161
A structure representing a range defined by a minimum and a maximum value.
Definition: min_max.h:37
constexpr min_max(T min, T max)
Definition: min_max.h:62
constexpr T const & max() const
Definition: min_max.h:55
constexpr T & min()
Definition: min_max.h:40
constexpr T & max()
Definition: min_max.h:50
T value_type
Definition: min_max.h:38
constexpr min_max()=default
constexpr T const & min() const
Definition: min_max.h:45