1#ifndef ENTT_ENTITY_RUNTIME_VIEW_HPP
2#define ENTT_ENTITY_RUNTIME_VIEW_HPP
4#include "../stl/algorithm.hpp"
5#include "../stl/cstddef.hpp"
6#include "../stl/iterator.hpp"
7#include "../stl/utility.hpp"
8#include "../stl/vector.hpp"
18class runtime_view_iterator final {
19 using iterator_type = Set::iterator;
20 using iterator_traits = stl::iterator_traits<iterator_type>;
22 [[nodiscard]]
bool valid()
const {
23 return (!tombstone_check || *it != tombstone)
24 && stl::all_of(++pools->begin(), pools->end(), [entt = *it](
const auto *curr) { return curr->contains(entt); })
25 && stl::none_of(filter->cbegin(), filter->cend(), [entt = *it](
const auto *curr) { return curr && curr->contains(entt); });
29 using value_type = iterator_traits::value_type;
30 using pointer = iterator_traits::pointer;
31 using reference = iterator_traits::reference;
32 using difference_type = iterator_traits::difference_type;
33 using iterator_category = stl::bidirectional_iterator_tag;
35 constexpr runtime_view_iterator() noexcept
41 runtime_view_iterator(
const stl::vector<Set *> &cpools, iterator_type curr,
const stl::vector<Set *> &ignore) noexcept
45 tombstone_check{pools->size() == 1u && (*pools)[0u]->policy() == deletion_policy::in_place} {
46 if(it != (*pools)[0]->end() && !valid()) {
51 runtime_view_iterator &operator++() {
53 for(
const auto last = (*pools)[0]->end(); it != last && !valid(); ++it) {}
57 runtime_view_iterator operator++(
int) {
58 const runtime_view_iterator orig = *
this;
59 return ++(*this), orig;
62 runtime_view_iterator &operator--() {
64 for(
const auto first = (*pools)[0]->begin(); it != first && !valid(); --it) {}
68 runtime_view_iterator operator--(
int) {
69 const runtime_view_iterator orig = *
this;
70 return operator--(), orig;
73 [[nodiscard]] pointer operator->() const noexcept {
74 return it.operator->();
77 [[nodiscard]] reference operator*() const noexcept {
81 [[nodiscard]]
constexpr bool operator==(
const runtime_view_iterator &other)
const noexcept {
82 return it == other.it;
86 const stl::vector<Set *> *pools;
87 const stl::vector<Set *> *filter;
118template<
typename Type,
typename Allocator>
120 using alloc_traits = stl::allocator_traits<Allocator>;
121 static_assert(stl::is_same_v<typename alloc_traits::value_type, Type *>,
"Invalid value type");
122 using container_type = stl::vector<Type *, Allocator>;
124 [[nodiscard]]
auto offset()
const noexcept {
125 ENTT_ASSERT(!pools.empty(),
"Invalid view");
126 const auto &leading = *pools.front();
142 using iterator = internal::runtime_view_iterator<common_type>;
165 : pools{other.pools, allocator},
166 filter{other.filter, allocator} {}
177 : pools{stl::move(other.pools), allocator},
178 filter{stl::move(other.filter), allocator} {}
201 swap(pools, other.pools);
202 swap(filter, other.filter);
210 return pools.get_allocator();
225 if(pools.empty() || !(base.
size() < pools.front()->size())) {
226 pools.push_back(&base);
228 pools.push_back(stl::exchange(pools.front(), &base));
240 filter.push_back(&base);
249 return pools.empty() ?
size_type{} : offset();
271 return pools.empty() ?
iterator{} :
iterator{pools, pools.front()->end(), filter};
278 [[nodiscard]]
explicit operator bool() const noexcept {
279 return !(pools.empty() && filter.empty());
288 return !pools.empty()
289 && stl::all_of(pools.cbegin(), pools.cend(), [
entt](
const auto *curr) { return curr->contains(entt); })
290 && stl::none_of(filter.cbegin(), filter.cend(), [
entt](
const auto *curr) { return curr && curr->contains(entt); })
291 && pools.front()->index(
entt) < offset();
308 template<
typename Func>
310 for(
const auto entity: *
this) {
316 container_type pools;
317 container_type filter;
basic_runtime_view(const basic_runtime_view &)=default
Default copy constructor.
iterator begin() const
Returns an iterator to the first entity that has the given elements.
basic_runtime_view() noexcept
Default constructor to use to create empty, invalid views.
basic_runtime_view(const allocator_type &allocator)
Constructs an empty, invalid view with a given allocator.
internal::runtime_view_iterator< common_type > iterator
basic_runtime_view & operator=(const basic_runtime_view &)=default
Default copy assignment operator.
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
stl::ptrdiff_t difference_type
~basic_runtime_view()=default
Default destructor.
bool contains(const entity_type entt) const
Checks if a view contains an entity.
void each(Func func) const
Iterates entities and applies the given function object to them.
basic_runtime_view & exclude(common_type &base)
Adds an opaque storage object as a filter of a runtime view.
sparse_set::entity_type entity_type
void clear()
Clears the view.
iterator end() const
Returns an iterator that is past the last entity that has the given elements.
basic_runtime_view & operator=(basic_runtime_view &&) noexcept=default
Default move assignment operator.
size_type size_hint() const
Estimates the number of entities iterated by the view.
basic_runtime_view(const basic_runtime_view &other, const allocator_type &allocator)
Allocator-extended copy constructor.
void swap(basic_runtime_view &other) noexcept
basic_runtime_view & iterate(common_type &base)
Appends an opaque storage object to a runtime view.
basic_runtime_view(basic_runtime_view &&) noexcept=default
Default move constructor.
size_type size() const noexcept
Returns the number of elements in a sparse set.
entity
Default entity identifier.
@ swap_only
Swap-only deletion policy.