-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclamp.cpp
More file actions
executable file
·52 lines (44 loc) · 1.5 KB
/
Copy pathclamp.cpp
File metadata and controls
executable file
·52 lines (44 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright David Stone 2020.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
export module bounded.clamp;
import bounded.bounded_integer;
import bounded.integer;
import bounded.integral;
import bounded.minmax;
import numeric_traits;
namespace bounded {
export constexpr auto clamp(bounded_integer auto const & value, bounded_integer auto const & minimum, bounded_integer auto const & maximum) {
return min(
max(
value,
minimum
),
maximum
);
}
export template<typename Target>
constexpr auto clamp(integral auto const value) {
return ::bounded::clamp(bounded::integer(value), numeric_traits::min_value<Target>, numeric_traits::max_value<Target>);
}
} // namespace bounded
constexpr auto minimum = bounded::constant<27>;
constexpr auto maximum = bounded::constant<567>;
static_assert(
clamp(bounded::constant<20>, minimum, maximum) == minimum,
"Failure to properly clamp lesser positive values."
);
static_assert(
clamp(bounded::constant<-25>, minimum, maximum) == minimum,
"Failure to properly clamp negative values to a positive value."
);
static_assert(
clamp(bounded::constant<1000>, minimum, maximum) == maximum,
"Failure to properly clamp greater positive values."
);
static_assert(
clamp(bounded::constant<2000>, minimum, maximum) == maximum,
"Fail to clamp above range with a strictly greater type."
);
static_assert(bounded::clamp<bounded::integer<30, 40>>(20) == bounded::constant<30>);