| Directory: | ./ |
|---|---|
| File: | include/pinocchio/utils/static-if.hpp |
| Date: | 2025-02-12 21:03:38 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 26 | 31 | 83.9% |
| Branches: | 12 | 50 | 24.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 | 958908 | 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 350092 times.
✓ Branch 2 taken 24350 times.
✓ Branch 3 taken 440328 times.
✓ Branch 4 taken 144138 times.
✗ Branch 5 not taken.
|
958908 | 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 | 350092 | case LT: | |
| 58 |
2/2✓ Branch 0 taken 133568 times.
✓ Branch 1 taken 216524 times.
|
350092 | if (lhs_value < rhs_value) |
| 59 | 133568 | return then_value; | |
| 60 | else | ||
| 61 | 216524 | return else_value; | |
| 62 | break; | ||
| 63 | 24350 | case LE: | |
| 64 |
2/2✓ Branch 0 taken 5171 times.
✓ Branch 1 taken 19179 times.
|
24350 | if (lhs_value <= rhs_value) |
| 65 | 5171 | return then_value; | |
| 66 | else | ||
| 67 | 19179 | return else_value; | |
| 68 | break; | ||
| 69 | 440328 | case GE: | |
| 70 |
2/2✓ Branch 0 taken 372536 times.
✓ Branch 1 taken 67792 times.
|
440328 | if (lhs_value >= rhs_value) |
| 71 | 372536 | return then_value; | |
| 72 | else | ||
| 73 | 67792 | return else_value; | |
| 74 | break; | ||
| 75 | 144138 | case GT: | |
| 76 |
2/2✓ Branch 0 taken 84494 times.
✓ Branch 1 taken 59644 times.
|
144138 | if (lhs_value > rhs_value) |
| 77 | 84494 | return then_value; | |
| 78 | else | ||
| 79 | 59644 | 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 | 963134 | 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 | 963134 | 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 | 10 | static inline bool run(const LhsType & lhs_value, const RhsType & rhs_value) | |
| 105 | { | ||
| 106 | 10 | return lhs_value == rhs_value; | |
| 107 | } | ||
| 108 | }; | ||
| 109 | |||
| 110 | template<typename condition_type> | ||
| 111 | struct comparison_eq_impl<condition_type, condition_type> | ||
| 112 | { | ||
| 113 | 48176 | static inline bool run(const condition_type & lhs_value, const condition_type & rhs_value) | |
| 114 | { | ||
| 115 | 48176 | return lhs_value == rhs_value; | |
| 116 | } | ||
| 117 | }; | ||
| 118 | |||
| 119 | template<typename LhsType, typename RhsType> | ||
| 120 | 24093 | inline bool comparison_eq(const LhsType & lhs_value, const RhsType & rhs_value) | |
| 121 | { | ||
| 122 | typedef comparison_eq_impl<LhsType, RhsType> algo; | ||
| 123 | 24093 | return algo::run(lhs_value, rhs_value); | |
| 124 | } | ||
| 125 | } // namespace internal | ||
| 126 | } // namespace pinocchio | ||
| 127 | |||
| 128 | #endif | ||
| 129 |