Skip to main content

StateToIdMap.ixx File

Policy mapping states to IDs. More...

Included Headers

#include <vector> #include <span> #include <helios/helios_config.h> #include <cassert> #include <bit> #include <algorithm> #include <helios.core.types> #include <helios.engine.common.types.ViewportId> #include <helios.engine.mechanics.match.types> #include <helios.engine.state.types>

Namespaces Index

namespacehelios
namespaceengine

Main engine module aggregating core infrastructure and game systems. More...

namespacestate

Generic, template-based state management system. More...

Classes Index

classStateToIdMap<TState, TId>

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

Description

Policy mapping states to IDs.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file StateToIdMap.ixx
3 * @brief Policy mapping states to IDs.
4 */
5module;
6
7#include <vector>
8#include <span>
9#include <helios/helios_config.h>
10#include <cassert>
11#include <bit>
12#include <algorithm>
13
14export module helios.engine.state.StateToIdMap;
15
16
17import helios.engine.state.types;
18import helios.engine.mechanics.match.types;
19
20import helios.engine.common.types.ViewportId;
21import helios.core.types;
22
24using namespace helios::engine::state::types;
26
27export namespace helios::engine::state {
28
29 /**
30 * @brief Maps state enum values to lists of IDs.
31 *
32 * @details Provides a lookup table that associates each bit position
33 * of a state enum with a list of IDs. States must be power-of-two
34 * values (single bits). Supports finalization and freezing for
35 * runtime optimization.
36 *
37 * @tparam TState The state enum type (e.g., GameState, MatchState).
38 * @tparam TId The ID type to associate with states.
39 */
40 template<typename TState, typename TId>
42
43 /**
44 * @brief ID lists indexed by state bit position.
45 */
46 std::vector<std::vector<TId>> states_;
47
48 /**
49 * @brief Empty vector returned for invalid lookups.
50 */
51 const std::vector<TId> empty_;
52
53 /**
54 * @brief Updates internal storage for a state-ID association.
55 *
56 * @param st The state value as size_t.
57 * @param id The ID to associate.
58 */
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 }
78
79 /**
80 * @brief Whether the map is frozen.
81 */
82 bool frozen_ = false;
83
84 public:
85
86 /**
87 * @brief Default constructor.
88 */
90 states_.reserve(STATE_TO_IDMAP_DEFAULT_CAPACITY);
91 };
92
93 /**
94 * @brief Adds an ID for a given state.
95 *
96 * @param state The state to associate the ID with.
97 * @param id The ID to add.
98 *
99 * @return Reference to this map for chaining.
100 */
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 }
114
115 /**
116 * @brief Sorts and deduplicates all ID lists.
117 */
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 }
125
126 /**
127 * @brief Checks if the map is frozen.
128 *
129 * @return True if frozen.
130 */
131 [[nodiscard]] bool isFrozen() const noexcept {
132 return frozen_;
133 }
134
135 /**
136 * @brief Finalizes and prevents further modifications.
137 */
138 void freeze() {
139 finalize();
140 states_.shrink_to_fit();
141 frozen_ = true;
142 }
143
144 /**
145 * @brief Returns IDs for a single state.
146 *
147 * @param state The state to query (must be a power of 2).
148 *
149 * @return Reference to the ID list for the state.
150 */
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 }
167
168
169 };
170
171
172}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.