Skip to main content

CombinedStateToIdMapPair Class Template

Maps combined state pairs directly to ID lists. More...

Declaration

template <typename LState, typename RState, typename TId> class helios::engine::state::CombinedStateToIdMapPair<LState, RState, TId> { ... }

Public Member Functions Index

template <typename LState, typename RState, typename TId>
CombinedStateToIdMapPair &add (const LState stateLft, const RState stateRgt, const TId id) noexcept

Adds an ID for a specific state combination. More...

template <typename LState, typename RState, typename TId>
voidfinalize ()

Sorts and deduplicates all ID lists. More...

template <typename LState, typename RState, typename TId>
voidfreeze ()

Finalizes and prevents further modifications. More...

template <typename LState, typename RState, typename TId>
boolisFrozen () const noexcept

Checks if the map is frozen. More...

template <typename LState, typename RState, typename TId>
auto ids (const LState stateLft, const RState stateRgt) const noexcept -> std::span< const TId >

Returns IDs for a state combination. More...

Private Member Attributes Index

template <typename LState, typename RState, typename TId>
std::vector< std::vector< std::vector< TId > > >states_

2D matrix of ID lists indexed by [LState][RState]. More...

template <typename LState, typename RState, typename TId>
const std::vector< TId >empty_

Empty vector returned for invalid lookups. More...

template <typename LState, typename RState, typename TId>
boolfrozen_ = false

Whether the map is frozen. More...

Private Static Functions Index

template <typename LState, typename RState, typename TId>
static constexpr size_tindexOf (const LState stateLft)

Computes the index for a left state value. More...

template <typename LState, typename RState, typename TId>
static constexpr size_tindexOf (const RState stateRgt)

Computes the index for a right state value. More...

Description

Maps combined state pairs directly to ID lists.

Unlike StateToIdMapPair which stores IDs per state type and merges them at lookup time, this class uses a 2D matrix indexed by both state bit positions. This allows associating IDs with specific state combinations rather than individual states.

The right state (RState) supports an "undefined" value (0) which maps to index 0, allowing fallback behavior when only the left state is defined.

Template Parameters
LState

The left/primary state type.

RState

The right/secondary state type.

TId

The ID type to associate with state combinations.

See Also

StateToIdMapPair

Definition at line 51 of file CombinedStateToIdMapPair.ixx.

Public Member Functions

add()

template <typename LState, typename RState, typename TId>
CombinedStateToIdMapPair & helios::engine::state::CombinedStateToIdMapPair< LState, RState, TId >::add (const LState stateLft, const RState stateRgt, const TId id)
inline noexcept

Adds an ID for a specific state combination.

Parameters
stateLft

The left state (must be non-zero).

stateRgt

The right state (may be 0 for fallback).

id

The ID to associate with this combination.

Returns

Reference to this map for chaining.

Definition at line 117 of file CombinedStateToIdMapPair.ixx.

117 CombinedStateToIdMapPair& add(const LState stateLft, const RState stateRgt, const TId id) noexcept {
118
119 assert(!frozen_ && "Cannot add to a frozen map");
120
121 if (frozen_) {
122 return *this;
123 }
124
125 const auto stL = indexOf(stateLft);
126 const auto stR = indexOf(stateRgt);
127
128 if (states_.size() <= stL) {
129 states_.resize(stL + 1);
130 }
131 if (states_[stL].size() <= stR) {
132 states_[stL].resize(stR + 1);
133 }
134
135 states_[stL][stR].push_back(id);
136
137 return *this;
138 }

finalize()

template <typename LState, typename RState, typename TId>
void helios::engine::state::CombinedStateToIdMapPair< LState, RState, TId >::finalize ()
inline

Sorts and deduplicates all ID lists.

Definition at line 143 of file CombinedStateToIdMapPair.ixx.

143 void finalize() {
144 for (auto& statesL : states_) {
145 for (auto& statesR : statesL) {
146 std::sort(statesR.begin(), statesR.end());
147 auto [first, last] = std::ranges::unique(statesR);
148 statesR.erase(first, last);
149 }
150 }
151 }

Referenced by helios::engine::state::CombinedStateToIdMapPair< LState, RState, TId >::freeze.

freeze()

template <typename LState, typename RState, typename TId>
void helios::engine::state::CombinedStateToIdMapPair< LState, RState, TId >::freeze ()
inline

Finalizes and prevents further modifications.

Definition at line 156 of file CombinedStateToIdMapPair.ixx.

156 void freeze() {
157 finalize();
158 frozen_ = true;
159 }

Reference helios::engine::state::CombinedStateToIdMapPair< LState, RState, TId >::finalize.

