Skip to main content

StateToIdMapPair.ixx File

Combined policy mapping two state types to IDs. More...

Included Headers

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

Namespaces Index

namespacehelios
namespaceengine

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

namespacestate

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

Classes Index

classStateToIdMapPair<LState, RState, TId>

Combines two StateToIdMap instances for dual-state lookups. More...

Description

Combined policy mapping two state types to IDs.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file StateToIdMapPair.ixx
3 * @brief Combined policy mapping two state types 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#include <iterator>
14
15export module helios.engine.state.StateToIdMapPair;
16
17import helios.engine.state.StateToIdMap;
18
19import helios.engine.state.types;
20import helios.engine.mechanics.match.types;
21
22import helios.engine.common.types.ViewportId;
23import helios.core.types;
24
26using namespace helios::engine::state::types;
28
29export namespace helios::engine::state {
30
31 /**
32 * @brief Combines two StateToIdMap instances for dual-state lookups.
33 *
34 * @details Allows associating IDs with two different state types
35 * (e.g., GameState and MatchState). When queried, returns the
36 * union of IDs from both maps for the given state combination.
37 *
38 * @tparam LState The left/primary state type.
39 * @tparam RState The right/secondary state type.
40 * @tparam TId The ID type to associate with states.
41 */
42 template<typename LState, typename RState, typename TId>
44
45 /**
46 * @brief Map for the left state type.
47 */
49
50 /**
51 * @brief Map for the right state type.
52 */
54
55 /**
56 * @brief Buffer for merged results.
57 */
58 mutable std::vector<TId> combined_;
59
60 /**
61 * @brief Merges two sorted ID lists into the output.
62 *
63 * @param out The output vector.
64 * @param lft IDs from the left map.
65 * @param rgt IDs from the right map.
66 */
67 void mergeInto(std::vector<TId>& out, std::span<const TId> lft, std::span<const TId> rgt) const {
68
69 out.clear();
70
71 out.reserve(lft.size() + rgt.size());
72
73 std::set_union(lft.begin(), lft.end(), rgt.begin(), rgt.end(), std::back_inserter(out));
74 }
75
76 public:
77
78 /**
79 * @brief Adds an ID for the left state type.
80 *
81 * @param state The state to associate the ID with.
82 * @param id The ID to add.
83 *
84 * @return Reference to this map for chaining.
85 */
86 StateToIdMapPair& add(LState state, TId id) {
87 lft_.add(state, id);
88 return *this;
89 }
90
91 /**
92 * @brief Adds an ID for the right state type.
93 *
94 * @param state The state to associate the ID with.
95 * @param id The ID to add.
96 *
97 * @return Reference to this map for chaining.
98 */
99 StateToIdMapPair& add(RState state, TId id) {
100 rgt_.add(state, id);
101 return *this;
102 }
103
104 /**
105 * @brief Returns IDs for a state combination.
106 *
107 * @details Returns the sorted union of IDs from both maps.
108 * Both maps must be frozen before calling this method.
109 *
110 * @param stateLft The left state to query.
111 * @param stateRgt The right state to query.
112 *
113 * @return Span of IDs for the state combination.
114 */
115 [[nodiscard]] std::span<const TId> ids(const LState stateLft, const RState stateRgt) const {
116
117 assert(lft_.isFrozen() && rgt_.isFrozen() && "Cannot merge if sources are not frozen.");
118
119 if (!lft_.isFrozen() || !rgt_.isFrozen()) {
120 return {};
121 }
122
123 mergeInto(combined_, lft_.ids(stateLft), rgt_.ids(stateRgt));
124
125 return combined_;
126 }
127
128 /**
129 * @brief Freezes both underlying maps.
130 */
131 void freeze() {
132 lft_.freeze();
133 rgt_.freeze();
134 }
135
136 };
137
138
139}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.