8#ifndef URANUS_SPARSEARRAY_HPP
9#define URANUS_SPARSEARRAY_HPP
24 template<
typename Component>
27 using ValueType = std::shared_ptr<Component>;
28 using ReferenceType = ValueType &;
29 using ConstReferenceType =
const ValueType &;
30 using Container = std::vector<ValueType>;
31 using SizeType =
typename Container::size_type;
32 using Iterator =
typename Container::iterator;
33 using ConstIterator =
typename Container::const_iterator;
79 ReferenceType operator[](std::
size_t idx);
87 ConstReferenceType operator[](std::
size_t idx) const;
101 ConstIterator
begin() const;
108 ConstIterator
cbegin() const;
122 ConstIterator
end() const;
129 ConstIterator
cend() const;
136 SizeType
size() const;
145 ReferenceType
insertAt(SizeType pos, const Component &);
155 ReferenceType
insertAt(SizeType pos, Component &&);
165 template<class... Params>
166 ReferenceType
emplaceAt(SizeType pos, Params &&...params);
173 void erase(SizeType pos);
181 std::optional<SizeType>
getIndex(const ValueType &) const;
193 template<typename Component>
200 template<
typename Component>
203 _data = std::move(other._data);
207 template<
typename Component>
210 if (idx >=
_data.size())
_data.resize(idx + 1);
214 template<
typename Component>
217 if (idx >=
_data.size())
return nullptr;
221 template<
typename Component>
224 return _data.begin();
227 template<
typename Component>
230 return _data.begin();
233 template<
typename Component>
236 return _data.cbegin();
239 template<
typename Component>
245 template<
typename Component>
251 template<
typename Component>
257 template<
typename Component>
263 template<
typename Component>
266 if (pos >=
_data.size())
_data.resize(pos + 1);
267 _data[pos] = std::make_shared<Component>(component);
271 template<
typename Component>
274 if (pos >=
_data.size())
_data.resize(pos + 1);
275 _data[pos] = std::make_shared<Component>(std::move(component));
280 template<
typename Component>
281 template<
class... Params>
284 if (pos >=
_data.size())
_data.resize(pos + 1);
285 _data[pos] = std::make_shared<Component>(std::forward<Params>(params)...);
289 template<
typename Component>
292 if (pos >=
_data.size())
return;
293 _data[pos] =
nullptr;
296 template<
typename Component>
299 for (SizeType i = 0; i <
_data.size(); i++) {
300 if (
_data[i] && ptr) {
301 if (std::addressof(
_data[i].get()) == std::addressof(ptr.get()))
return i;
307 template<
typename Component>
This file is used to include some core files of the engine.
SparseArray class, used to store components in a sparse way.
Definition: SparseArray.hpp:25
void erase(SizeType pos)
Erase the component at the given index.
Definition: SparseArray.hpp:290
Container & getData() const
Get the _data container.
Definition: SparseArray.hpp:308
SizeType size() const
Return the size of the SparseArray.
Definition: SparseArray.hpp:258
SparseArray(const SparseArray &)=default
Default copy constructor.
Iterator end()
Return the iterator pointing to the end of the SparseArray.
Definition: SparseArray.hpp:240
ReferenceType insertAt(SizeType pos, const Component &)
Insert a component at the given index.
Definition: SparseArray.hpp:264
std::optional< SizeType > getIndex(const ValueType &) const
Get the index of the entity possessing the given component.
Definition: SparseArray.hpp:297
SparseArray()=default
Default constructor.
ConstIterator cbegin() const
Return the const_iterator pointing to the beginning of the SparseArray.
Definition: SparseArray.hpp:234
SparseArray(SparseArray &&) noexcept=default
Default move constructor.
Container _data
Container of the components.
Definition: SparseArray.hpp:190
ReferenceType emplaceAt(SizeType pos, Params &&...params)
Emplace a component at the given index.
Definition: SparseArray.hpp:282
Iterator begin()
Return the iterator pointing to the beginning of the SparseArray.
Definition: SparseArray.hpp:222
SparseArray & operator=(const SparseArray &)
Copy assignment operator.
Definition: SparseArray.hpp:194
ConstIterator cend() const
Return the const_iterator pointing to the end of the SparseArray.
Definition: SparseArray.hpp:252
ReferenceType operator[](std::size_t idx)
Access the component at the given index, if it exists, otherwise resize the SparseArray.
Definition: SparseArray.hpp:208
Namespace containing all the ECS related classes.
Definition: Components.hpp:21