11#ifndef SPARSE_ARRAY_HPP
12#define SPARSE_ARRAY_HPP
31 template <
typename Component>
32 std::ostream&
operator<<(std::ostream& os, std::optional<Component>
const &c)
47 template <
typename Component>
60 using iterator =
typename container_t::iterator;
180 if (_data.size() <= pos)
181 _data.resize(pos + 1);
182 _data[pos] = std::make_optional(c);
194 if (_data.size() <= pos)
195 _data.resize(pos + 1);
196 _data[pos] = std::make_optional(std::move(c));
208 template <
class ... Params>
213 if (_data.size() <= pos + i)
214 _data.resize(pos + i);
229 if (pos >= 0 && pos < _data.size()) {
230 _data[pos] = std::nullopt;
241 auto it = std::find(_data.begin(), _data.end(), v);
242 if (it != _data.end())
243 return std::distance(_data.begin(), it);
253 for (
auto const &v : _data) {
255 std::cout <<
", " << v;
261 std::cout <<
" }" << std::endl;
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
std::ostream & operator<<(std::ostream &os, std::optional< Component > const &c)
help to print the optional value
Definition SparseArray.hpp:32