R-Type  2
Doom but in better
Loading...
Searching...
No Matches
DenseArray.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <optional>
5#include <memory>
6#include <cassert>
7#include <algorithm>
8
19template <typename Component, typename Allocator = std::allocator<Component>>
21public:
23 using value_type = Component;
24 using optional_t = std::optional<Component>;
27 using id_type = std::size_t;
28 using id_container_t = std::vector<id_type>;
29 using component_container_t = std::vector<optional_t, Allocator>;
30 using size_type = typename component_container_t::size_type;
31 using iterator = typename component_container_t::iterator;
32 using const_iterator = typename component_container_t::const_iterator;
37 DenseArray() = default;
38
44 DenseArray(const DenseArray& other) = default;
45
51 DenseArray(DenseArray&& other) noexcept = default;
52
56 ~DenseArray() = default;
57
64 DenseArray& operator=(const DenseArray& other) = default;
65
72 DenseArray& operator=(DenseArray&& other) noexcept = default;
73
81 assert(idx < _components.size() && "Index out of bounds");
82 return _components[idx];
83 }
84
92 return _components[idx];
93 }
94
101 return _components.begin();
102 }
103
110 return _components.begin();
111 }
112
119 return _components.cbegin();
120 }
121
128 return _components.end();
129 }
130
137 return _components.end();
138 }
139
146 return _components.cend();
147 }
148
154 size_type size() const {
155 return _components.size();
156 }
157
164 void insert_at(size_type id, const Component& component) {
165 ensure_size(id);
166 _ids.push_back(id);
167 _components.push_back(component);
168 }
169
176 void insert_at(size_type id, Component&& component) {
177 ensure_size(id);
178 _ids.push_back(id);
179 _components[id] = std::move(component);
180 }
181
189 template <typename... Params>
190 void emplace_at(size_type id, Params&&... params) {
191 ensure_size(id);
192 _ids.push_back(id);
193 _components.emplace_back(std::forward<Params>(params)...);
194 }
195
201 void erase(size_type id) {
202 _ids.erase(_ids.begin() + id);
203 _components.erase(_components.begin() + id);
204 }
205
206private:
212 void ensure_size(size_type pos) {
213 if (pos >= _components.size()) {
214 _ids.resize(pos + 1);
215 _components.resize(pos + 1);
216 }
217 }
218
219 id_container_t _ids;
220 component_container_t _components;
221 Allocator _allocator;
222};
A container that provides dense storage for components with a mapping between entity IDs and componen...
void insert_at(size_type id, const Component &component)
Inserts a component at a specific ID.
DenseArray(const DenseArray &other)=default
Copy constructor.
DenseArray()=default
Default constructor.
const_iterator end() const
Returns a const iterator to the end of the component container.
typename component_container_t::iterator iterator
Component value_type
Type aliases for convenience.
std::size_t id_type
iterator end()
Returns an iterator to the end of the component container.
iterator begin()
Returns an iterator to the beginning of the component container.
size_type size() const
Returns the number of components stored in the array.
DenseArray(DenseArray &&other) noexcept=default
Move constructor.
reference_type operator[](size_type idx)
Accesses the component at the given index.
std::vector< optional_t, Allocator > component_container_t
const optional_t & const_reference_type
optional_t & reference_type
DenseArray & operator=(const DenseArray &other)=default
Copy assignment operator.
void emplace_at(size_type id, Params &&... params)
Constructs and inserts a component at a specific ID.
typename component_container_t::const_iterator const_iterator
std::optional< Component > optional_t
void insert_at(size_type id, Component &&component)
Inserts a component at a specific ID (move version).
const_iterator cend() const
Returns a const iterator to the end of the component container.
DenseArray & operator=(DenseArray &&other) noexcept=default
Move assignment operator.
void erase(size_type id)
Removes the component at a specific ID.
typename component_container_t::size_type size_type
const_iterator cbegin() const
Returns a const iterator to the beginning of the component container.
std::vector< id_type > id_container_t
const_iterator begin() const
Returns a const iterator to the beginning of the component container.
const_reference_type operator[](size_type idx) const
Accesses the component at the given index (const version).
~DenseArray()=default
Destructor.