R-Type  2
Doom but in better
Loading...
Searching...
No Matches
SparseArray.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
18template <typename Component, typename Allocator = std::allocator<Component>>
20public:
22 using value_type = std::optional<Component>;
25 using container_t = std::vector<value_type, Allocator>;
26 using size_type = typename container_t::size_type;
27 using iterator = typename container_t::iterator;
28 using const_iterator = typename container_t::const_iterator;
33 SparseArray() = default;
34
40 SparseArray(const SparseArray& other) = default;
41
47 SparseArray(SparseArray&& other) noexcept = default;
48
52 ~SparseArray() = default;
53
60 SparseArray& operator=(const SparseArray& other) = default;
61
68 SparseArray& operator=(SparseArray&& other) noexcept = default;
69
76 reference_type operator[](std::size_t idx) {
77 assert(idx < _data.size() && "Index out of bounds");
78 return _data[idx];
79 }
80
87 const_reference_type operator[](std::size_t idx) const {
88 assert(idx < _data.size() && "Index out of bounds");
89 return _data[idx];
90 }
91
98 return _data.begin();
99 }
100
107 return _data.begin();
108 }
109
116 return _data.cbegin();
117 }
118
125 return _data.end();
126 }
127
134 return _data.end();
135 }
136
143 return _data.cend();
144 }
145
151 size_type size() const {
152 return _data.size();
153 }
154
162 reference_type insert_at(size_type pos, const Component& component) {
163 ensure_size(pos);
164 _data[pos] = component;
165 return _data[pos];
166 }
167
175 reference_type insert_at(size_type pos, Component&& component) {
176 ensure_size(pos);
177 _data[pos] = std::move(component);
178 return _data[pos];
179 }
180
189 template <class... Params>
190 reference_type emplace_at(size_type pos, Params&&... params) {
191 ensure_size(pos);
192 _data[pos].reset();
193 std::allocator_traits<Allocator>::construct(
194 _allocator, std::addressof(_data[pos]), std::forward<Params>(params)...);
195 return _data[pos];
196 }
197
203 void erase(size_type pos) {
204 if (pos < _data.size()) {
205 _data[pos].reset();
206 }
207 }
208
209private:
215 void ensure_size(size_type pos) {
216 if (pos >= _data.size()) {
217 _data.resize(pos + 1);
218 }
219 }
220
221 container_t _data;
222 Allocator _allocator;
223};
A container that provides sparse storage for optional components.
reference_type insert_at(size_type pos, const Component &component)
Inserts a component at a specific position.
SparseArray()=default
Default constructor.
value_type & reference_type
iterator begin()
Returns an iterator to the beginning of the array.
std::optional< Component > value_type
Type aliases for convenience.
std::vector< value_type, Allocator > container_t
SparseArray & operator=(SparseArray &&other) noexcept=default
Move assignment operator.
const_iterator begin() const
Returns a const iterator to the beginning of the array.
const_iterator cend() const
Returns a const iterator to the end of the array.
reference_type insert_at(size_type pos, Component &&component)
Inserts a component at a specific position (move version).
typename container_t::size_type size_type
SparseArray(const SparseArray &other)=default
Copy constructor.
const_reference_type operator[](std::size_t idx) const
Accesses the value at the given index (const version).
typename container_t::iterator iterator
const_iterator end() const
Returns a const iterator to the end of the array.
iterator end()
Returns an iterator to the end of the array.
const value_type & const_reference_type
SparseArray(SparseArray &&other) noexcept=default
Move constructor.
SparseArray & operator=(const SparseArray &other)=default
Copy assignment operator.
~SparseArray()=default
Destructor.
void erase(size_type pos)
Removes the component at a specific position.
size_type size() const
Returns the size of the array.
reference_type operator[](std::size_t idx)
Accesses the value at the given index.
reference_type emplace_at(size_type pos, Params &&... params)
Constructs and inserts a component at a specific position.
typename container_t::const_iterator const_iterator
const_iterator cbegin() const
Returns a const iterator to the beginning of the array.