Skip to main content

StateToIdMap Class Template

Maps state enum values to lists of IDs. More...

Declaration

template <typename TState, typename TId> class helios::engine::state::StateToIdMap<TState, TId> { ... }

Public Constructors Index

template <typename TState, typename TId>
StateToIdMap ()

Default constructor. More...

Public Member Functions Index

template <typename TState, typename TId>
StateToIdMap &add (TState state, TId id) noexcept

Adds an ID for a given state. More...

template <typename TState, typename TId>
voidfinalize ()

Sorts and deduplicates all ID lists. More...

template <typename TState, typename TId>
boolisFrozen () const noexcept

Checks if the map is frozen. More...

template <typename TState, typename TId>
voidfreeze ()

Finalizes and prevents further modifications. More...

template <typename TState, typename TId>
auto ids (const TState state) const noexcept -> const std::vector< TId > &

Returns IDs for a single state. More...

Private Member Functions Index

template <typename TState, typename TId>
voidupdate (size_t st, const TId id)

Updates internal storage for a state-ID association. More...

Private Member Attributes Index

template <typename TState, typename TId>
std::vector< std::vector< TId > >states_

ID lists indexed by state bit position. More...

template <typename TState, typename TId>
const std::vector< TId >empty_

Empty vector returned for invalid lookups. More...

template <typename TState, typename TId>
boolfrozen_ = false

Whether the map is frozen. More...

Description

Maps state enum values to lists of IDs.

Provides a lookup table that associates each bit position of a state enum with a list of IDs. States must be power-of-two values (single bits). Supports finalization and freezing for runtime optimization.

Template Parameters
TState

The state enum type (e.g., GameState, MatchState).

TId

The ID type to associate with states.

Definition at line 38 of file StateToIdMap.ixx.

Public Constructors

StateToIdMap()

template <typename TState, typename TId>
helios::engine::state::StateToIdMap< TState, TId >::StateToIdMap ()
inline

Default constructor.

Definition at line 86 of file StateToIdMap.ixx.

87 states_.reserve(STATE_TO_IDMAP_DEFAULT_CAPACITY);
88 };

Reference STATE_TO_IDMAP_DEFAULT_CAPACITY.

Public Member Functions

add()

template <typename TState, typename TId>
StateToIdMap & helios::engine::state::StateToIdMap< TState, TId >::add (TState state, TId id)
inline noexcept

Adds an ID for a given state.

Parameters
state

The state to associate the ID with.

id

The ID to add.

Returns

Reference to this map for chaining.

Definition at line 98 of file StateToIdMap.ixx.

98 StateToIdMap& add(TState state, TId id) noexcept {
99 assert(!frozen_ && "Cannot add to a frozen map");
100
101 const auto st = static_cast<size_t>(std::to_underlying(state));
102
103 if (st == 0) {
104 return *this;
105 }
106
107 update(st, id);
108
109 return *this;
110 }

Reference helios::registerComponents.

Referenced by helios::engine::state::StateToIdMapPair< LState, RState, TId >::add and helios::engine::state::StateToIdMapPair< LState, RState, TId >::add.

finalize()

template <typename TState, typename TId>
void helios::engine::state::StateToIdMap< TState, TId >::finalize ()
inline

Sorts and deduplicates all ID lists.

Definition at line 115 of file StateToIdMap.ixx.

115 void finalize() {
116 for (auto& v : states_) {
117 std::sort(v.begin(), v.end());
118 auto [first, last] = std::ranges::unique(v);
119 v.erase(first, last);
120 }
121 }

Reference helios::registerComponents.

Referenced by helios::engine::state::StateToIdMap< TState, TId >::freeze.

freeze()

template <typename TState, typename TId>
void helios::engine::state::StateToIdMap< TState, TId >::freeze ()
inline

Finalizes and prevents further modifications.

Definition at line 135 of file StateToIdMap.ixx.

135 void freeze() {
136 finalize();
137 states_.shrink_to_fit();
138 frozen_ = true;
139 }

Reference helios::engine::state::StateToIdMap< TState, TId >::finalize.

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

ids()

template <typename TState, typename TId>
const std::vector< TId > & helios::engine::state::StateToIdMap< TState, TId >::ids (const TState state)
inline noexcept

Returns IDs for a single state.

Parameters
state

The state to query (must be a power of 2).

Returns

Reference to the ID list for the state.

Definition at line 148 of file StateToIdMap.ixx.

148 [[nodiscard]] const std::vector<TId>& ids(const TState state) const noexcept {
149
150 if (std::to_underlying(state) == 0) {
151 return empty_;
152 }
153
154 assert((std::to_underlying(state) & (std::to_underlying(state) -1)) == 0 && "State must be a power of 2");
155
156 const auto stateIdx = static_cast<size_t>(std::countr_zero(std::to_underlying(state)));
157
158 if (states_.size() <= stateIdx) {
159 return empty_;
160 }
161
162 return states_[stateIdx];
163 }

Reference helios::registerComponents.

Referenced by helios::engine::state::StateToIdMapPair< LState, RState, TId >::ids.

isFrozen()

template <typename TState, typename TId>
bool helios::engine::state::StateToIdMap< TState, TId >::isFrozen ()
inline noexcept

Checks if the map is frozen.

Returns

True if frozen.

Definition at line 128 of file StateToIdMap.ixx.

129 return frozen_;
130 }

Referenced by helios::engine::state::StateToIdMapPair< LState, RState, TId >::ids.

Private Member Functions

update()

template <typename TState, typename TId>
void helios::engine::state::StateToIdMap< TState, TId >::update (size_t st, const TId id)
inline

Updates internal storage for a state-ID association.

Parameters
st

The state value as size_t.

id

The ID to associate.

Definition at line 56 of file StateToIdMap.ixx.

56 void update(size_t st, const TId id) {
57
58 size_t idx = 0;
59 while (st > 0) {
60
61 if ((st & 1) != 0) {
62 if (states_.size() <= idx) {
63 states_.resize(idx + 1);
64 }
65 if (std::ranges::find(states_[idx], id) == states_[idx].end()) {
66 states_[idx].push_back(id);
67 }
68 }
69
70 idx++;
71 st >>= 1;
72 }
73
74 }

Private Member Attributes

empty_

template <typename TState, typename TId>
const std::vector<TId> helios::engine::state::StateToIdMap< TState, TId >::empty_

Empty vector returned for invalid lookups.

Definition at line 48 of file StateToIdMap.ixx.

48 const std::vector<TId> empty_;

frozen_

template <typename TState, typename TId>
bool helios::engine::state::StateToIdMap< TState, TId >::frozen_ = false

Whether the map is frozen.

Definition at line 79 of file StateToIdMap.ixx.

79 bool frozen_ = false;

states_

template <typename TState, typename TId>
std::vector<std::vector<TId> > helios::engine::state::StateToIdMap< TState, TId >::states_

ID lists indexed by state bit position.

Definition at line 43 of file StateToIdMap.ixx.

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

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.