1#ifndef ENTT_ENTITY_STORAGE_HPP
2#define ENTT_ENTITY_STORAGE_HPP
11#include "../config/config.h"
12#include "../core/bit.hpp"
13#include "../core/iterator.hpp"
14#include "../core/memory.hpp"
15#include "../core/type_info.hpp"
16#include "component.hpp"
19#include "sparse_set.hpp"
26template<
typename Container>
27class storage_iterator final {
28 friend storage_iterator<const Container>;
30 using container_type = std::remove_const_t<Container>;
31 using alloc_traits = std::allocator_traits<typename container_type::allocator_type>;
33 using iterator_traits = std::iterator_traits<std::conditional_t<
34 std::is_const_v<Container>,
35 typename alloc_traits::template rebind_traits<typename std::pointer_traits<typename container_type::value_type>::element_type>::const_pointer,
36 typename alloc_traits::template rebind_traits<typename std::pointer_traits<typename container_type::value_type>::element_type>::pointer>>;
39 using value_type =
typename iterator_traits::value_type;
40 using pointer =
typename iterator_traits::pointer;
41 using reference =
typename iterator_traits::reference;
42 using difference_type =
typename iterator_traits::difference_type;
43 using iterator_category = std::random_access_iterator_tag;
45 constexpr storage_iterator() noexcept = default;
47 constexpr storage_iterator(Container *ref, const difference_type idx) noexcept
51 template<
bool Const = std::is_const_v<Container>,
typename = std::enable_if_t<Const>>
52 constexpr storage_iterator(
const storage_iterator<std::remove_const_t<Container>> &other) noexcept
53 : storage_iterator{other.payload, other.offset} {}
55 constexpr storage_iterator &operator++() noexcept {
56 return --offset, *
this;
59 constexpr storage_iterator operator++(
int)
noexcept {
60 storage_iterator orig = *
this;
61 return ++(*this), orig;
64 constexpr storage_iterator &operator--() noexcept {
65 return ++offset, *
this;
68 constexpr storage_iterator operator--(
int)
noexcept {
69 storage_iterator orig = *
this;
70 return operator--(), orig;
73 constexpr storage_iterator &operator+=(
const difference_type value)
noexcept {
78 constexpr storage_iterator
operator+(
const difference_type value)
const noexcept {
79 storage_iterator copy = *
this;
80 return (copy += value);
83 constexpr storage_iterator &operator-=(
const difference_type value)
noexcept {
84 return (*
this += -value);
87 constexpr storage_iterator operator-(
const difference_type value)
const noexcept {
88 return (*
this + -value);
91 [[nodiscard]]
constexpr reference operator[](
const difference_type value)
const noexcept {
92 const auto pos = index() - value;
93 constexpr auto page_size = component_traits<value_type>::page_size;
94 return (*payload)[pos / page_size][
fast_mod(
static_cast<std::size_t
>(pos), page_size)];
97 [[nodiscard]]
constexpr pointer operator->() const noexcept {
98 return std::addressof(
operator[](0));
101 [[nodiscard]]
constexpr reference operator*() const noexcept {
102 return operator[](0);
105 [[nodiscard]]
constexpr difference_type index() const noexcept {
111 difference_type offset;
114template<
typename Lhs,
typename Rhs>
115[[nodiscard]]
constexpr std::ptrdiff_t operator-(
const storage_iterator<Lhs> &lhs,
const storage_iterator<Rhs> &rhs)
noexcept {
116 return rhs.index() - lhs.index();
119template<
typename Lhs,
typename Rhs>
120[[nodiscard]]
constexpr bool operator==(
const storage_iterator<Lhs> &lhs,
const storage_iterator<Rhs> &rhs)
noexcept {
121 return lhs.index() == rhs.index();
124template<
typename Lhs,
typename Rhs>
125[[nodiscard]]
constexpr bool operator!=(
const storage_iterator<Lhs> &lhs,
const storage_iterator<Rhs> &rhs)
noexcept {
126 return !(lhs == rhs);
129template<
typename Lhs,
typename Rhs>
130[[nodiscard]]
constexpr bool operator<(
const storage_iterator<Lhs> &lhs,
const storage_iterator<Rhs> &rhs)
noexcept {
131 return lhs.index() > rhs.index();
134template<
typename Lhs,
typename Rhs>
135[[nodiscard]]
constexpr bool operator>(
const storage_iterator<Lhs> &lhs,
const storage_iterator<Rhs> &rhs)
noexcept {
139template<
typename Lhs,
typename Rhs>
140[[nodiscard]]
constexpr bool operator<=(
const storage_iterator<Lhs> &lhs,
const storage_iterator<Rhs> &rhs)
noexcept {
144template<
typename Lhs,
typename Rhs>
145[[nodiscard]]
constexpr bool operator>=(
const storage_iterator<Lhs> &lhs,
const storage_iterator<Rhs> &rhs)
noexcept {
149template<
typename It,
typename... Other>
150class extended_storage_iterator final {
151 template<
typename Iter,
typename... Args>
152 friend class extended_storage_iterator;
155 using iterator_type = It;
156 using value_type =
decltype(std::tuple_cat(std::make_tuple(*std::declval<It>()), std::forward_as_tuple(*std::declval<Other>()...)));
157 using pointer = input_iterator_pointer<value_type>;
158 using reference = value_type;
159 using difference_type = std::ptrdiff_t;
160 using iterator_category = std::input_iterator_tag;
161 using iterator_concept = std::forward_iterator_tag;
163 constexpr extended_storage_iterator()
166 constexpr extended_storage_iterator(iterator_type base, Other... other)
167 : it{base, other...} {}
169 template<
typename... Args,
typename = std::enable_if_t<(!std::is_same_v<Other, Args> && ...) && (std::is_constructible_v<Other, Args> && ...)>>
170 constexpr extended_storage_iterator(
const extended_storage_iterator<It, Args...> &other)
173 constexpr extended_storage_iterator &operator++() noexcept {
174 return ++std::get<It>(it), (++std::get<Other>(it), ...), *
this;
177 constexpr extended_storage_iterator operator++(
int)
noexcept {
178 extended_storage_iterator orig = *
this;
179 return ++(*this), orig;
182 [[nodiscard]]
constexpr pointer operator->() const noexcept {
186 [[nodiscard]]
constexpr reference operator*() const noexcept {
187 return {*std::get<It>(it), *std::get<Other>(it)...};
190 [[nodiscard]]
constexpr iterator_type base() const noexcept {
191 return std::get<It>(it);
194 template<
typename... Lhs,
typename... Rhs>
195 friend constexpr bool operator==(
const extended_storage_iterator<Lhs...> &,
const extended_storage_iterator<Rhs...> &)
noexcept;
198 std::tuple<It, Other...> it;
201template<
typename... Lhs,
typename... Rhs>
202[[nodiscard]]
constexpr bool operator==(
const extended_storage_iterator<Lhs...> &lhs,
const extended_storage_iterator<Rhs...> &rhs)
noexcept {
203 return std::get<0>(lhs.it) == std::get<0>(rhs.it);
206template<
typename... Lhs,
typename... Rhs>
207[[nodiscard]]
constexpr bool operator!=(
const extended_storage_iterator<Lhs...> &lhs,
const extended_storage_iterator<Rhs...> &rhs)
noexcept {
208 return !(lhs == rhs);
229template<
typename Type,
typename Entity,
typename Allocator,
typename>
231 using alloc_traits = std::allocator_traits<Allocator>;
232 static_assert(std::is_same_v<typename alloc_traits::value_type, Type>,
"Invalid value type");
233 using container_type = std::vector<typename alloc_traits::pointer, typename alloc_traits::template rebind_alloc<typename alloc_traits::pointer>>;
238 [[
nodiscard]]
auto &element_at(
const std::size_t
pos)
const {
242 auto assure_at_least(
const std::size_t
pos) {
245 if(!(
idx < payload.size())) {
246 auto curr = payload.size();
248 payload.resize(
idx + 1u,
nullptr);
251 for(
const auto last = payload.size();
curr < last; ++
curr) {
256 payload.resize(
curr);
264 template<
typename...
Args>
280 void shrink_to_size(
const std::size_t
sz) {
287 alloc_traits::destroy(
allocator, std::addressof(element_at(
pos)));
290 alloc_traits::destroy(
allocator, std::addressof(element_at(
pos)));
294 for(
auto pos =
from, last = payload.size();
pos < last; ++
pos) {
298 payload.resize(
from);
302 [[
nodiscard]]
const void *get_at(
const std::size_t
pos)
const final {
303 return std::addressof(element_at(
pos));
307 static constexpr bool is_pinned_type_v = !(std::is_move_constructible_v<Type> && std::is_move_assignable_v<Type>);
334 void pop(underlying_iterator first, underlying_iterator last)
override {
360 alloc_traits::destroy(
allocator, std::addressof(element_at(
static_cast<size_type>(first.index()))));
364 alloc_traits::destroy(
allocator, std::addressof(element_at(
static_cast<size_type>(first.index()))));
377 if(
value !=
nullptr) {
378 if constexpr(std::is_copy_constructible_v<element_type>) {
384 if constexpr(std::is_default_constructible_v<element_type>) {
406 using pointer =
typename container_type::pointer;
408 using const_pointer =
typename alloc_traits::template rebind_traits<typename alloc_traits::const_pointer>::const_pointer;
410 using iterator = internal::storage_iterator<container_type>;
449 payload{std::move(
other.payload)} {}
460 ENTT_ASSERT(alloc_traits::is_always_equal::value ||
get_allocator() ==
other.get_allocator(),
"Copying a storage is not allowed");
481 ENTT_ASSERT(alloc_traits::is_always_equal::value ||
get_allocator() ==
other.get_allocator(),
"Copying a storage is not allowed");
501 return payload.get_allocator();
515 assure_at_least(
cap - 1u);
539 return payload.data();
544 return payload.data();
597 return std::make_reverse_iterator(
cend());
607 return std::make_reverse_iterator(
end());
616 return std::make_reverse_iterator(
cbegin());
626 return std::make_reverse_iterator(
begin());
654 return std::forward_as_tuple(
get(
entt));
659 return std::forward_as_tuple(
get(
entt));
674 template<
typename...
Args>
676 if constexpr(std::is_aggregate_v<value_type> && (
sizeof...(Args) != 0
u || !std::is_default_constructible_v<value_type>)) {
677 const auto it = emplace_element(
entt,
false, Type{std::forward<Args>(
args)...});
678 return element_at(
static_cast<size_type>(
it.index()));
680 const auto it = emplace_element(
entt,
false, std::forward<Args>(
args)...);
681 return element_at(
static_cast<size_type>(
it.index()));
692 template<
typename... Func>
696 (std::forward<Func>(func)(
elem), ...);
714 template<
typename It>
716 for(; first != last; ++first) {
717 emplace_element(*first,
true,
value);
738 for(; first != last; ++first, ++
from) {
739 emplace_element(*first,
true, *
from);
779 container_type payload;
783template<
typename Type,
typename Entity,
typename Allocator>
785 :
public basic_sparse_set<Entity, typename std::allocator_traits<Allocator>::template rebind_alloc<Entity>> {
786 using alloc_traits = std::allocator_traits<Allocator>;
787 static_assert(std::is_same_v<typename alloc_traits::value_type, Type>,
"Invalid value type");
864 if constexpr(std::is_void_v<element_type> && !std::is_constructible_v<allocator_type, typename base_type::allocator_type>) {
904 template<
typename...
Args>
915 template<
typename... Func>
918 (std::forward<Func>(func)(), ...);
928 template<
typename It,
typename...
Args>
930 for(; first != last; ++first) {
973template<
typename Entity,
typename Allocator>
976 using alloc_traits = std::allocator_traits<Allocator>;
977 static_assert(std::is_same_v<typename alloc_traits::value_type, Entity>,
"Invalid value type");
981 auto next()
noexcept {
985 ENTT_ASSERT(placeholder < traits_type::to_entity(
null),
"No more entities available");
1053 placeholder{
other.placeholder} {}
1062 placeholder{
other.placeholder} {}
1079 placeholder =
other.placeholder;
1104 return std::tuple{};
1142 template<
typename... Func>
1145 (std::forward<Func>(func)(), ...);
1154 template<
typename It>
1160 for(; first != last; ++first) {
1173 return std::as_const(*this).each();
1190 return std::as_const(*this).reach();
typename Traits::entity_type entity_type
Underlying entity type.
Sparse set implementation.
iterator begin() const noexcept
Returns an iterator to the beginning.
const_iterator cbegin() const noexcept
Returns an iterator to the beginning.
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 swap_and_pop(const basic_iterator it)
Erases an entity from a sparse set.
iterator end() const noexcept
Returns an iterator to the end.
reverse_iterator rend() const noexcept
Returns a reverse iterator to the end.
pointer data() const noexcept
Direct access to the internal packed array.
virtual void reserve(const size_type cap)
Increases the capacity of a sparse set.
size_type size() const noexcept
Returns the number of elements in a sparse set.
basic_sparse_set & operator=(const basic_sparse_set &)=delete
Default copy assignment operator, deleted on purpose.
virtual void shrink_to_fit()
Requests the removal of unused capacity.
void swap(basic_sparse_set &other) noexcept
Exchanges the contents with those of a given sparse set.
const_iterator find(const entity_type entt) const noexcept
Finds an entity.
virtual void pop(basic_iterator first, basic_iterator last)
Erases entities from 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.
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.
const_reverse_iterator crend() const noexcept
Returns a reverse iterator to the end.
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
const void * value(const entity_type entt) const noexcept
Returns the element assigned to an entity, if any.
reverse_iterator rbegin() const noexcept
Returns a reverse iterator to the beginning.
size_type free_list() const noexcept
Returns data on the free list whose meaning depends on the mode.
internal::sparse_set_iterator< packed_container_type > basic_iterator
Random access iterator type.
const_reverse_iterator crbegin() const noexcept
Returns a reverse iterator to the beginning.
size_type index(const entity_type entt) const noexcept
Returns the position of an entity in a sparse set.
const_reverse_iterable reach() const noexcept
Returns a reverse iterable object to use to visit a storage.
std::size_t size_type
Unsigned integer type.
void get(const entity_type entt) const noexcept
Returns the object assigned to an entity, that is void.
void value_type
Type of the objects assigned to entities.
void insert(It first, It last)
Assigns each element in a range an identifier.
Entity entity_type
Underlying entity identifier.
void pop_all() override
Erases all entities of a storage.
entity_type emplace(const entity_type hint)
Creates a new identifier or recycles a destroyed one.
~basic_storage() override=default
Default destructor.
underlying_iterator try_emplace(const Entity hint, const bool, const void *) override
Assigns an entity to a storage.
basic_storage & operator=(basic_storage &&other) noexcept
Move assignment operator.
reverse_iterable reach() noexcept
Returns a reverse iterable object to use to visit a storage.
basic_storage(basic_storage &&other, const allocator_type &allocator)
Allocator-extended move constructor.
Entity element_type
Element type.
const_iterable each() const noexcept
Returns an iterable object to use to visit a storage.
basic_storage(const allocator_type &allocator)
Constructs an empty container with a given allocator.
Allocator allocator_type
Allocator type.
basic_storage(basic_storage &&other) noexcept
Move constructor.
entity_type emplace()
Creates a new identifier or recycles a destroyed one.
iterable each() noexcept
Returns an iterable object to use to visit a storage.
basic_storage(const basic_storage &)=delete
Default copy constructor, deleted on purpose.
void patch(const entity_type entt, Func &&...func)
Updates a given identifier.
basic_storage()
Default constructor.
basic_storage & operator=(const basic_storage &)=delete
Default copy assignment operator, deleted on purpose.
std::tuple get_as_tuple(const entity_type entt) const noexcept
Returns an empty tuple.
basic_storage(const basic_storage &)=delete
Default copy constructor, deleted on purpose.
basic_storage()
Default constructor.
void emplace(const entity_type entt, Args &&...)
Assigns an entity to a storage and constructs its object.
reverse_iterable reach() noexcept
Returns a reverse iterable object to use to visit a storage.
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
void get(const entity_type entt) const noexcept
Returns the object assigned to an entity, that is void.
basic_storage(basic_storage &&other) noexcept=default
Move constructor.
std::size_t size_type
Unsigned integer type.
Type element_type
Element type.
~basic_storage() override=default
Default destructor.
basic_storage(const allocator_type &allocator)
Constructs an empty container with a given allocator.
basic_storage & operator=(const basic_storage &)=delete
Default copy assignment operator, deleted on purpose.
Entity entity_type
Underlying entity identifier.
const_iterable each() const noexcept
Returns an iterable object to use to visit a storage.
Allocator allocator_type
Allocator type.
void patch(const entity_type entt, Func &&...func)
Updates the instance assigned to a given entity in-place.
void value_type
Type of the objects assigned to entities.
std::tuple get_as_tuple(const entity_type entt) const noexcept
Returns an empty tuple.
iterable each() noexcept
Returns an iterable object to use to visit a storage.
basic_storage & operator=(basic_storage &&other) noexcept=default
Move assignment operator.
void insert(It first, It last, Args &&...)
Assigns entities to a storage.
basic_storage(basic_storage &&other, const allocator_type &allocator)
Allocator-extended move constructor.
const_reverse_iterable reach() const noexcept
Returns a reverse iterable object to use to visit a storage.
iterator end() noexcept
Returns an iterator to the end.
internal::storage_iterator< const container_type > const_iterator
Constant random access iterator type.
~basic_storage() override
Default destructor.
basic_storage()
Default constructor.
iterator insert(EIt first, EIt last, CIt from)
Assigns one or more entities to a storage and constructs their objects from a given range.
const_reverse_iterator rbegin() const noexcept
Returns a reverse iterator to the beginning.
const_iterator end() const noexcept
Returns an iterator to the end.
const_pointer raw() const noexcept
Direct access to the array of objects.
void pop_all() override
Erases all entities of a storage.
basic_storage(basic_storage &&other) noexcept
Move constructor.
const value_type & get(const entity_type entt) const noexcept
Returns the object assigned to an entity.
const_iterator cend() const noexcept
Returns an iterator to the end.
size_type capacity() const noexcept override
Returns the number of elements that a storage has currently allocated space for.
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
typename alloc_traits::template rebind_traits< typename alloc_traits::const_pointer >::const_pointer const_pointer
Constant pointer type to contained elements.
const_iterable each() const noexcept
Returns an iterable object to use to visit a storage.
void swap(basic_storage &other) noexcept
Exchanges the contents with those of a given storage.
element_type value_type
Type of the objects assigned to entities.
reverse_iterator rbegin() noexcept
Returns a reverse iterator to the beginning.
const_iterator begin() const noexcept
Returns an iterator to the beginning.
reverse_iterator rend() noexcept
Returns a reverse iterator to the end.
basic_storage & operator=(basic_storage &&other) noexcept
Move assignment operator.
Entity entity_type
Underlying entity identifier.
const_reverse_iterator rend() const noexcept
Returns a reverse iterator to the end.
underlying_iterator try_emplace(const Entity entt, const bool force_back, const void *value) override
Assigns an entity to a storage.
const_iterator cbegin() const noexcept
Returns an iterator to the beginning.
std::reverse_iterator< const_iterator > const_reverse_iterator
Constant reverse iterator type.
value_type & patch(const entity_type entt, Func &&...func)
Updates the instance assigned to a given entity in-place.
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator type.
std::size_t size_type
Unsigned integer type.
iterator insert(It first, It last, const value_type &value={})
Assigns one or more entities to a storage and constructs their objects from a given instance.
void pop(underlying_iterator first, underlying_iterator last) override
Erases entities from a storage.
typename container_type::pointer pointer
Pointer type to contained elements.
reverse_iterable reach() noexcept
Returns a reverse iterable object to use to visit a storage.
pointer raw() noexcept
Direct access to the array of objects.
std::tuple< value_type & > get_as_tuple(const entity_type entt) noexcept
Returns the object assigned to an entity as a tuple.
value_type & get(const entity_type entt) noexcept
Returns the object assigned to an entity.
const_reverse_iterator crend() const noexcept
Returns a reverse iterator to the end.
const_reverse_iterable reach() const noexcept
Returns a reverse iterable object to use to visit a storage.
iterable each() noexcept
Returns an iterable object to use to visit a storage.
basic_storage & operator=(const basic_storage &)=delete
Default copy assignment operator, deleted on purpose.
Type element_type
Element type.
void reserve(const size_type cap) override
Increases the capacity of a storage.
basic_storage(const allocator_type &allocator)
Constructs an empty storage with a given allocator.
value_type & emplace(const entity_type entt, Args &&...args)
Assigns an entity to a storage and constructs its object.
std::tuple< const value_type & > get_as_tuple(const entity_type entt) const noexcept
Returns the object assigned to an entity as a tuple.
void shrink_to_fit() override
Requests the removal of unused capacity.
const_reverse_iterator crbegin() const noexcept
Returns a reverse iterator to the beginning.
basic_storage(basic_storage &&other, const allocator_type &allocator)
Allocator-extended move constructor.
basic_storage(const basic_storage &)=delete
Default copy constructor, deleted on purpose.
iterator begin() noexcept
Returns an iterator to the beginning.
internal::storage_iterator< container_type > iterator
Random access iterator type.
static constexpr deletion_policy storage_policy
Storage deletion policy.
Allocator allocator_type
Allocator type.
constexpr Type make_obj_using_allocator(const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
constexpr std::enable_if_t< std::is_unsigned_v< Type >, Type > fast_mod(const Type value, const std::size_t mod) noexcept
Fast module utility function (powers of two only).
constexpr null_t null
Compile-time constant for null entities.
constexpr tombstone_t tombstone
Compile-time constant for tombstone entities.
constexpr Type * uninitialized_construct_using_allocator(Type *value, const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
constexpr get_t< Type... > get
Variable template for lists of observed elements.
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.
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.
@ ref
Aliasing mode, the object points to a non-const element.
constexpr bool operator>(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr auto to_address(Type &&ptr) noexcept
Unwraps fancy pointers, does nothing otherwise (waiting for C++20).
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Common way to access various properties of components.
static constexpr std::size_t page_size
Page size, default is ENTT_PACKED_PAGE for non-empty types.
static constexpr bool in_place_delete
Pointer stability, default is false.
Utility class to create an iterable object from a pair of iterators.