1 #ifndef ENTT_ENTITY_SPARSE_SET_HPP 2 #define ENTT_ENTITY_SPARSE_SET_HPP 53 template<
typename Entity>
58 using value_type = Entity;
60 Iterator(
const std::vector<Entity> *direct, std::size_t pos)
61 : direct{direct}, pos{pos}
64 Iterator & operator++() noexcept {
68 Iterator operator++(
int) noexcept {
69 Iterator orig = *
this;
70 return ++(*this), orig;
73 bool operator==(
const Iterator &other)
const noexcept {
74 return other.pos == pos && other.direct == direct;
77 bool operator!=(
const Iterator &other)
const noexcept {
78 return !(*
this == other);
81 value_type operator*()
const noexcept {
82 return (*direct)[pos-1];
86 const std::vector<Entity> *direct;
127 return direct.size();
135 return direct.empty();
154 return direct.data();
170 return Iterator{&direct, direct.size()};
187 return Iterator{&direct, 0};
196 const auto entt = entity & traits_type::entity_mask;
197 return entt < reverse.size() && reverse[
entt] < direct.size() && direct[reverse[
entt]] == entity;
214 return reverse[entity & traits_type::entity_mask];
230 assert(!has(entity));
231 const auto entt = entity & traits_type::entity_mask;
233 if(!(
entt < reverse.size())) {
234 reverse.resize(
entt+1);
237 const auto pos =
pos_type(direct.size());
239 direct.emplace_back(entity);
257 const auto entt = entity & traits_type::entity_mask;
258 const auto back = direct.back() & traits_type::entity_mask;
259 const auto pos = reverse[
entt];
261 direct[pos] = direct.back();
283 const auto le = lhs & traits_type::entity_mask;
284 const auto re = rhs & traits_type::entity_mask;
285 std::swap(direct[reverse[le]], direct[reverse[re]]);
286 std::swap(reverse[le], reverse[re]);
304 template<
typename Compare>
306 std::vector<pos_type> copy{direct.cbegin(), direct.cend()};
307 std::sort(copy.begin(), copy.end(), [compare = std::move(compare)](
auto... args) {
308 return !compare(args...);
311 for(
pos_type i = 0; i < copy.size(); ++i) {
312 if(direct[i] != copy[i]) {
313 swap(direct[i], copy[i]);
338 struct Bool {
bool value{
false}; };
339 std::vector<Bool> check(std::max(other.reverse.size(), reverse.size()));
341 for(
auto entity: other.direct) {
342 check[entity & traits_type::entity_mask].value =
true;
345 sort([
this, &other, &check](
auto lhs,
auto rhs) {
346 const auto le = lhs & traits_type::entity_mask;
347 const auto re = rhs & traits_type::entity_mask;
349 const bool bLhs = check[le].value;
350 const bool bRhs = check[re].value;
351 bool compare =
false;
354 compare = other.
get(rhs) < other.
get(lhs);
355 }
else if(!bLhs && !bRhs) {
374 std::vector<entity_type> reverse;
375 std::vector<entity_type> direct;
401 template<
typename Entity,
typename Type>
445 const
type * raw() const noexcept {
446 return instances.data();
465 return instances.data();
481 return instances[underlying_type::get(entity)];
497 return const_cast<type &
>(
const_cast<const SparseSet *
>(
this)->
get(entity));
514 template<
typename... Args>
516 underlying_type::construct(entity);
517 instances.push_back({ std::forward<Args>(args)... });
518 return instances.back();
533 instances[underlying_type::get(entity)] = std::move(instances.back());
534 instances.pop_back();
535 underlying_type::destroy(entity);
555 std::swap(instances[underlying_type::get(lhs)], instances[underlying_type::get(rhs)]);
556 underlying_type::swap(lhs, rhs);
563 underlying_type::reset();
568 std::vector<type> instances;
575 #endif // ENTT_ENTITY_SPARSE_SET_HPP void swap(entity_type lhs, entity_type rhs) override
Swaps the two entities and their objects.
void sort(Compare compare)
Sort entities according to the given comparison function.
Type type
Type of the objects associated to the entities.
virtual void reset()
Resets the sparse set.
void reset() override
Resets the sparse set.
bool empty() const noexcept
Checks whether the sparse set is empty.
virtual void swap(entity_type lhs, entity_type rhs)
Swaps the position of the entities in the internal packed array.
type & construct(entity_type entity, Args &&... args)
Assigns an entity to the sparse set and constructs its object.
std::size_t size_type
Unsigned integer type.
void respect(const SparseSet< Entity > &other)
Sort entities according to their order in the given sparse set.
Extended sparse set implementation.
void destroy(entity_type entity) override
Removes an entity from the sparse set and destroies its object.
Entity entity_type
Underlying entity identifier.
Iterator iterator_type
Input iterator type.
Basic sparse set implementation.
type * raw() noexcept
Direct access to the array of objects.
bool has(entity_type entity) const noexcept
Checks if the sparse set contains the given entity.
entity_type pos_type
Entity dependent position type.
const entity_type * data() const noexcept
Direct access to the internal packed array.
iterator_type end() const noexcept
Returns an iterator to the end.
virtual void destroy(entity_type entity)
Removes the given entity from the sparse set.
bool operator!=(const SigH< Ret(Args...)> &lhs, const SigH< Ret(Args...)> &rhs) noexcept
Checks if the contents of the two signals are different.
iterator_type begin() const noexcept
Returns an iterator to the beginning.
pos_type construct(entity_type entity)
Assigns an entity to the sparse set.
pos_type get(entity_type entity) const noexcept
Returns the position of the entity in the sparse set.