ecs
Loading...
Searching...
No Matches
SparseArray.hpp
Go to the documentation of this file.
1
8#ifndef SPARSE_ARRAY_HPP
9#define SPARSE_ARRAY_HPP
10
11//local
12
13//global
14#include <iostream>
15#include <vector>
16#include <optional>
17
18namespace ecs {
19
28 template <typename Component>
29 std::ostream& operator<<(std::ostream& os, std::optional<Component> const &c)
30 {
31 if (c)
32 os << c.value();
33 else
34 os << "nullopt";
35 return os;
36 }
37
44 template <typename Component>
46
47 public:
48
49 using value_type = std::optional<Component>;
52
53 using container_t = std::vector<value_type>;
54
55 using size_type = typename container_t::size_type;
56
57 using iterator = typename container_t::iterator;
58 using const_iterator = typename container_t::const_iterator;
59
60 public:
61
66 SparseArray() = default;
67
73 SparseArray(SparseArray const &) = default;
74
80 SparseArray(SparseArray &&) noexcept = default;
81
85 ~SparseArray() = default;
86
93 SparseArray &operator=(SparseArray const &) = default;
94
101 SparseArray &operator=(SparseArray &&) noexcept = default;
102
109 inline reference_type operator[](size_t idx) { return _data[idx]; }
110
117 const_reference_type operator[](size_t idx) const { return _data.at(idx); }
118
124 inline iterator begin() { return _data.begin(); }
125
131 inline const_iterator begin() const { return _data.begin(); }
132
138 inline const_iterator cbegin() const { return _data.cbegin(); }
139
145 inline iterator end() { return _data.end(); }
146
152 inline const_iterator end() const { return _data.end(); }
153
159 inline const_iterator cend() const { return _data.cend(); }
160
166 inline size_type size() const { return _data.size(); }
167
177 if (_data.size() <= pos)
178 _data.resize(pos + 1);
179 _data[pos] = std::make_optional(c);
180 return _data[pos];
181 };
182
191 if (_data.size() <= pos)
192 _data.resize(pos + 1);
193 _data[pos] = std::make_optional(std::move(c));
194 return _data[pos];
195 };
196
205 template <class ... Params>
206 reference_type emplaceAt(size_type pos, Params &&...args) {
207 container_t vecArgs = {args...};
208
209 size_type i = vecArgs.size();
210 if (_data.size() <= pos + i)
211 _data.resize(pos + i);
212 i = 0;
213 for (value_type v : vecArgs) {
214 _data[pos + i] = v;
215 ++i;
216 }
217 return _data[pos];
218 };
219
225 void erase(size_type pos) {
226 if (pos >= 0 && pos < _data.size()) {
227 _data[pos] = std::nullopt;
228 }
229 };
230
237 size_type getIndex(const value_type &v) const {
238 auto it = std::find(_data.begin(), _data.end(), v);
239 if (it != _data.end())
240 return std::distance(_data.begin(), it);
241 return -1; // return 0 if not found
242 };
243
247 void print() const {
248 bool first = true;
249 std::cout << "{ ";
250 for (auto const &v : _data) {
251 if (!first) {
252 std::cout << ", " << v;
253 } else {
254 std::cout << v;
255 first = false;
256 }
257 }
258 std::cout << " }" << std::endl;
259 };
260
261 private:
262
263 container_t _data;
264 };
265
266} // namespace name
267
268
269#endif // SPARSE_ARRAY_HPP
Component is a template define in Registry. (not a class)
SparseArray class.
Definition SparseArray.hpp:45
const_iterator begin() const
give a const_iterator at the begining of the SparseArray
Definition SparseArray.hpp:131
reference_type insertAt(size_type pos, Component const &c)
insert a component at the given position & return a reference to it
Definition SparseArray.hpp:176
typename container_t::size_type size_type
Definition SparseArray.hpp:55
void erase(size_type pos)
erase the component at the given position
Definition SparseArray.hpp:225
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:190
const_iterator cbegin() const
give a const_iterator at the begining of the SparseArray
Definition SparseArray.hpp:138
const_iterator end() const
give a const_iterator at the end of the SparseArray
Definition SparseArray.hpp:152
iterator begin()
give a iterator at the begining of the SparseArray
Definition SparseArray.hpp:124
const_iterator cend() const
give a const_iterator at the end of the SparseArray
Definition SparseArray.hpp:159
value_type const & const_reference_type
Definition SparseArray.hpp:51
size_type size() const
give the size of the SparseArray
Definition SparseArray.hpp:166
reference_type emplaceAt(size_type pos, Params &&...args)
emplace a component at the given position & return a reference to it
Definition SparseArray.hpp:206
void print() const
print the SparseArray
Definition SparseArray.hpp:247
SparseArray(SparseArray &&) noexcept=default
move constructor
std::vector< value_type > container_t
Definition SparseArray.hpp:53
const_reference_type operator[](size_t idx) const
give the value at the index idx of the SparseArray (const)
Definition SparseArray.hpp:117
size_type getIndex(const value_type &v) const
get the index of the given value
Definition SparseArray.hpp:237
typename container_t::iterator iterator
Definition SparseArray.hpp:57
typename container_t::const_iterator const_iterator
Definition SparseArray.hpp:58
iterator end()
give a iterator at the end of the SparseArray
Definition SparseArray.hpp:145
value_type & reference_type
Definition SparseArray.hpp:50
SparseArray(SparseArray const &)=default
copy constructor
std::optional< Component > value_type
Definition SparseArray.hpp:49
Entity Component System.
std::ostream & operator<<(std::ostream &os, std::optional< Component > const &c)
help to print the optional value
Definition SparseArray.hpp:29