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 Type>
24 class runtime_view_iterator final {
25 [[nodiscard]]
bool valid()
const {
26 return std::all_of(pools->begin(), pools->end(), [
entt = *it](
const auto *curr) { return curr->contains(entt); })
27 && std::none_of(filter->cbegin(), filter->cend(), [
entt = *it](
const auto *curr) { return curr && curr->contains(entt); });
31 using iterator_type =
typename Type::iterator;
32 using difference_type =
typename iterator_type::difference_type;
33 using value_type =
typename iterator_type::value_type;
34 using pointer =
typename iterator_type::pointer;
35 using reference =
typename iterator_type::reference;
36 using iterator_category = std::bidirectional_iterator_tag;
38 runtime_view_iterator() ENTT_NOEXCEPT = default;
40 runtime_view_iterator(const std::vector<const Type *> &cpools, const std::vector<const Type *> &ignore, iterator_type curr) ENTT_NOEXCEPT
44 if(it != (*pools)[0]->end() && !valid()) {
49 runtime_view_iterator &operator++() {
50 while(++it != (*pools)[0]->end() && !valid()) {}
54 runtime_view_iterator operator++(
int) {
55 runtime_view_iterator orig = *
this;
56 return ++(*this), orig;
59 runtime_view_iterator &operator--() ENTT_NOEXCEPT {
60 while(--it != (*pools)[0]->begin() && !valid()) {}
64 runtime_view_iterator operator--(
int) ENTT_NOEXCEPT {
65 runtime_view_iterator orig = *
this;
66 return operator--(), orig;
69 [[nodiscard]] pointer operator->()
const {
70 return it.operator->();
73 [[nodiscard]] reference operator*()
const {
77 [[nodiscard]]
bool operator==(
const runtime_view_iterator &other)
const ENTT_NOEXCEPT {
78 return it == other.it;
81 [[nodiscard]]
bool operator!=(
const runtime_view_iterator &other)
const ENTT_NOEXCEPT {
82 return !(*
this == other);
86 const std::vector<const Type *> *pools;
87 const std::vector<const Type *> *filter;
136 template<
typename Entity>
140 [[nodiscard]]
bool valid()
const {
141 return !pools.empty() && pools.front();
150 using iterator = internal::runtime_view_iterator<basic_common_type>;
162 basic_runtime_view(std::vector<const basic_common_type *> cpools, std::vector<const basic_common_type *> epools) ENTT_NOEXCEPT
163 : pools{std::move(cpools)},
164 filter{std::move(epools)} {
165 auto candidate = std::min_element(pools.begin(), pools.end(), [](
const auto *lhs,
const auto *rhs) {
166 return (!lhs && rhs) || (lhs && rhs && lhs->size() < rhs->size());
170 std::rotate(pools.begin(), candidate, pools.end());
178 return valid() ? pools.front()->size() :
size_type{};
216 return valid() && std::all_of(pools.cbegin(), pools.cend(), [
entt](
const auto *curr) { return curr->contains(entt); })
217 && std::none_of(filter.cbegin(), filter.cend(), [
entt](
const auto *curr) { return curr && curr->contains(entt); });
235 template<
typename Func>
237 for(
const auto entity: *
this) {
243 std::vector<const basic_common_type *> pools;
244 std::vector<const basic_common_type *> filter;
internal::runtime_view_iterator< basic_common_type > iterator
Bidirectional iterator type.
basic_runtime_view()
Default constructor to use to create empty, invalid views.
basic_runtime_view(std::vector< const basic_common_type * > cpools, std::vector< const basic_common_type * > epools)
Constructs a runtime view from a set of storage classes.
iterator end() const
Returns an iterator that is past the last entity that has the given components.
void each(Func func) const
Iterates entities and applies the given function object to them.
Entity entity_type
Underlying entity identifier.
iterator begin() const
Returns an iterator to the first entity that has the given components.
std::size_t size_type
Unsigned integer type.
bool contains(const entity_type entt) const
Checks if a view contains an entity.
size_type size_hint() const
Estimates the number of entities iterated by the view.
Basic sparse set implementation.
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs)
Compares two hashed strings.
entity
Default entity identifier.
bool operator!=(const basic_any< Len, Align > &lhs, const basic_any< Len, Align > &rhs)
Checks if two wrappers differ in their content.