18template <
typename Component,
typename Allocator = std::allocator<Component>>
27 using iterator =
typename container_t::iterator;
77 assert(idx < _data.size() &&
"Index out of bounds");
88 assert(idx < _data.size() &&
"Index out of bounds");
107 return _data.begin();
116 return _data.cbegin();
164 _data[pos] = component;
177 _data[pos] = std::move(component);
189 template <
class... Params>
193 std::allocator_traits<Allocator>::construct(
194 _allocator, std::addressof(_data[pos]), std::forward<Params>(params)...);
204 if (pos < _data.size()) {
216 if (pos >= _data.size()) {
217 _data.resize(pos + 1);
222 Allocator _allocator;
A container that provides sparse storage for optional components.
reference_type insert_at(size_type pos, const Component &component)
Inserts a component at a specific position.
SparseArray()=default
Default constructor.
value_type & reference_type
iterator begin()
Returns an iterator to the beginning of the array.
std::optional< Component > value_type
Type aliases for convenience.
std::vector< value_type, Allocator > container_t
SparseArray & operator=(SparseArray &&other) noexcept=default
Move assignment operator.
const_iterator begin() const
Returns a const iterator to the beginning of the array.
const_iterator cend() const
Returns a const iterator to the end of the array.
reference_type insert_at(size_type pos, Component &&component)
Inserts a component at a specific position (move version).
typename container_t::size_type size_type
SparseArray(const SparseArray &other)=default
Copy constructor.
const_reference_type operator[](std::size_t idx) const
Accesses the value at the given index (const version).
typename container_t::iterator iterator
const_iterator end() const
Returns a const iterator to the end of the array.
iterator end()
Returns an iterator to the end of the array.
const value_type & const_reference_type
SparseArray(SparseArray &&other) noexcept=default
Move constructor.
SparseArray & operator=(const SparseArray &other)=default
Copy assignment operator.
~SparseArray()=default
Destructor.
void erase(size_type pos)
Removes the component at a specific position.
size_type size() const
Returns the size of the array.
reference_type operator[](std::size_t idx)
Accesses the value at the given index.
reference_type emplace_at(size_type pos, Params &&... params)
Constructs and inserts a component at a specific position.
typename container_t::const_iterator const_iterator
const_iterator cbegin() const
Returns a const iterator to the beginning of the array.