8#ifndef SPARSE_ARRAY_HPP
9#define SPARSE_ARRAY_HPP
28 template <
typename Component>
29 std::ostream&
operator<<(std::ostream& os, std::optional<Component>
const &c)
44 template <
typename Component>
57 using iterator =
typename container_t::iterator;
177 if (_data.size() <= pos)
178 _data.resize(pos + 1);
179 _data[pos] = std::make_optional(c);
191 if (_data.size() <= pos)
192 _data.resize(pos + 1);
193 _data[pos] = std::make_optional(std::move(c));
205 template <
class ... Params>
210 if (_data.size() <= pos + i)
211 _data.resize(pos + i);
226 if (pos >= 0 && pos < _data.size()) {
227 _data[pos] = std::nullopt;
238 auto it = std::find(_data.begin(), _data.end(), v);
239 if (it != _data.end())
240 return std::distance(_data.begin(), it);
250 for (
auto const &v : _data) {
252 std::cout <<
", " << v;
258 std::cout <<
" }" << std::endl;
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
std::ostream & operator<<(std::ostream &os, std::optional< Component > const &c)
help to print the optional value
Definition SparseArray.hpp:29