Directory: | ./ |
---|---|
File: | include/pinocchio/utils/static-if.hpp |
Date: | 2024-08-27 18:20:05 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 23 | 31 | 74.2% |
Branches: | 11 | 50 | 22.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | // | ||
2 | // Copyright (c) 2019-2020 INRIA | ||
3 | // | ||
4 | |||
5 | #ifndef __pinocchio_utils_static_if_hpp__ | ||
6 | #define __pinocchio_utils_static_if_hpp__ | ||
7 | |||
8 | #include "pinocchio/fwd.hpp" | ||
9 | |||
10 | namespace pinocchio | ||
11 | { | ||
12 | namespace internal | ||
13 | { | ||
14 | |||
15 | enum ComparisonOperators | ||
16 | { | ||
17 | LT, | ||
18 | LE, | ||
19 | EQ, | ||
20 | GE, | ||
21 | GT | ||
22 | }; | ||
23 | |||
24 | template<typename LhsType, typename RhsType, typename ThenType, typename ElseType> | ||
25 | struct if_then_else_impl; | ||
26 | |||
27 | template<typename LhsType, typename RhsType> | ||
28 | struct comparison_eq_impl; | ||
29 | |||
30 | /// \brief Template specialization for similar return types; | ||
31 | template<typename LhsType, typename RhsType, typename return_type> | ||
32 | struct traits<if_then_else_impl<LhsType, RhsType, return_type, return_type>> | ||
33 | { | ||
34 | typedef return_type ReturnType; | ||
35 | }; | ||
36 | |||
37 | template<typename condition_type, typename ThenType, typename ElseType> | ||
38 | struct if_then_else_impl<condition_type, condition_type, ThenType, ElseType> | ||
39 | { | ||
40 | typedef typename internal::traits<if_then_else_impl>::ReturnType ReturnType; | ||
41 | |||
42 | 57444 | static inline ReturnType run( | |
43 | const ComparisonOperators op, | ||
44 | const condition_type & lhs_value, | ||
45 | const condition_type & rhs_value, | ||
46 | const ThenType & then_value, | ||
47 | const ElseType & else_value) | ||
48 | { | ||
49 |
4/6✗ Branch 0 not taken.
✓ Branch 1 taken 14818 times.
✓ Branch 2 taken 1381 times.
✓ Branch 3 taken 31533 times.
✓ Branch 4 taken 9712 times.
✗ Branch 5 not taken.
|
57444 | switch (op) |
50 | { | ||
51 | ✗ | case EQ: | |
52 | ✗ | if (lhs_value == rhs_value) | |
53 | ✗ | return then_value; | |
54 | else | ||
55 | ✗ | return else_value; | |
56 | break; | ||
57 | 14818 | case LT: | |
58 |
2/2✓ Branch 0 taken 4180 times.
✓ Branch 1 taken 10638 times.
|
14818 | if (lhs_value < rhs_value) |
59 | 4180 | return then_value; | |
60 | else | ||
61 | 10638 | return else_value; | |
62 | break; | ||
63 | 1381 | case LE: | |
64 |
2/2✓ Branch 0 taken 1252 times.
✓ Branch 1 taken 129 times.
|
1381 | if (lhs_value <= rhs_value) |
65 | 1252 | return then_value; | |
66 | else | ||
67 | 129 | return else_value; | |
68 | break; | ||
69 | 31533 | case GE: | |
70 |
2/2✓ Branch 0 taken 19886 times.
✓ Branch 1 taken 11647 times.
|
31533 | if (lhs_value >= rhs_value) |
71 | 19886 | return then_value; | |
72 | else | ||
73 | 11647 | return else_value; | |
74 | break; | ||
75 | 9712 | case GT: | |
76 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9712 times.
|
9712 | if (lhs_value > rhs_value) |
77 | ✗ | return then_value; | |
78 | else | ||
79 | 9712 | return else_value; | |
80 | break; | ||
81 | } | ||
82 | ✗ | PINOCCHIO_THROW_PRETTY( | |
83 | std::logic_error, "ComparisonOperators " << static_cast<int>(op) << " is not managed"); | ||
84 | } | ||
85 | }; | ||
86 | |||
87 | template<typename LhsType, typename RhsType, typename ThenType, typename ElseType> | ||
88 | inline typename if_then_else_impl<LhsType, RhsType, ThenType, ElseType>::ReturnType | ||
89 | 57444 | if_then_else( | |
90 | const ComparisonOperators op, | ||
91 | const LhsType & lhs_value, | ||
92 | const RhsType & rhs_value, | ||
93 | const ThenType & then_value, | ||
94 | const ElseType & else_value) | ||
95 | { | ||
96 | typedef if_then_else_impl<LhsType, RhsType, ThenType, ElseType> algo; | ||
97 | 57444 | return algo::run(op, lhs_value, rhs_value, then_value, else_value); | |
98 | } | ||
99 | |||
100 | // Generic | ||
101 | template<typename LhsType, typename RhsType> | ||
102 | struct comparison_eq_impl | ||
103 | { | ||
104 | ✗ | static inline bool run(const LhsType & lhs_value, const RhsType & rhs_value) | |
105 | { | ||
106 | ✗ | return lhs_value == rhs_value; | |
107 | } | ||
108 | }; | ||
109 | |||
110 | template<typename condition_type> | ||
111 | struct comparison_eq_impl<condition_type, condition_type> | ||
112 | { | ||
113 | 780 | static inline bool run(const condition_type & lhs_value, const condition_type & rhs_value) | |
114 | { | ||
115 | 780 | return lhs_value == rhs_value; | |
116 | } | ||
117 | }; | ||
118 | |||
119 | template<typename LhsType, typename RhsType> | ||
120 | 390 | inline bool comparison_eq(const LhsType & lhs_value, const RhsType & rhs_value) | |
121 | { | ||
122 | typedef comparison_eq_impl<LhsType, RhsType> algo; | ||
123 | 390 | return algo::run(lhs_value, rhs_value); | |
124 | } | ||
125 | } // namespace internal | ||
126 | } // namespace pinocchio | ||
127 | |||
128 | #endif | ||
129 |