| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \file CircularIndex.hpp | ||
| 3 | // \author bocchino | ||
| 4 | // \brief An index value that wraps around modulo an integer | ||
| 5 | // ====================================================================== | ||
| 6 | |||
| 7 | #ifndef Fw_CircularIndex_HPP | ||
| 8 | #define Fw_CircularIndex_HPP | ||
| 9 | |||
| 10 | #include "Fw/FPrimeBasicTypes.hpp" | ||
| 11 | #include "Fw/Types/Assert.hpp" | ||
| 12 | |||
| 13 | namespace Fw { | ||
| 14 | |||
| 15 | class CircularIndex final { | ||
| 16 | public: | ||
| 17 | // ---------------------------------------------------------------------- | ||
| 18 | // Public constructors and destructors | ||
| 19 | // ---------------------------------------------------------------------- | ||
| 20 | |||
| 21 | //! Zero-argument constructor | ||
| 22 | 74 | CircularIndex() : m_value(0), m_modulus(1) {} | |
| 23 | |||
| 24 | //! Constructor with specified members | ||
| 25 | 9 | explicit CircularIndex(FwSizeType modulus, //!< The modulus | |
| 26 | FwSizeType value = 0 //!< The initial value | ||
| 27 | ) | ||
| 28 | 9 | : m_modulus(modulus) { | |
| 29 | 9 | FW_ASSERT(modulus > 0); | |
| 30 | 9 | this->setValue(value); | |
| 31 | 9 | } | |
| 32 | |||
| 33 | //! Copy constructor | ||
| 34 | 13019 | CircularIndex(const CircularIndex& ci) { *this = ci; } | |
| 35 | |||
| 36 | //! Destructor | ||
| 37 | ~CircularIndex() = default; | ||
| 38 | |||
| 39 | public: | ||
| 40 | // ---------------------------------------------------------------------- | ||
| 41 | // Public functions | ||
| 42 | // ---------------------------------------------------------------------- | ||
| 43 | |||
| 44 | //! operator= | ||
| 45 | 13024 | CircularIndex& operator=(const CircularIndex& ci) { | |
| 46 |
1/2✓ Branch 0 taken 13024 times.
✗ Branch 1 not taken.
|
13024 | if (this != &ci) { |
| 47 | 13024 | this->m_value = ci.m_value; | |
| 48 | 13024 | this->m_modulus = ci.m_modulus; | |
| 49 | } | ||
| 50 | 13024 | return *this; | |
| 51 | } | ||
| 52 | |||
| 53 | //! Get the index value | ||
| 54 | //! \return The index value | ||
| 55 | 11923 | FwSizeType getValue() const { | |
| 56 | 11923 | FW_ASSERT(this->m_value < this->m_modulus); | |
| 57 | 11923 | return this->m_value; | |
| 58 | } | ||
| 59 | |||
| 60 | //! Set the index value | ||
| 61 | 25847 | void setValue(FwSizeType value //!< The index value | |
| 62 | ) { | ||
| 63 | 25847 | FW_ASSERT(this->m_modulus > 0); | |
| 64 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 25847 times.
|
25847 | this->m_value = value % this->m_modulus; |
| 65 | 25847 | } | |
| 66 | |||
| 67 | //! Get the modulus | ||
| 68 | 17 | FwSizeType getModulus() const { | |
| 69 | 17 | FW_ASSERT(this->m_value < this->m_modulus); | |
| 70 | 17 | return this->m_modulus; | |
| 71 | } | ||
| 72 | |||
| 73 | //! Set the modulus | ||
| 74 | 67 | void setModulus(FwSizeType modulus //!< The modulus value | |
| 75 | ) { | ||
| 76 | 67 | this->m_modulus = modulus; | |
| 77 | 67 | this->setValue(this->m_value); | |
| 78 | 67 | } | |
| 79 | |||
| 80 | //! Increment the index value | ||
| 81 | //! \return The new value | ||
| 82 | 25186 | FwSizeType increment(FwSizeType amount = 1 //!< The amount by which to increment | |
| 83 | ) { | ||
| 84 | 25186 | FW_ASSERT(this->m_modulus > 0); | |
| 85 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 25186 times.
|
25186 | const FwSizeType offset = amount % m_modulus; |
| 86 | // The distance to the wrap point, in (0, m_modulus]. Adding the offset without this | ||
| 87 | // comparison could overflow FwSizeType before the modular reduction is applied. | ||
| 88 | 25186 | const FwSizeType toWrap = this->m_modulus - this->m_value; | |
| 89 |
2/2✓ Branch 1 taken 25175 times.
✓ Branch 2 taken 11 times.
|
25186 | this->setValue((offset < toWrap) ? (this->m_value + offset) : (offset - toWrap)); |
| 90 | 25186 | return this->m_value; | |
| 91 | } | ||
| 92 | |||
| 93 | //! Decrement the index value | ||
| 94 | //! \return The new value | ||
| 95 | 2 | FwSizeType decrement(FwSizeType amount = 1 //!< The amount by which to decrement | |
| 96 | ) { | ||
| 97 | 2 | FW_ASSERT(this->m_modulus > 0); | |
| 98 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | const FwSizeType offset = amount % this->m_modulus; |
| 99 | // Subtracting first where possible avoids overflowing FwSizeType on m_value + m_modulus | ||
| 100 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
|
3 | this->setValue((offset <= this->m_value) ? (this->m_value - offset) |
| 101 | 1 | : (this->m_modulus - (offset - this->m_value))); | |
| 102 | 2 | return this->m_value; | |
| 103 | } | ||
| 104 | |||
| 105 | private: | ||
| 106 | // ---------------------------------------------------------------------- | ||
| 107 | // Private member variables | ||
| 108 | // ---------------------------------------------------------------------- | ||
| 109 | |||
| 110 | //! The index value | ||
| 111 | FwSizeType m_value; | ||
| 112 | |||
| 113 | //! The modulus | ||
| 114 | FwSizeType m_modulus; | ||
| 115 | }; | ||
| 116 | |||
| 117 | } // namespace Fw | ||
| 118 | |||
| 119 | #endif | ||
| 120 |