1 #ifndef ENTT_ENTITY_RUNTIME_VIEW_HPP
2 #define ENTT_ENTITY_RUNTIME_VIEW_HPP
9 #include "../config/config.h"
12 #include "sparse_set.hpp"
23 template<
typename Set>
24 class runtime_view_iterator final {
25 using iterator_type =
typename Set::iterator;
27 [[nodiscard]]
bool valid()
const {
28 return (!tombstone_check || *it !=
tombstone)
29 && std::all_of(++pools->begin(), pools->end(), [
entt = *it](
const auto *curr) { return curr->contains(entt); })
30 && std::none_of(filter->cbegin(), filter->cend(), [
entt = *it](
const auto *curr) { return curr && curr->contains(entt); });
34 using difference_type =
typename iterator_type::difference_type;
35 using value_type =
typename iterator_type::value_type;
36 using pointer =
typename iterator_type::pointer;
37 using reference =
typename iterator_type::reference;
38 using iterator_category = std::bidirectional_iterator_tag;
40 runtime_view_iterator() ENTT_NOEXCEPT
46 runtime_view_iterator(
const std::vector<const Set *> &cpools,
const std::vector<const Set *> &ignore, iterator_type curr) ENTT_NOEXCEPT
51 if(it != (*pools)[0]->end() && !valid()) {
56 runtime_view_iterator &operator++() {
57 while(++it != (*pools)[0]->end() && !valid()) {}
61 runtime_view_iterator operator++(
int) {
62 runtime_view_iterator orig = *
this;
63 return ++(*this), orig;
66 runtime_view_iterator &operator--() {
67 while(--it != (*pools)[0]->begin() && !valid()) {}
71 runtime_view_iterator operator--(
int) {
72 runtime_view_iterator orig = *
this;
73 return operator--(), orig;
76 [[nodiscard]] pointer operator->() const ENTT_NOEXCEPT {
77 return it.operator->();
80 [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
84 [[nodiscard]]
bool operator==(
const runtime_view_iterator &other)
const ENTT_NOEXCEPT {
85 return it == other.it;
88 [[nodiscard]]
bool operator!=(
const runtime_view_iterator &other)
const ENTT_NOEXCEPT {
89 return !(*
this == other);
93 const std::vector<const Set *> *pools;
94 const std::vector<const Set *> *filter;
113 struct basic_runtime_view;
154 template<
typename Entity,
typename Allocator>
163 using iterator = internal::runtime_view_iterator<base_type>;
176 if(pools.empty() || !(base.
size() < pools[0u]->size())) {
177 pools.push_back(&base);
179 pools.push_back(std::exchange(pools[0u], &base));
191 filter.push_back(&base);
200 return pools.empty() ?
size_type{} : pools.front()->size();
214 return pools.empty() ?
iterator{} :
iterator{pools, filter, pools[0]->begin()};
229 return pools.empty() ?
iterator{} :
iterator{pools, filter, pools[0]->end()};
238 return !pools.empty()
239 && std::all_of(pools.cbegin(), pools.cend(), [
entt](
const auto *curr) { return curr->contains(entt); })
240 && std::none_of(filter.cbegin(), filter.cend(), [
entt](
const auto *curr) { return curr && curr->contains(entt); });
258 template<
typename Func>
260 for(
const auto entity: *
this) {
266 std::vector<const base_type *> pools;
267 std::vector<const base_type *> filter;
Basic sparse set implementation.
size_type size() const
Returns the number of elements in a sparse set.
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs)
Compares two hashed strings.
entity
Default entity identifier.
constexpr tombstone_t tombstone
Compile-time constant for tombstone entities.
@ in_place
In-place deletion policy.
bool operator!=(const basic_any< Len, Align > &lhs, const basic_any< Len, Align > &rhs)
Checks if two wrappers differ in their content.
bool contains(const entity_type entt) const
Checks if a view contains an entity.
iterator begin() const
Returns an iterator to the first entity that has the given components.
std::size_t size_type
Unsigned integer type.
basic_runtime_view()
Default constructor to use to create empty, invalid views.
size_type size_hint() const
Estimates the number of entities iterated by the view.
void each(Func func) const
Iterates entities and applies the given function object to them.
iterator end() const
Returns an iterator that is past the last entity that has the given components.
Entity entity_type
Underlying entity identifier.
basic_runtime_view & exclude(const base_type &base)
Adds an opaque storage object as a filter of a runtime view.
basic_runtime_view & iterate(const base_type &base)
Appends an opaque storage object to a runtime view.
internal::runtime_view_iterator< base_type > iterator
Bidirectional iterator type.
Runtime view implementation.