ids()

template <typename LState, typename RState, typename TId>
std::span< const TId > helios::engine::state::CombinedStateToIdMapPair< LState, RState, TId >::ids (const LState stateLft, const RState stateRgt)
inline nodiscard noexcept

Returns IDs for a state combination.

If the specific combination has no IDs, falls back to the IDs registered for the left state with undefined right state.

Parameters
stateLft

The left state to query.

stateRgt

The right state to query.

Returns

Span of IDs for the state combination.

Definition at line 181 of file CombinedStateToIdMapPair.ixx.

181 [[nodiscard]] std::span<const TId> ids(const LState stateLft, const RState stateRgt) const noexcept {
182
183 assert(frozen_ && "Cannot return from a non-frozen map");
184
185 const auto stL = indexOf(stateLft);
186 const auto stR = indexOf(stateRgt);
187
188 if (states_.size() <= stL) {
189 return empty_;
190 }
191
192 if (states_[stL].size() <= stR || states_[stL][stR].empty()) {
193 if (!states_[stL].empty()) {
194 return states_[stL][0];
195 }
196 return empty_;
197 }
198
199 return states_[stL][stR];
200 }

isFrozen()

template <typename LState, typename RState, typename TId>
bool helios::engine::state::CombinedStateToIdMapPair< LState, RState, TId >::isFrozen ()
inline nodiscard noexcept

Checks if the map is frozen.

Returns

True if frozen.

Definition at line 166 of file CombinedStateToIdMapPair.ixx.

166 [[nodiscard]] bool isFrozen() const noexcept {
167 return frozen_;
168 }

Private Member Attributes

empty_

template <typename LState, typename RState, typename TId>
const std::vector<TId> helios::engine::state::CombinedStateToIdMapPair< LState, RState, TId >::empty_

Empty vector returned for invalid lookups.

Definition at line 61 of file CombinedStateToIdMapPair.ixx.

61 const std::vector<TId> empty_;

frozen_

template <typename LState, typename RState, typename TId>
bool helios::engine::state::CombinedStateToIdMapPair< LState, RState, TId >::frozen_ = false

Whether the map is frozen.

Definition at line 66 of file CombinedStateToIdMapPair.ixx.

66 bool frozen_ = false;

states_

template <typename LState, typename RState, typename TId>
std::vector<std::vector<std::vector<TId> > > helios::engine::state::CombinedStateToIdMapPair< LState, RState, TId >::states_

2D matrix of ID lists indexed by [LState][RState].

Definition at line 56 of file CombinedStateToIdMapPair.ixx.

56 std::vector<std::vector<std::vector<TId>>> states_;

Private Static Functions

indexOf()

template <typename LState, typename RState, typename TId>
constexpr size_t helios::engine::state::CombinedStateToIdMapPair< LState, RState, TId >::indexOf (const LState stateLft)
inline nodiscard constexpr static

Computes the index for a left state value.

Parameters
stateLft

The left state (must be non-zero, power of 2).

Returns

The bit position index.

Definition at line 75 of file CombinedStateToIdMapPair.ixx.

75 [[nodiscard]] static constexpr size_t indexOf(const LState stateLft) {
76 const auto stL = static_cast<size_t>(std::to_underlying(stateLft));
77 assert(stL != 0 && "LState must be defined");
78
79 assert((std::to_underlying(stateLft) & (std::to_underlying(stateLft) -1)) == 0 && "LState must be a power of 2");
80
81 return std::countr_zero(std::to_underlying(stateLft));
82 }

indexOf()

template <typename LState, typename RState, typename TId>
constexpr size_t helios::engine::state::CombinedStateToIdMapPair< LState, RState, TId >::indexOf (const RState stateRgt)
inline nodiscard constexpr static

Computes the index for a right state value.

Supports undefined (0) state which maps to index 0. Non-zero states map to their bit position + 1.

Parameters
stateRgt

The right state (may be 0, otherwise power of 2).

Returns

The index (0 for undefined, bit position + 1 otherwise).

Definition at line 94 of file CombinedStateToIdMapPair.ixx.

94 [[nodiscard]] static constexpr size_t indexOf(const RState stateRgt) {
95
96 if (std::to_underlying(stateRgt) != 0 ) {
97 assert((std::to_underlying(stateRgt) & (std::to_underlying(stateRgt) -1)) == 0 && "RState must be a power of 2");
98 // undefined should map to 0, so 1 << 0 (bit count = 0) must map to 1
99 // (countr_zero counts the number of zeroes beginning at the lsb)
100 return std::countr_zero(std::to_underlying(stateRgt)) + 1;
101 }
102
103 return 0;
104 }

The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.