Branch data Line data Source code
1 : : #pragma once
2 : :
3 : : #include <cassert>
4 : : #include <cstddef>
5 : : #include <functional>
6 : : #include <limits>
7 : : #include <type_traits>
8 : :
9 : : namespace sl3
10 : : {
11 : :
12 : : template <typename T>
13 : : inline size_t
14 : 442 : as_size_t (T val)
15 : : {
16 : : static_assert (std::is_signed_v<T>);
17 : 442 : assert (val >= 0); // LCOV_EXCL_BR_LINE
18 : 442 : return static_cast<size_t> (val);
19 : : }
20 : :
21 : : inline int
22 : 8 : as_int (size_t val)
23 : : {
24 : 8 : assert (val < std::numeric_limits<int>::max ()); // LCOV_EXCL_BR_LINE
25 : 8 : return static_cast<int> (val);
26 : : }
27 : :
28 : : template <typename T1, typename T2>
29 : : bool
30 : 10 : is_less (const T1& a, const T2& b)
31 : : {
32 : : using common_type = std::common_type_t<T1, T2>;
33 : 20 : return std::less<common_type>{}(static_cast<common_type> (a),
34 : 10 : static_cast<common_type> (b));
35 : : }
36 : :
37 : : template <typename T1, typename T2>
38 : : bool
39 : 3 : is_greater (const T1& a, const T2& b)
40 : : {
41 : : using common_type = std::common_type_t<T1, T2>;
42 : 6 : return std::greater<common_type>{}(static_cast<common_type> (a),
43 : 3 : static_cast<common_type> (b));
44 : : }
45 : :
46 : : template <typename T1, typename T2>
47 : : bool
48 : 3 : is_less_equal (const T1& a, const T2& b)
49 : : {
50 : : using common_type = std::common_type_t<T1, T2>;
51 : 6 : return std::less_equal<common_type>{}(static_cast<common_type> (a),
52 : 3 : static_cast<common_type> (b));
53 : : }
54 : :
55 : : template <typename T1, typename T2>
56 : : bool
57 : 4 : is_equal (const T1& a, const T2& b)
58 : : {
59 : : using common_type = std::common_type_t<T1, T2>;
60 : 8 : return std::equal_to<common_type>{}(static_cast<common_type> (a),
61 : 4 : static_cast<common_type> (b));
62 : : }
63 : :
64 : : }
|