8#ifndef URANUS_SPARSEARRAY_HPP
9#define URANUS_SPARSEARRAY_HPP
15namespace uranus::ecs {
21 template<
typename Component>
24 using ValueType = std::shared_ptr<std::optional<Component>>;
25 using ReferenceType = ValueType &;
26 using ConstReferenceType =
const ValueType &;
27 using Container = std::vector<ValueType>;
28 using SizeType =
typename Container::size_type;
29 using Iterator =
typename Container::iterator;
30 using ConstIterator =
typename Container::const_iterator;
76 ReferenceType operator[](std::
size_t idx);
84 ConstReferenceType operator[](std::
size_t idx) const;
98 ConstIterator
begin() const;
105 ConstIterator
cbegin() const;
119 ConstIterator
end() const;
126 ConstIterator
cend() const;
133 SizeType
size() const;
142 ReferenceType
insertAt(SizeType pos, const Component &);
152 ReferenceType
insertAt(SizeType pos, Component &&);
162 template<class... Params>
163 ReferenceType
emplaceAt(SizeType pos, Params &&...params);
170 void erase(SizeType pos);
178 std::optional<SizeType>
getIndex(const ValueType &) const;
190 template<typename Component>
197 template<
typename Component>
200 _data = std::move(other._data);
204 template<
typename Component>
207 if (idx >=
_data.size())
_data.resize(idx + 1);
211 template<
typename Component>
214 if (idx >=
_data.size())
return nullptr;
218 template<
typename Component>
221 return _data.begin();
224 template<
typename Component>
227 return _data.begin();
230 template<
typename Component>
233 return _data.cbegin();
236 template<
typename Component>
242 template<
typename Component>
248 template<
typename Component>
254 template<
typename Component>
260 template<
typename Component>
263 if (pos >=
_data.size())
_data.resize(pos + 1);
264 _data[pos] = std::make_shared<std::optional<Component>>(std::make_optional<Component>(component));
268 template<
typename Component>
271 if (pos >=
_data.size())
_data.resize(pos + 1);
272 _data[pos] = std::move(std::make_shared<std::optional<Component>>(std::make_optional<Component>(component)));
277 template<
typename Component>
278 template<
class... Params>
281 if (pos >=
_data.size())
_data.resize(pos + 1);
282 _data[pos] = std::make_shared<std::optional<Component>>(std::make_optional<Component>(std::forward<Params>(params)...));
286 template<
typename Component>
289 if (pos >=
_data.size())
return;
290 _data[pos] =
nullptr;
293 template<
typename Component>
296 for (SizeType i = 0; i <
_data.size(); i++) {
297 if (
_data[i] &&
_data[i]->has_value() && ptr->has_value()) {
298 if (std::addressof(
_data[i]->value()) == std::addressof(ptr->value()))
return i;
304 template<
typename Component>
SparseArray class, used to store components in a sparse way.
Definition: SparseArray.hpp:22
void erase(SizeType pos)
Erase the component at the given index.
Definition: SparseArray.hpp:287
Container & getData() const
Get the _data container.
Definition: SparseArray.hpp:305
SizeType size() const
Return the size of the SparseArray.
Definition: SparseArray.hpp:255
SparseArray(const SparseArray &)=default
Default copy constructor.
Iterator end()
Return the iterator pointing to the end of the SparseArray.
Definition: SparseArray.hpp:237
ReferenceType insertAt(SizeType pos, const Component &)
Insert a component at the given index.
Definition: SparseArray.hpp:261
std::optional< SizeType > getIndex(const ValueType &) const
Get the index of the entity possessing the given component.
Definition: SparseArray.hpp:294
SparseArray()=default
Default constructor.
ConstIterator cbegin() const
Return the const_iterator pointing to the beginning of the SparseArray.
Definition: SparseArray.hpp:231
SparseArray(SparseArray &&) noexcept=default
Default move constructor.
Container _data
Container of the components.
Definition: SparseArray.hpp:187
ReferenceType emplaceAt(SizeType pos, Params &&...params)
Emplace a component at the given index.
Definition: SparseArray.hpp:279
Iterator begin()
Return the iterator pointing to the beginning of the SparseArray.
Definition: SparseArray.hpp:219
SparseArray & operator=(const SparseArray &)
Copy assignment operator.
Definition: SparseArray.hpp:191
ConstIterator cend() const
Return the const_iterator pointing to the end of the SparseArray.
Definition: SparseArray.hpp:249
ReferenceType operator[](std::size_t idx)
Access the component at the given index, if it exists, otherwise resize the SparseArray.
Definition: SparseArray.hpp:205