EnTT  3.8.1
runtime_view.hpp
1 #ifndef ENTT_ENTITY_RUNTIME_VIEW_HPP
2 #define ENTT_ENTITY_RUNTIME_VIEW_HPP
3 
4 
5 #include <iterator>
6 #include <vector>
7 #include <utility>
8 #include <algorithm>
9 #include <type_traits>
10 #include "../config/config.h"
11 #include "entity.hpp"
12 #include "sparse_set.hpp"
13 #include "fwd.hpp"
14 
15 
16 namespace entt {
17 
18 
57 template<typename Entity>
58 class basic_runtime_view final {
60  using underlying_iterator = typename basic_common_type::iterator;
61 
62  class view_iterator final {
63  [[nodiscard]] bool valid() const {
64  const auto entt = *it;
65 
66  return (!stable_storage || (entt != tombstone))
67  && std::all_of(pools->begin()++, pools->end(), [entt](const auto *curr) { return curr->contains(entt); })
68  && std::none_of(filter->cbegin(), filter->cend(), [entt](const auto *curr) { return curr && curr->contains(entt); });
69  }
70 
71  public:
72  using difference_type = typename underlying_iterator::difference_type;
73  using value_type = typename underlying_iterator::value_type;
74  using pointer = typename underlying_iterator::pointer;
75  using reference = typename underlying_iterator::reference;
76  using iterator_category = std::bidirectional_iterator_tag;
77 
78  view_iterator() ENTT_NOEXCEPT = default;
79 
80  view_iterator(const std::vector<const basic_common_type *> &cpools, const std::vector<const basic_common_type *> &ignore, underlying_iterator curr) ENTT_NOEXCEPT
81  : pools{&cpools},
82  filter{&ignore},
83  it{curr},
84  stable_storage{std::any_of(pools->cbegin(), pools->cend(), [](const basic_common_type *cpool) { return (cpool->policy() == deletion_policy::in_place); })}
85  {
86  if(it != (*pools)[0]->end() && !valid()) {
87  ++(*this);
88  }
89  }
90 
91  view_iterator & operator++() {
92  while(++it != (*pools)[0]->end() && !valid());
93  return *this;
94  }
95 
96  view_iterator operator++(int) {
97  view_iterator orig = *this;
98  return ++(*this), orig;
99  }
100 
101  view_iterator & operator--() ENTT_NOEXCEPT {
102  while(--it != (*pools)[0]->begin() && !valid());
103  return *this;
104  }
105 
106  view_iterator operator--(int) ENTT_NOEXCEPT {
107  view_iterator orig = *this;
108  return operator--(), orig;
109  }
110 
111  [[nodiscard]] bool operator==(const view_iterator &other) const ENTT_NOEXCEPT {
112  return other.it == it;
113  }
114 
115  [[nodiscard]] bool operator!=(const view_iterator &other) const ENTT_NOEXCEPT {
116  return !(*this == other);
117  }
118 
119  [[nodiscard]] pointer operator->() const {
120  return it.operator->();
121  }
122 
123  [[nodiscard]] reference operator*() const {
124  return *operator->();
125  }
126 
127  private:
128  const std::vector<const basic_common_type *> *pools;
129  const std::vector<const basic_common_type *> *filter;
130  underlying_iterator it;
131  bool stable_storage;
132  };
133 
134  [[nodiscard]] bool valid() const {
135  return !pools.empty() && pools.front();
136  }
137 
138 public:
140  using entity_type = Entity;
142  using size_type = std::size_t;
144  using iterator = view_iterator;
145 
147  basic_runtime_view() ENTT_NOEXCEPT
148  : pools{},
149  filter{}
150  {}
151 
157  basic_runtime_view(std::vector<const basic_common_type *> cpools, std::vector<const basic_common_type *> epools) ENTT_NOEXCEPT
158  : pools{std::move(cpools)},
159  filter{std::move(epools)}
160  {
161  // brings the best candidate (if any) on front of the vector
162  std::rotate(pools.begin(), std::min_element(pools.begin(), pools.end(), [](const auto *lhs, const auto *rhs) {
163  return (!lhs && rhs) || (lhs && rhs && lhs->size() < rhs->size());
164  }), pools.end());
165  }
166 
171  [[nodiscard]] size_type size_hint() const {
172  return valid() ? pools.front()->size() : size_type{};
173  }
174 
185  [[nodiscard]] iterator begin() const {
186  return valid() ? iterator{pools, filter, pools[0]->begin()} : iterator{};
187  }
188 
200  [[nodiscard]] iterator end() const {
201  return valid() ? iterator{pools, filter, pools[0]->end()} : iterator{};
202  }
203 
209  [[nodiscard]] bool contains(const entity_type entt) const {
210  return valid() && std::all_of(pools.cbegin(), pools.cend(), [entt](const auto *curr) { return curr->contains(entt); })
211  && std::none_of(filter.cbegin(), filter.cend(), [entt](const auto *curr) { return curr && curr->contains(entt); });
212  }
213 
229  template<typename Func>
230  void each(Func func) const {
231  for(const auto entity: *this) {
232  func(entity);
233  }
234  }
235 
236 private:
237  std::vector<const basic_common_type *> pools;
238  std::vector<const basic_common_type *> filter;
239 };
240 
241 
242 }
243 
244 
245 #endif
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.
view_iterator iterator
Bidirectional iterator type.
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.
Definition: sparse_set.hpp:53
sparse_set_iterator iterator
Random access iterator type.
Definition: sparse_set.hpp:286
EnTT default namespace.
Definition: algorithm.hpp:13
entity
Default entity identifier.
Definition: fwd.hpp:61
constexpr tombstone_t tombstone
Compile-time constant for tombstone entities.
Definition: entity.hpp:345
bool operator!=(const basic_any< Len, Align > &lhs, const basic_any< Len, Align > &rhs)
Checks if two wrappers differ in their content.
Definition: any.hpp:368
constexpr bool operator==(const Entity entity, const null_t other)
Compares a null object and an entity identifier of any type.
Definition: entity.hpp:219