ecs
Entity-Component-System
Loading...
Searching...
No Matches
SparseArray.hpp
Go to the documentation of this file.
1
11#ifndef SPARSE_ARRAY_HPP
12#define SPARSE_ARRAY_HPP
13
14//local
15
16//global
17#include <iostream>
18#include <vector>
19#include <optional>
20
21namespace ecs {
22
31 template <typename Component>
32 std::ostream& operator<<(std::ostream& os, std::optional<Component> const &c)
33 {
34 if (c)
35 os << c.value();
36 else
37 os << "nullopt";
38 return os;
39 }
40
47 template <typename Component>
49
50 public:
51
52 using value_type = std::optional<Component>;
55
56 using container_t = std::vector<value_type>;
57
58 using size_type = typename container_t::size_type;
59
60 using iterator = typename container_t::iterator;
61 using const_iterator = typename container_t::const_iterator;
62
63 public:
64
69 SparseArray() = default;
70
76 SparseArray(SparseArray const &) = default;
77
83 SparseArray(SparseArray &&) noexcept = default;
84
88 ~SparseArray() = default;
89
96 SparseArray &operator=(SparseArray const &) = default;
97
104 SparseArray &operator=(SparseArray &&) noexcept = default;
105
112 inline reference_type operator[](size_t idx) { return _data[idx]; }
113
120 const_reference_type operator[](size_t idx) const { return _data.at(idx); }
121
127 inline iterator begin() { return _data.begin(); }
128
134 inline const_iterator begin() const { return _data.begin(); }
135
141 inline const_iterator cbegin() const { return _data.cbegin(); }
142
148 inline iterator end() { return _data.end(); }
149
155 inline const_iterator end() const { return _data.end(); }
156
162 inline const_iterator cend() const { return _data.cend(); }
163
169 inline size_type size() const { return _data.size(); }
170
180 if (_data.size() <= pos)
181 _data.resize(pos + 1);
182 _data[pos] = std::make_optional(c);
183 return _data[pos];
184 };
185
194 if (_data.size() <= pos)
195 _data.resize(pos + 1);
196 _data[pos] = std::make_optional(std::move(c));
197 return _data[pos];
198 };
199
208 template <class ... Params>
209 reference_type emplaceAt(size_type pos, Params &&...args) {
210 container_t vecArgs = {args...};
211
212 size_type i = vecArgs.size();
213 if (_data.size() <= pos + i)
214 _data.resize(pos + i);
215 i = 0;
216 for (value_type v : vecArgs) {
217 _data[pos + i] = v;
218 ++i;
219 }
220 return _data[pos];
221 };
222
228 void erase(size_type pos) {
229 if (pos >= 0 && pos < _data.size()) {
230 _data[pos] = std::nullopt;
231 }
232 };
233
240 size_type getIndex(const value_type &v) const {
241 auto it = std::find(_data.begin(), _data.end(), v);
242 if (it != _data.end())
243 return std::distance(_data.begin(), it);
244 return -1; // return 0 if not found
245 };
246
250 void print() const {
251 bool first = true;
252 std::cout << "{ ";
253 for (auto const &v : _data) {
254 if (!first) {
255 std::cout << ", " << v;
256 } else {
257 std::cout << v;
258 first = false;
259 }
260 }
261 std::cout << " }" << std::endl;
262 };
263
264 private:
265
266 container_t _data;
267 };
268
269} // namespace name
270
271
274#endif // SPARSE_ARRAY_HPP
Component is a template define in Registry. (not a class)
SparseArray class.
Definition SparseArray.hpp:48
const_iterator begin() const
give a const_iterator at the begining of the SparseArray
Definition SparseArray.hpp:134
reference_type insertAt(size_type pos, Component const &c)
insert a component at the given position & return a reference to it
Definition SparseArray.hpp:179
typename container_t::size_type size_type
Definition SparseArray.hpp:58
void erase(size_type pos)
erase the component at the given position
Definition SparseArray.hpp:228
SparseArray()=default
construct a new SparseArray object
reference_type insertAt(size_type pos, Component &&c)
insert a component at the given position & return a reference to it
Definition SparseArray.hpp:193
const_iterator cbegin() const
give a const_iterator at the begining of the SparseArray
Definition SparseArray.hpp:141
const_iterator end() const
give a const_iterator at the end of the SparseArray
Definition SparseArray.hpp:155
iterator begin()
give a iterator at the begining of the SparseArray
Definition SparseArray.hpp:127
const_iterator cend() const
give a const_iterator at the end of the SparseArray
Definition SparseArray.hpp:162
value_type const & const_reference_type
Definition SparseArray.hpp:54
size_type size() const
give the size of the SparseArray
Definition SparseArray.hpp:169
reference_type emplaceAt(size_type pos, Params &&...args)
emplace a component at the given position & return a reference to it
Definition SparseArray.hpp:209
void print() const
print the SparseArray
Definition SparseArray.hpp:250
SparseArray(SparseArray &&) noexcept=default
move constructor
std::vector< value_type > container_t
Definition SparseArray.hpp:56
const_reference_type operator[](size_t idx) const
give the value at the index idx of the SparseArray (const)
Definition SparseArray.hpp:120
size_type getIndex(const value_type &v) const
get the index of the given value
Definition SparseArray.hpp:240
typename container_t::iterator iterator
Definition SparseArray.hpp:60
typename container_t::const_iterator const_iterator
Definition SparseArray.hpp:61
iterator end()
give a iterator at the end of the SparseArray
Definition SparseArray.hpp:148
value_type & reference_type
Definition SparseArray.hpp:53
SparseArray(SparseArray const &)=default
copy constructor
std::optional< Component > value_type
Definition SparseArray.hpp:52
Entity Component System.
std::ostream & operator<<(std::ostream &os, std::optional< Component > const &c)
help to print the optional value
Definition SparseArray.hpp:32