8 #ifndef SPARSEARRAY_HPP_
9 #define SPARSEARRAY_HPP_
21 template <
typename Component>
24 using value_type = std::optional<Component>;
25 using reference_type = value_type &;
26 using const_reference_type = value_type
const &;
27 using container_t = std::vector<value_type>;
28 using iterator =
typename container_t::iterator;
29 using const_iterator =
typename container_t::const_iterator;
30 using size_type =
typename container_t::size_type;
45 for (std::size_t i = 0; i < sp_arr.
size(); i++) {
46 _data.push_back(std::nullopt);
48 for (std::size_t i = 0; i < sp_arr.
size(); i++) {
49 _data[i] = sp_arr._data[i];
67 for (std::size_t i = 0; i <
size; i++) {
68 _data.push_back(std::nullopt);
85 std::swap(_data, sp_arr._data);
97 _data = std::move(sp_arr._data);
130 return _data.begin();
140 return _data.begin();
150 return _data.cbegin();
168 const_iterator
end()
const
200 for (
size_t x = 0; x < sizeToExtend; x += 1)
201 _data.push_back(std::nullopt);
213 if (pos > _data.size())
214 throw std::invalid_argument(
"Invalid pos in insert_at (SparseArray)");
215 if (pos < _data.size()) {
216 auto tmp = _data.get_allocator();
218 std::allocator_traits<decltype(tmp)>::destroy(tmp, std::addressof(_data[pos]));
236 if (pos > _data.size())
237 throw std::invalid_argument(
"Invalid position in sparse array");
238 if (pos < _data.size()) {
239 auto tmp = _data.get_allocator();
241 std::allocator_traits<decltype(tmp)>::destroy(tmp, std::addressof(_data[pos]));
258 template <
class... Params>
261 if (pos > _data.size())
262 throw std::invalid_argument(
"Invalid pos in sparse array");
263 auto tmp = _data.get_allocator();
264 std::allocator_traits<decltype(tmp)>::destroy(tmp, std::addressof(_data[pos]));
265 std::allocator_traits<decltype(tmp)>::construct(tmp, std::addressof(_data[pos]), std::forward(params)...);
276 _data[pos] = std::nullopt;
287 auto tmp = std::addressof(val_type);
289 for (std::size_t i = 0; i < _data.size(); i++) {
290 if (tmp == std::addressof(_data[i]))
293 throw std::invalid_argument(
"Component doesn't exist (SparseArray)");
Class that handle Element of type Component like a vector which can be holed.
Definition: SparseArray.hpp:22
iterator end()
Overloads the operator ebd()
Definition: SparseArray.hpp:158
Sparse_array & operator=(Sparse_array &&sp_arr) noexcept
Overload the operator =.
Definition: SparseArray.hpp:95
Sparse_array(Sparse_array &&sp_arr) noexcept
Construct a new Sparse_array object.
Definition: SparseArray.hpp:58
reference_type emplace_at(size_type pos, Params &&...params)
Overloads the operator emplace_at()
Definition: SparseArray.hpp:259
const_reference_type operator[](size_t idx) const
Overloads the operator [] const.
Definition: SparseArray.hpp:118
size_type size() const
Overloads the operator size()
Definition: SparseArray.hpp:188
reference_type operator[](size_t idx)
Overloads the operator [].
Definition: SparseArray.hpp:107
Namespace for all components.
Definition: CConnection.hpp:14
size_type get_index(value_type const &val_type) const
Get the index object.
Definition: SparseArray.hpp:285
Namespace for all components.
Definition: CDamage.hpp:14
void extend(size_t sizeToExtend)
Overload the operator extend()
Definition: SparseArray.hpp:198
iterator begin()
Overloads the operator begin()
Definition: SparseArray.hpp:128
const_iterator cbegin() const
Overloads the operator cbegin() const.
Definition: SparseArray.hpp:148
Sparse_array(std::size_t size)
Construct a new Sparse_array object.
Definition: SparseArray.hpp:65
void erase(size_type pos)
Overloads the operator erase()
Definition: SparseArray.hpp:274
const_iterator cend() const
Overloads the operator cend() const.
Definition: SparseArray.hpp:178
reference_type insert_at(size_type pos, Component &&component)
Overloads the operator insert_at()
Definition: SparseArray.hpp:234
Sparse_array & operator=(Sparse_array const &sp_arr)
Overload the operator =.
Definition: SparseArray.hpp:83
virtual ~Sparse_array()=default
Destroy the Sparse_array object.
Sparse_array()=default
Construct a new Sparse_array object.
Sparse_array(Sparse_array const &sp_arr)
Construct a new Sparse_array object.
Definition: SparseArray.hpp:43
reference_type insert_at(size_type pos, Component const &component)
Overloads the operator insert_at()
Definition: SparseArray.hpp:211
const_iterator begin() const
Overloads the operator begin() const.
Definition: SparseArray.hpp:138
const_iterator end() const
Overloads the operator end() const.
Definition: SparseArray.hpp:168