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 50 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 116 of file CombinedStateToIdMapPair.ixx.

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

Reference helios::registerComponents.

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 142 of file CombinedStateToIdMapPair.ixx.

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

Reference helios::registerComponents.

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 155 of file CombinedStateToIdMapPair.ixx.

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

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 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 180 of file CombinedStateToIdMapPair.ixx.

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

Reference helios::registerComponents.

isFrozen()

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

Checks if the map is frozen.

Returns

True if frozen.

Definition at line 165 of file CombinedStateToIdMapPair.ixx.

166 return frozen_;
167 }

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 60 of file CombinedStateToIdMapPair.ixx.

60 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 65 of file CombinedStateToIdMapPair.ixx.

65 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 55 of file CombinedStateToIdMapPair.ixx.

55 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 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 74 of file CombinedStateToIdMapPair.ixx.

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

indexOf()

template <typename LState, typename RState, typename TId>
constexpr size_t helios::engine::state::CombinedStateToIdMapPair< LState, RState, TId >::indexOf (const RState stateRgt)
inline 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 93 of file CombinedStateToIdMapPair.ixx.

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

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.