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"
27template<
typename Container>
28struct sparse_set_iterator final {
29 using value_type =
typename Container::value_type;
30 using pointer =
typename Container::const_pointer;
31 using reference =
typename Container::const_reference;
32 using difference_type =
typename Container::difference_type;
33 using iterator_category = std::random_access_iterator_tag;
35 constexpr sparse_set_iterator() noexcept
39 constexpr sparse_set_iterator(
const Container &ref,
const difference_type idx) noexcept
40 : packed{std::addressof(ref)},
43 constexpr sparse_set_iterator &operator++() noexcept {
44 return --offset, *
this;
47 constexpr sparse_set_iterator operator++(
int)
noexcept {
48 sparse_set_iterator orig = *
this;
49 return ++(*this), orig;
52 constexpr sparse_set_iterator &operator--() noexcept {
53 return ++offset, *
this;
56 constexpr sparse_set_iterator operator--(
int)
noexcept {
57 sparse_set_iterator orig = *
this;
58 return operator--(), orig;
61 constexpr sparse_set_iterator &operator+=(
const difference_type value)
noexcept {
66 constexpr sparse_set_iterator
operator+(
const difference_type value)
const noexcept {
67 sparse_set_iterator copy = *
this;
68 return (copy += value);
71 constexpr sparse_set_iterator &operator-=(
const difference_type value)
noexcept {
72 return (*
this += -value);
75 constexpr sparse_set_iterator operator-(
const difference_type value)
const noexcept {
76 return (*
this + -value);
79 [[nodiscard]]
constexpr reference operator[](
const difference_type value)
const noexcept {
80 return packed->data()[index() - value];
83 [[nodiscard]]
constexpr pointer operator->() const noexcept {
84 return packed->data() + index();
87 [[nodiscard]]
constexpr reference operator*() const noexcept {
91 [[nodiscard]]
constexpr pointer data() const noexcept {
92 return packed ? packed->data() :
nullptr;
95 [[nodiscard]]
constexpr difference_type index() const noexcept {
100 const Container *packed;
101 difference_type offset;
104template<
typename Container>
105[[nodiscard]]
constexpr std::ptrdiff_t operator-(
const sparse_set_iterator<Container> &lhs,
const sparse_set_iterator<Container> &rhs)
noexcept {
106 return rhs.index() - lhs.index();
109template<
typename Container>
110[[nodiscard]]
constexpr bool operator==(
const sparse_set_iterator<Container> &lhs,
const sparse_set_iterator<Container> &rhs)
noexcept {
111 return lhs.index() == rhs.index();
114template<
typename Container>
115[[nodiscard]]
constexpr bool operator!=(
const sparse_set_iterator<Container> &lhs,
const sparse_set_iterator<Container> &rhs)
noexcept {
116 return !(lhs == rhs);
119template<
typename Container>
120[[nodiscard]]
constexpr bool operator<(
const sparse_set_iterator<Container> &lhs,
const sparse_set_iterator<Container> &rhs)
noexcept {
121 return lhs.index() > rhs.index();
124template<
typename Container>
125[[nodiscard]]
constexpr bool operator>(
const sparse_set_iterator<Container> &lhs,
const sparse_set_iterator<Container> &rhs)
noexcept {
129template<
typename Container>
130[[nodiscard]]
constexpr bool operator<=(
const sparse_set_iterator<Container> &lhs,
const sparse_set_iterator<Container> &rhs)
noexcept {
134template<
typename Container>
135[[nodiscard]]
constexpr bool operator>=(
const sparse_set_iterator<Container> &lhs,
const sparse_set_iterator<Container> &rhs)
noexcept {
165template<
typename Entity,
typename Allocator>
167 using alloc_traits = std::allocator_traits<Allocator>;
168 static_assert(std::is_same_v<typename alloc_traits::value_type, Entity>,
"Invalid value type");
169 using sparse_container_type = std::vector<typename alloc_traits::pointer, typename alloc_traits::template rebind_alloc<typename alloc_traits::pointer>>;
170 using packed_container_type = std::vector<Entity, Allocator>;
172 [[nodiscard]]
auto sparse_ptr(
const Entity
entt)
const {
178 [[nodiscard]]
auto &sparse_ref(
const Entity
entt)
const {
179 ENTT_ASSERT(sparse_ptr(
entt),
"Invalid element");
184 [[nodiscard]]
auto to_iterator(
const Entity
entt)
const {
188 [[nodiscard]]
auto &assure_at_least(
const Entity
entt) {
192 if(!(page < sparse.size())) {
193 sparse.resize(page + 1u,
nullptr);
197 auto page_allocator{packed.get_allocator()};
203 ENTT_ASSERT(elem ==
null,
"Slot not available");
207 void release_sparse_pages() {
208 auto page_allocator{packed.get_allocator()};
210 for(
auto &&page: sparse) {
211 if(page !=
nullptr) {
220 virtual const void *get_at(
const std::size_t)
const {
224 virtual void swap_or_move(
const std::size_t,
const std::size_t) {}
235 void swap_at(
const std::size_t lhs,
const std::size_t rhs) {
243 swap(packed[lhs], packed[rhs]);
252 auto &self = sparse_ref(*it);
257 ENTT_ASSERT((packed.back() =
null,
true),
"");
281 for(; first != last; ++first) {
285 for(; first != last; ++first) {
293 if(
const auto prev = std::exchange(free_list,
tombstone); prev ==
null) {
294 for(
auto first =
begin(); !(first.index() < 0); ++first) {
295 sparse_ref(*first) =
null;
298 for(
auto first =
begin(); !(first.index() < 0); ++first) {
300 sparse_ref(*first) =
null;
315 ENTT_ASSERT(!
contains(
entt),
"Set already contains entity");
317 if(
auto &elem = assure_at_least(
entt); free_list ==
null || force_back) {
318 packed.push_back(
entt);
324 free_list = std::exchange(packed[pos],
entt);
325 return --(
end() - pos);
341 using pointer =
typename packed_container_type::const_pointer;
389 : sparse{std::move(other.sparse)},
390 packed{std::move(other.packed)},
392 free_list{std::exchange(other.free_list,
tombstone)},
401 : sparse{std::move(other.sparse), allocator},
402 packed{std::move(other.packed), allocator},
404 free_list{std::exchange(other.free_list,
tombstone)},
406 ENTT_ASSERT(alloc_traits::is_always_equal::value || packed.get_allocator() == other.packed.get_allocator(),
"Copying a sparse set is not allowed");
411 release_sparse_pages();
420 ENTT_ASSERT(alloc_traits::is_always_equal::value || packed.get_allocator() == other.packed.get_allocator(),
"Copying a sparse set is not allowed");
422 release_sparse_pages();
423 sparse = std::move(other.sparse);
424 packed = std::move(other.packed);
426 free_list = std::exchange(other.free_list,
tombstone);
437 swap(sparse, other.sparse);
438 swap(packed, other.packed);
439 swap(info, other.info);
440 swap(free_list, other.free_list);
441 swap(mode, other.mode);
449 return packed.get_allocator();
478 return packed.capacity();
483 packed.shrink_to_fit();
511 return packed.size();
518 [[nodiscard]]
bool empty() const noexcept {
519 return packed.empty();
527 return (free_list ==
null);
535 return packed.data();
547 const auto pos =
static_cast<typename iterator::difference_type
>(packed.size());
580 return std::make_reverse_iterator(
end());
594 return std::make_reverse_iterator(
begin());
618 const auto elem = sparse_ptr(
entt);
631 const auto elem = sparse_ptr(
entt);
647 ENTT_ASSERT(
contains(
entt),
"Set does not contain entity");
657 return pos < packed.size() ? packed[pos] :
null;
666 ENTT_ASSERT(pos < packed.size(),
"Position is out of bounds");
686 return const_cast<void *
>(std::as_const(*this).value(
entt));
718 template<
typename It>
720 for(
auto it = first; it != last; ++it) {
724 return first == last ?
end() :
find(*first);
755 const auto it = to_iterator(
entt);
768 template<
typename It>
770 if constexpr(std::is_same_v<It, basic_iterator>) {
773 for(; first != last; ++first) {
795 template<
typename It>
799 if constexpr(std::is_same_v<It, basic_iterator>) {
800 while(first != last) {
801 while(first != last && !
contains(*first)) {
805 const auto it = first;
807 while(first != last &&
contains(*first)) {
811 count += std::distance(it, first);
815 for(; first != last; ++first) {
826 for(; from && packed[from - 1u] ==
tombstone; --from) {}
831 swap_or_move(from, to);
833 packed[to] = std::exchange(packed[from],
tombstone);
838 for(; from && packed[from - 1u] ==
tombstone; --from) {}
860 const auto from =
index(lhs);
861 const auto to =
index(rhs);
864 swap_or_move(from, to);
898 template<
typename Compare,
typename Sort =
std_sort,
typename... Args>
899 void sort_n(
const size_type length, Compare compare, Sort algo = Sort{}, Args &&...args) {
900 ENTT_ASSERT(!(length > packed.size()),
"Length exceeds the number of elements");
901 ENTT_ASSERT(free_list ==
null,
"Partial sorting with tombstones is not supported");
903 algo(packed.rend() - length, packed.rend(), std::move(compare), std::forward<Args>(args)...);
905 for(
size_type pos{}; pos < length; ++pos) {
907 auto next =
index(packed[curr]);
909 while(curr != next) {
910 const auto idx =
index(packed[next]);
911 const auto entt = packed[curr];
913 swap_or_move(next, idx);
916 curr = std::exchange(next, idx);
933 template<
typename Compare,
typename Sort = std_sort,
typename... Args>
934 void sort(Compare compare, Sort algo = Sort{}, Args &&...args) {
936 sort_n(packed.size(), std::move(compare), std::move(algo), std::forward<Args>(args)...);
952 const auto to = other.
end();
953 auto from = other.
begin();
955 for(
auto it =
begin(); it.index() && from != to; ++from) {
956 if(
const auto curr = *from;
contains(curr)) {
957 if(
const auto entt = *it;
entt != curr) {
971 ENTT_ASSERT((
compact(),
size()) == 0u,
"Non-empty set");
988 sparse_container_type sparse;
989 packed_container_type packed;
typename Traits::entity_type entity_type
Underlying entity type.
static constexpr entity_type to_integral(const value_type value) noexcept
Converts an entity to its underlying type.
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.
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.
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 rbegin() const noexcept
Returns a reverse iterator to the beginning.
iterator const_iterator
Constant random access iterator type.
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 another sparse set.
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.
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.
entity_type operator[](const size_type pos) const noexcept
Returns the entity at specified location, without bounds checking.
internal::sparse_set_iterator< packed_container_type > basic_iterator
Random access iterator type.
virtual ~basic_sparse_set()
Default destructor.
entity_type at(const size_type pos) const noexcept
Returns the entity at specified location, with bounds checking.
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.
const type_info & type() const noexcept
Returned value type, if any.
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.
void swap_at(const std::size_t lhs, const std::size_t rhs)
Swaps two items at specific locations.
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.
const_iterator begin() const noexcept
Returns an iterator to the beginning.
Allocator allocator_type
Allocator type.
iterator find(const entity_type entt) const noexcept
Finds an entity.
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_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.