1 #ifndef ENTT_ENTITY_SPARSE_SET_HPP 2 #define ENTT_ENTITY_SPARSE_SET_HPP 12 #include <type_traits> 13 #include "entt_traits.hpp" 56 template<
typename Entity>
60 struct Iterator
final {
61 using difference_type = std::size_t;
62 using value_type = Entity;
63 using pointer = value_type *;
64 using reference = value_type;
65 using iterator_category = std::input_iterator_tag;
67 Iterator(
const std::vector<value_type> &direct, std::size_t pos)
68 : direct{direct}, pos{pos}
71 Iterator & operator++() noexcept {
75 Iterator operator++(
int) noexcept {
76 Iterator orig = *
this;
77 return ++(*this), orig;
80 Iterator & operator+=(difference_type value) noexcept {
85 Iterator operator+(difference_type value) noexcept {
86 return Iterator{direct, pos-value};
89 bool operator==(
const Iterator &other)
const noexcept {
90 return other.pos == pos;
93 bool operator!=(
const Iterator &other)
const noexcept {
94 return !(*
this == other);
97 reference operator*()
const noexcept {
102 const std::vector<value_type> &direct;
106 static constexpr Entity in_use = (Entity{1} << traits_type::entity_shift);
157 return reverse.size();
171 return direct.size();
179 return direct.empty();
198 return direct.data();
214 return Iterator{direct, direct.size()};
231 return Iterator{direct, 0};
240 const auto pos =
size_type(entity & traits_type::entity_mask);
242 return (pos < reverse.size()) && (reverse[pos] & in_use);
263 const auto pos =
size_type(entity & traits_type::entity_mask);
264 assert(pos < reverse.size());
266 return (reverse[pos] & in_use);
283 const auto entt = entity & traits_type::entity_mask;
285 return reverse[
entt] & traits_type::entity_mask;
300 assert(!has(entity));
301 const auto pos =
size_type(entity & traits_type::entity_mask);
303 if(!(pos < reverse.size())) {
309 reverse[pos] =
pos_type(direct.size()) | in_use;
310 direct.emplace_back(entity);
326 const auto entt = entity & traits_type::entity_mask;
327 const auto back = direct.back() & traits_type::entity_mask;
329 const auto pos = reverse[
entt] & traits_type::entity_mask;
330 reverse[back] = reverse[
entt];
333 direct[pos] = direct.back();
353 assert(lhs < direct.size());
354 assert(rhs < direct.size());
355 const auto src = direct[lhs] & traits_type::entity_mask;
356 const auto dst = direct[rhs] & traits_type::entity_mask;
357 std::swap(reverse[src], reverse[dst]);
358 std::swap(direct[lhs], direct[rhs]);
381 auto from = other.begin();
382 auto to = other.end();
386 while(pos && from != to) {
388 if(*from != direct[pos]) {
389 swap(pos,
get(*from));
408 std::vector<pos_type> reverse;
409 std::vector<entity_type> direct;
435 template<
typename Entity,
typename Type>
439 struct Iterator
final {
440 using difference_type = std::size_t;
441 using value_type = Type;
442 using pointer = value_type *;
443 using reference = value_type &;
444 using iterator_category = std::input_iterator_tag;
446 Iterator(std::vector<value_type> &instances, std::size_t pos)
447 : instances{instances}, pos{pos}
450 Iterator & operator++() noexcept {
454 Iterator operator++(
int) noexcept {
455 Iterator orig = *
this;
456 return ++(*this), orig;
459 Iterator & operator+=(difference_type value) noexcept {
464 Iterator operator+(difference_type value) noexcept {
465 return Iterator{instances, pos-value};
468 bool operator==(
const Iterator &other)
const noexcept {
469 return other.pos == pos;
472 bool operator!=(
const Iterator &other)
const noexcept {
473 return !(*
this == other);
476 reference operator*() noexcept {
477 return instances[pos-1];
480 pointer operator->() noexcept {
481 return &instances.data()[pos-1];
485 std::vector<value_type> &instances;
523 underlying_type::reserve(cap);
524 instances.reserve(cap);
543 return instances.data();
562 return instances.data();
578 return Iterator{instances, instances.size()};
596 return Iterator{instances, 0};
612 return instances[underlying_type::get(entity)];
651 template<
typename... Args>
652 std::enable_if_t<std::is_constructible<Type, Args...>::value,
object_type &>
654 underlying_type::construct(entity);
655 instances.emplace_back(std::forward<Args>(args)...);
656 return instances.back();
679 template<
typename... Args>
680 std::enable_if_t<!std::is_constructible<Type, Args...>::value,
object_type &>
682 underlying_type::construct(entity);
683 instances.emplace_back(Type{std::forward<Args>(args)...});
684 return instances.back();
701 auto tmp = std::move(instances.back());
702 instances[underlying_type::get(entity)] = std::move(tmp);
703 instances.pop_back();
704 underlying_type::destroy(entity);
730 template<
typename Compare>
732 std::vector<pos_type> copy(instances.size());
733 std::iota(copy.begin(), copy.end(), 0);
735 std::sort(copy.begin(), copy.end(), [
this, compare = std::move(compare)](
auto lhs,
auto rhs) {
736 return compare(const_cast<const object_type &>(instances[rhs]), const_cast<const object_type &>(instances[lhs]));
739 for(
pos_type pos = 0, last = copy.size(); pos < last; ++pos) {
741 auto next = copy[curr];
743 while(curr != next) {
744 const auto lhs = copy[curr];
745 const auto rhs = copy[next];
746 std::swap(instances[lhs], instances[rhs]);
747 underlying_type::swap(lhs, rhs);
779 auto from = other.begin();
780 auto to = other.end();
782 pos_type pos = underlying_type::size() - 1;
783 const auto *local = underlying_type::data();
785 while(pos && from != to) {
786 const auto curr = *from;
788 if(underlying_type::has(curr)) {
789 if(curr != *(local + pos)) {
790 auto candidate = underlying_type::get(curr);
791 std::swap(instances[pos], instances[candidate]);
792 underlying_type::swap(pos, candidate);
806 underlying_type::reset();
811 std::vector<object_type> instances;
818 #endif // ENTT_ENTITY_SPARSE_SET_HPP size_type extent() const noexcept
Returns the extent of a sparse set.
void respect(const SparseSet< Entity > &other) noexcept
Sort entities according to their order in another sparse set.
void sort(Compare compare)
Sort components according to the given comparison function.
virtual void reset()
Resets a sparse set.
void reset() override
Resets a sparse set.
const object_type * raw() const noexcept
Direct access to the array of objects.
bool empty() const noexcept
Checks whether a sparse set is empty.
std::size_t size_type
Unsigned integer type.
Extended sparse set implementation.
void destroy(entity_type entity) override
Removes an entity from a sparse set and destroies its object.
Entity entity_type
Underlying entity identifier.
Iterator iterator_type
Input iterator type.
Basic sparse set implementation.
std::enable_if_t< std::is_constructible< Type, Args... >::value, object_type & > construct(entity_type entity, Args &&... args)
Assigns an entity to a sparse set and constructs its object.
bool has(entity_type entity) const noexcept
Checks if a sparse set contains an entity.
std::enable_if_t<!std::is_constructible< Type, Args... >::value, object_type & > construct(entity_type entity, Args &&... args)
Assigns an entity to a sparse set and constructs its object.
entity_type pos_type
Entity dependent position type.
Type object_type
Type of the objects associated to the entities.
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 an entity from a sparse set.
iterator_type begin() noexcept
Returns an iterator to the beginning.
bool fast(entity_type entity) const noexcept
Checks if a sparse set contains an entity (unsafe).
iterator_type begin() const noexcept
Returns an iterator to the beginning.
iterator_type end() noexcept
Returns an iterator to the end.
void construct(entity_type entity)
Assigns an entity to a sparse set.
size_type size() const noexcept
Returns the number of elements in a sparse set.
void respect(const SparseSet< Entity > &other) noexcept
Sort components according to the order of the entities in another sparse set.
constexpr bool operator!=(const HashedString &lhs, const HashedString &rhs) noexcept
Compares two hashed strings.
object_type * raw() noexcept
Direct access to the array of objects.
void swap(pos_type lhs, pos_type rhs) noexcept
Swaps the position of two entities in the internal packed array.