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 41 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 89 of file StateToIdMap.ixx.

90 states_.reserve(STATE_TO_IDMAP_DEFAULT_CAPACITY);
91 };

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

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 101 of file StateToIdMap.ixx.

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

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

finalize()

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

Sorts and deduplicates all ID lists.

Definition at line 118 of file StateToIdMap.ixx.

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

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 138 of file StateToIdMap.ixx.

138 void freeze() {
139 finalize();
140 states_.shrink_to_fit();
141 frozen_ = true;
142 }

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

ids()

template <typename TState, typename TId>
const std::vector< TId > & helios::engine::state::StateToIdMap< TState, TId >::ids (const TState state)
inline nodiscard 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 151 of file StateToIdMap.ixx.

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

isFrozen()

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

Checks if the map is frozen.

Returns

True if frozen.

Definition at line 131 of file StateToIdMap.ixx.

131 [[nodiscard]] bool isFrozen() const noexcept {
132 return frozen_;
133 }

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 59 of file StateToIdMap.ixx.

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

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 51 of file StateToIdMap.ixx.

51 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 82 of file StateToIdMap.ixx.

82 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 46 of file StateToIdMap.ixx.

46 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.15.0.