1#ifndef ENTT_ENTITY_SPARSE_SET_HPP
2#define ENTT_ENTITY_SPARSE_SET_HPP
10#include "../config/config.h"
11#include "../core/algorithm.hpp"
12#include "../core/any.hpp"
13#include "../core/memory.hpp"
14#include "../core/type_info.hpp"
23template<
typename Container>
24struct sparse_set_iterator final {
25 using value_type =
typename Container::value_type;
26 using pointer =
typename Container::const_pointer;
27 using reference =
typename Container::const_reference;
28 using difference_type =
typename Container::difference_type;
29 using iterator_category = std::random_access_iterator_tag;
31 constexpr sparse_set_iterator() noexcept
35 constexpr sparse_set_iterator(
const Container &ref,
const difference_type idx) noexcept
36 : packed{std::addressof(ref)},
39 constexpr sparse_set_iterator &operator++() noexcept {
40 return --offset, *
this;
43 constexpr sparse_set_iterator operator++(
int)
noexcept {
44 sparse_set_iterator orig = *
this;
45 return ++(*this), orig;
48 constexpr sparse_set_iterator &operator--() noexcept {
49 return ++offset, *
this;
52 constexpr sparse_set_iterator operator--(
int)
noexcept {
53 sparse_set_iterator orig = *
this;
54 return operator--(), orig;
57 constexpr sparse_set_iterator &operator+=(
const difference_type value)
noexcept {
62 constexpr sparse_set_iterator
operator+(
const difference_type value)
const noexcept {
63 sparse_set_iterator copy = *
this;
64 return (copy += value);
67 constexpr sparse_set_iterator &operator-=(
const difference_type value)
noexcept {
68 return (*
this += -value);
71 constexpr sparse_set_iterator operator-(
const difference_type value)
const noexcept {
72 return (*
this + -value);
75 [[nodiscard]]
constexpr reference operator[](
const difference_type value)
const noexcept {
76 return packed->data()[index() - value];
79 [[nodiscard]]
constexpr pointer operator->() const noexcept {
80 return packed->data() + index();
83 [[nodiscard]]
constexpr reference operator*() const noexcept {
87 [[nodiscard]]
constexpr pointer data() const noexcept {
88 return packed ? packed->data() :
nullptr;
91 [[nodiscard]]
constexpr difference_type index() const noexcept {
96 const Container *packed;
97 difference_type offset;
100template<
typename Container>
101[[nodiscard]]
constexpr std::ptrdiff_t operator-(
const sparse_set_iterator<Container> &lhs,
const sparse_set_iterator<Container> &rhs)
noexcept {
102 return rhs.index() - lhs.index();
105template<
typename Container>
106[[nodiscard]]
constexpr bool operator==(
const sparse_set_iterator<Container> &lhs,
const sparse_set_iterator<Container> &rhs)
noexcept {
107 return lhs.index() == rhs.index();
110template<
typename Container>
111[[nodiscard]]
constexpr bool operator!=(
const sparse_set_iterator<Container> &lhs,
const sparse_set_iterator<Container> &rhs)
noexcept {
112 return !(lhs == rhs);
115template<
typename Container>
116[[nodiscard]]
constexpr bool operator<(
const sparse_set_iterator<Container> &lhs,
const sparse_set_iterator<Container> &rhs)
noexcept {
117 return lhs.index() > rhs.index();
120template<
typename Container>
121[[nodiscard]]
constexpr bool operator>(
const sparse_set_iterator<Container> &lhs,
const sparse_set_iterator<Container> &rhs)
noexcept {
125template<
typename Container>
126[[nodiscard]]
constexpr bool operator<=(
const sparse_set_iterator<Container> &lhs,
const sparse_set_iterator<Container> &rhs)
noexcept {
130template<
typename Container>
131[[nodiscard]]
constexpr bool operator>=(
const sparse_set_iterator<Container> &lhs,
const sparse_set_iterator<Container> &rhs)
noexcept {
157template<
typename Entity,
typename Allocator>
159 using alloc_traits = std::allocator_traits<Allocator>;
160 static_assert(std::is_same_v<typename alloc_traits::value_type, Entity>,
"Invalid value type");
161 using sparse_container_type = std::vector<typename alloc_traits::pointer, typename alloc_traits::template rebind_alloc<typename alloc_traits::pointer>>;
162 using packed_container_type = std::vector<Entity, Allocator>;
165 [[nodiscard]]
auto sparse_ptr(
const Entity
entt)
const {
171 [[nodiscard]]
auto &sparse_ref(
const Entity
entt)
const {
172 ENTT_ASSERT(sparse_ptr(
entt),
"Invalid element");
177 [[nodiscard]]
auto to_iterator(
const Entity
entt)
const {
181 [[nodiscard]]
auto &assure_at_least(
const Entity
entt) {
185 if(!(page < sparse.size())) {
186 sparse.resize(page + 1u,
nullptr);
191 auto page_allocator{packed.get_allocator()};
199 void release_sparse_pages() {
200 auto page_allocator{packed.get_allocator()};
202 for(
auto &&page: sparse) {
203 if(page !=
nullptr) {
211 void swap_at(
const std::size_t from,
const std::size_t to) {
212 auto &lhs = packed[from];
213 auto &rhs = packed[to];
221 underlying_type policy_to_head() {
226 virtual const void *get_at(
const std::size_t)
const {
230 virtual void swap_or_move([[maybe_unused]]
const std::size_t lhs, [[maybe_unused]]
const std::size_t rhs) {
244 const auto pos =
static_cast<underlying_type
>(
index(*it));
246 swap_at(pos,
static_cast<size_type>(head -= (pos < head)));
255 auto &self = sparse_ref(*it);
260 ENTT_ASSERT((packed.back() =
null,
true),
"");
285 for(; first != last; ++first) {
290 for(; first != last; ++first) {
295 for(; first != last; ++first) {
307 for(
auto first =
begin(); !(first.index() < 0); ++first) {
309 sparse_ref(*first) =
null;
317 for(
auto first =
begin(); !(first.index() < 0); ++first) {
318 sparse_ref(*first) =
null;
323 head = policy_to_head();
334 auto &elem = assure_at_least(
entt);
341 ENTT_ASSERT(elem ==
null,
"Slot not available");
348 packed.push_back(
entt);
349 ENTT_ASSERT(elem ==
null,
"Slot not available");
354 packed.push_back(
entt);
369 return --(
end() - pos);
384 using pointer =
typename packed_container_type::const_pointer;
425 head{policy_to_head()} {}
432 : sparse{std::move(other.sparse)},
433 packed{std::move(other.packed)},
436 head{std::exchange(other.head, policy_to_head())} {}
444 : sparse{std::move(other.sparse), allocator},
445 packed{std::move(other.packed), allocator},
448 head{std::exchange(other.head, policy_to_head())} {
449 ENTT_ASSERT(alloc_traits::is_always_equal::value || packed.get_allocator() == other.packed.get_allocator(),
"Copying a sparse set is not allowed");
454 release_sparse_pages();
463 ENTT_ASSERT(alloc_traits::is_always_equal::value || packed.get_allocator() == other.packed.get_allocator(),
"Copying a sparse set is not allowed");
465 release_sparse_pages();
466 sparse = std::move(other.sparse);
467 packed = std::move(other.packed);
470 head = std::exchange(other.head, policy_to_head());
480 swap(sparse, other.sparse);
481 swap(packed, other.packed);
482 swap(info, other.info);
483 swap(mode, other.mode);
484 swap(head, other.head);
492 return packed.get_allocator();
517 head =
static_cast<underlying_type
>(len);
538 return packed.capacity();
543 packed.shrink_to_fit();
571 return packed.size();
578 [[nodiscard]]
bool empty() const noexcept {
579 return packed.empty();
595 return packed.data();
607 const auto pos =
static_cast<typename iterator::difference_type
>(packed.size());
640 return std::make_reverse_iterator(
end());
654 return std::make_reverse_iterator(
begin());
684 return std::make_reverse_iterator(
end(0));
694 return std::make_reverse_iterator(
begin(0));
718 const auto elem = sparse_ptr(
entt);
732 const auto elem = sparse_ptr(
entt);
748 ENTT_ASSERT(
contains(
entt),
"Set does not contain entity");
758 return pos < packed.size() ? packed[pos] :
null;
767 ENTT_ASSERT(pos < packed.size(),
"Position is out of bounds");
787 return const_cast<void *
>(std::as_const(*this).value(
entt));
819 template<
typename It>
821 for(
auto it = first; it != last; ++it) {
825 return first == last ?
end() :
find(*first);
856 const auto it = to_iterator(
entt);
869 template<
typename It>
871 if constexpr(std::is_same_v<It, basic_iterator>) {
874 for(; first != last; ++first) {
896 template<
typename It>
900 if constexpr(std::is_same_v<It, basic_iterator>) {
901 while(first != last) {
902 while(first != last && !
contains(*first)) {
906 const auto it = first;
908 while(first != last &&
contains(*first)) {
912 count += std::distance(it, first);
916 for(; first != last; ++first) {
928 for(; from && packed[from - 1u] ==
tombstone; --from) {}
934 swap_or_move(from, to);
936 packed[to] = packed[from];
940 for(; from && packed[from - 1u] ==
tombstone; --from) {}
944 packed.erase(packed.begin() + from, packed.end());
962 const auto from =
index(lhs);
963 const auto to =
index(rhs);
966 swap_or_move(from, to);
1000 template<
typename Compare,
typename Sort =
std_sort,
typename... Args>
1001 void sort_n(
const size_type length, Compare compare, Sort algo = Sort{}, Args &&...args) {
1003 ENTT_ASSERT(!(length > packed.size()),
"Length exceeds the number of elements");
1005 algo(packed.rend() - length, packed.rend(), std::move(compare), std::forward<Args>(args)...);
1007 for(
size_type pos{}; pos < length; ++pos) {
1009 auto next =
index(packed[curr]);
1011 while(curr != next) {
1012 const auto idx =
index(packed[next]);
1013 const auto entt = packed[curr];
1015 swap_or_move(next, idx);
1018 curr = std::exchange(next, idx);
1035 template<
typename Compare,
typename Sort = std_sort,
typename... Args>
1036 void sort(Compare compare, Sort algo = Sort{}, Args &&...args) {
1037 sort_n(
static_cast<size_type>(
end(0) -
begin(0)), std::move(compare), std::move(algo), std::forward<Args>(args)...);
1052 template<
typename It>
1056 for(
auto it =
begin(0); it.index() && first != last; ++first) {
1057 if(
const auto curr = *first;
contains(curr)) {
1058 if(
const auto entt = *it;
entt != curr) {
1080 ENTT_ASSERT((
compact(),
size()) == 0u,
"Non-empty set");
1081 head = policy_to_head();
1097 sparse_container_type sparse;
1098 packed_container_type packed;
1101 underlying_type head;
typename Traits::entity_type entity_type
Underlying entity type.
static constexpr value_type next(const value_type value) noexcept
Returns the successor of a given identifier.
static constexpr entity_type to_integral(const value_type value) noexcept
Converts an entity to its underlying type.
static constexpr entity_type entity_mask
Entity mask size.
static constexpr entity_type to_entity(const value_type value) noexcept
Returns the entity part once converted to the underlying type.
typename Traits::value_type value_type
Value type.
typename Traits::version_type version_type
Underlying version type.
static constexpr value_type combine(const entity_type lhs, const entity_type rhs) noexcept
Combines two identifiers in a single one.
static constexpr version_type to_version(const value_type value) noexcept
Returns the version part once converted to the underlying type.
Basic sparse set implementation.
void erase(const entity_type entt)
Erases an entity from a sparse set.
void swap(basic_sparse_set &other)
Exchanges the contents with those of a given sparse set.
iterator begin() const noexcept
Returns an iterator to the beginning.
typename traits_type::version_type version_type
Underlying version type.
virtual size_type capacity() const noexcept
Returns the number of elements that a sparse set has currently allocated space for.
const_iterator cbegin() const noexcept
Returns an iterator to the beginning.
basic_sparse_set(const allocator_type &allocator)
Constructs an empty container with a given allocator.
virtual basic_iterator try_emplace(const Entity entt, const bool force_back, const void *=nullptr)
Assigns an entity to a sparse set.
virtual void pop_all()
Erases all entities of a sparse set.
void erase(It first, It last)
Erases entities from a set.
std::reverse_iterator< const_iterator > const_reverse_iterator
Constant reverse iterator type.
void swap_elements(const entity_type lhs, const entity_type rhs)
Swaps two entities in a sparse set.
void swap_and_pop(const basic_iterator it)
Erases an entity from a sparse set.
iterator end(int) const noexcept
Returns an iterator to the end. Useful only in case of swap-only policy.
std::size_t size_type
Unsigned integer type.
iterator end() const noexcept
Returns an iterator to the end.
void * value(const entity_type entt) noexcept
Returns the element assigned to an entity, if any.
const_reverse_iterator crend(int) const noexcept
Returns a reverse iterator to the beginning. Useful only in case of swap-only policy.
const_iterator cbegin(int) const noexcept
Returns an iterator to the beginning. Useful only in case of swap-only policy.
iterator const_iterator
Constant random access iterator type.
const_iterator cend(int) const noexcept
Returns an iterator to the end. Useful only in case of swap-only policy.
reverse_iterator rend() const noexcept
Returns a reverse iterator to the end.
iterator push(It first, It last)
Assigns one or more entities to a sparse set.
void sort_n(const size_type length, Compare compare, Sort algo=Sort{}, Args &&...args)
Sort the first count elements according to the given comparison function.
pointer data() const noexcept
Direct access to the internal packed array.
basic_sparse_set & operator=(basic_sparse_set &&other) noexcept
Move assignment operator.
virtual void reserve(const size_type cap)
Increases the capacity of a sparse set.
void clear()
Clears a sparse set.
size_type size() const noexcept
Returns the number of elements in a sparse set.
basic_iterator iterator
Random access iterator type.
bool contiguous() const noexcept
Checks whether a sparse set is fully packed.
version_type bump(const entity_type entt)
Bump the version number of an entity.
void sort_as(const basic_sparse_set &other)
Sort entities according to their order in a range.
virtual void shrink_to_fit()
Requests the removal of unused capacity.
size_type extent() const noexcept
Returns the extent of a sparse set.
typename packed_container_type::const_pointer pointer
Pointer type to contained entities.
const_iterator find(const entity_type entt) const noexcept
Finds an entity.
basic_sparse_set(basic_sparse_set &&other, const allocator_type &allocator) noexcept
Allocator-extended move constructor.
iterator push(const entity_type entt, const void *elem=nullptr)
Assigns an entity to a sparse set.
basic_sparse_set(deletion_policy pol, const allocator_type &allocator={})
Constructs an empty container with the given policy and allocator.
virtual void pop(basic_iterator first, basic_iterator last)
Erases entities from a sparse set.
deletion_policy policy() const noexcept
Returns the deletion policy of a sparse set.
version_type current(const entity_type entt) const noexcept
Returns the contained version for an identifier.
bool contains(const entity_type entt) const noexcept
Checks if a sparse set contains an entity.
virtual void bind(any) noexcept
Forwards variables to derived classes, if any.
const_iterator cend() const noexcept
Returns an iterator to the end.
void in_place_pop(const basic_iterator it)
Erases an entity from a sparse set.
basic_sparse_set(basic_sparse_set &&other) noexcept
Move constructor.
const_reverse_iterator crend() const noexcept
Returns a reverse iterator to the end.
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
size_type remove(It first, It last)
Removes entities from a sparse set if they exist.
const void * value(const entity_type entt) const noexcept
Returns the element assigned to an entity, if any.
const_reverse_iterator crbegin(int) const noexcept
Returns a reverse iterator to the beginning. Useful only in case of swap-only policy.
void free_list(const size_type len) noexcept
Sets the head of the free list, if possible.
reverse_iterator rbegin() const noexcept
Returns a reverse iterator to the beginning.
entity_type operator[](const size_type pos) const noexcept
Returns the entity at specified location, without bounds checking.
size_type free_list() const noexcept
Returns the head of the free list, if any.
void swap_only(const basic_iterator it)
Erases an entity from a sparse set.
internal::sparse_set_iterator< packed_container_type > basic_iterator
Random access iterator type.
virtual ~basic_sparse_set()
Default destructor.
iterator begin(int) const noexcept
Returns an iterator to the beginning. Useful only in case of swap-only policy.
entity_type at(const size_type pos) const noexcept
Returns the entity at specified location, with bounds checking.
reverse_iterator rend(int) const noexcept
Returns a reverse iterator to the beginning. Useful only in case of swap-only policy.
const_reverse_iterator crbegin() const noexcept
Returns a reverse iterator to the beginning.
bool empty() const noexcept
Checks whether a sparse set is empty.
size_type index(const entity_type entt) const noexcept
Returns the position of an entity in a sparse set.
void sort_as(It first, It last)
Sort entities according to their order in a range.
const type_info & type() const noexcept
Returned value type, if any.
reverse_iterator rbegin(int) const noexcept
Returns a reverse iterator to the beginning. Useful only in case of swap-only policy.
void sort(Compare compare, Sort algo=Sort{}, Args &&...args)
Sort all elements according to the given comparison function.
typename traits_type::value_type entity_type
Underlying entity identifier.
bool remove(const entity_type entt)
Removes an entity from a sparse set if it exists.
void compact()
Removes all tombstones from a sparse set.
basic_sparse_set(const type_info &elem, deletion_policy pol=deletion_policy::swap_and_pop, const allocator_type &allocator={})
Constructs an empty container with the given value type, policy and allocator.
Allocator allocator_type
Allocator type.
basic_sparse_set()
Default constructor.
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator type.
entity
Default entity identifier.
constexpr null_t null
Compile-time constant for null entities.
constexpr tombstone_t tombstone
Compile-time constant for tombstone entities.
constexpr std::size_t fast_mod(const std::size_t value, const std::size_t mod) noexcept
Fast module utility function (powers of two only).
constexpr bool operator<=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator<(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr type_list< Type..., Other... > operator+(type_list< Type... >, type_list< Other... >)
Concatenates multiple type lists.
deletion_policy
Storage deletion policy.
@ swap_only
Swap-only deletion policy.
@ swap_and_pop
Swap-and-pop deletion policy.
@ in_place
In-place deletion policy.
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator>=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
const type_info & type_id() noexcept
Returns the type info object associated to a given type.
constexpr bool operator>(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
static constexpr std::size_t page_size
Page size, default is ENTT_SPARSE_PAGE.
Function object to wrap std::sort in a class type.
Implementation specific information about a type.