EnTT  3.5.0
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 "sparse_set.hpp"
12 #include "fwd.hpp"
13 
14 
15 namespace entt {
16 
17 
56 template<typename Entity>
57 class basic_runtime_view {
59  friend class basic_registry<Entity>;
60 
61  using underlying_iterator = typename sparse_set<Entity>::iterator;
62 
63  class view_iterator final {
64  friend class basic_runtime_view<Entity>;
65 
66  view_iterator(const std::vector<const sparse_set<Entity> *> &cpools, const std::vector<const sparse_set<Entity> *> &ignore, underlying_iterator curr) ENTT_NOEXCEPT
67  : pools{&cpools},
68  filter{&ignore},
69  it{curr}
70  {
71  if(it != (*pools)[0]->end() && !valid()) {
72  ++(*this);
73  }
74  }
75 
76  [[nodiscard]] bool valid() const {
77  return std::all_of(pools->begin()++, pools->end(), [entt = *it](const auto *curr) { return curr->contains(entt); })
78  && std::none_of(filter->cbegin(), filter->cend(), [entt = *it](const auto *curr) { return curr && curr->contains(entt); });
79  }
80 
81  public:
82  using difference_type = typename underlying_iterator::difference_type;
83  using value_type = typename underlying_iterator::value_type;
84  using pointer = typename underlying_iterator::pointer;
85  using reference = typename underlying_iterator::reference;
86  using iterator_category = std::bidirectional_iterator_tag;
87 
88  view_iterator() ENTT_NOEXCEPT = default;
89 
90  view_iterator & operator++() {
91  while(++it != (*pools)[0]->end() && !valid());
92  return *this;
93  }
94 
95  view_iterator operator++(int) {
96  view_iterator orig = *this;
97  return ++(*this), orig;
98  }
99 
100  view_iterator & operator--() ENTT_NOEXCEPT {
101  while(--it != (*pools)[0]->begin() && !valid());
102  return *this;
103  }
104 
105  view_iterator operator--(int) ENTT_NOEXCEPT {
106  view_iterator orig = *this;
107  return operator--(), orig;
108  }
109 
110  [[nodiscard]] bool operator==(const view_iterator &other) const ENTT_NOEXCEPT {
111  return other.it == it;
112  }
113 
114  [[nodiscard]] bool operator!=(const view_iterator &other) const ENTT_NOEXCEPT {
115  return !(*this == other);
116  }
117 
118  [[nodiscard]] pointer operator->() const {
119  return it.operator->();
120  }
121 
122  [[nodiscard]] reference operator*() const {
123  return *operator->();
124  }
125 
126  private:
127  const std::vector<const sparse_set<Entity> *> *pools;
128  const std::vector<const sparse_set<Entity> *> *filter;
129  underlying_iterator it;
130  };
131 
132  basic_runtime_view(std::vector<const sparse_set<Entity> *> cpools, std::vector<const sparse_set<Entity> *> epools) ENTT_NOEXCEPT
133  : pools{std::move(cpools)},
134  filter{std::move(epools)}
135  {
136  const auto it = std::min_element(pools.begin(), pools.end(), [](const auto *lhs, const auto *rhs) {
137  return (!lhs && rhs) || (lhs && rhs && lhs->size() < rhs->size());
138  });
139 
140  // brings the best candidate (if any) on front of the vector
141  std::rotate(pools.begin(), it, pools.end());
142  }
143 
144  [[nodiscard]] bool valid() const {
145  return !pools.empty() && pools.front();
146  }
147 
148 public:
150  using entity_type = Entity;
152  using size_type = std::size_t;
154  using iterator = view_iterator;
155 
160  [[nodiscard]] size_type size() const {
161  return valid() ? pools.front()->size() : size_type{};
162  }
163 
168  [[nodiscard]] bool empty() const {
169  return !valid() || pools.front()->empty();
170  }
171 
186  [[nodiscard]] iterator begin() const {
187  return valid() ? iterator{pools, filter, pools[0]->begin()} : iterator{};
188  }
189 
205  [[nodiscard]] iterator end() const {
206  return valid() ? iterator{pools, filter, pools[0]->end()} : iterator{};
207  }
208 
214  [[nodiscard]] bool contains(const entity_type entt) const {
215  return valid() && std::all_of(pools.cbegin(), pools.cend(), [entt](const auto *curr) { return curr->contains(entt); })
216  && std::none_of(filter.cbegin(), filter.cend(), [entt](const auto *curr) { return curr && curr->contains(entt); });
217  }
218 
234  template<typename Func>
235  void each(Func func) const {
236  for(const auto entity: *this) {
237  func(entity);
238  }
239  }
240 
241 private:
242  std::vector<const sparse_set<Entity> *> pools;
243  std::vector<const sparse_set<Entity> *> filter;
244 };
245 
246 
247 }
248 
249 
250 #endif
entt::basic_runtime_view::size
size_type size() const
Estimates the number of entities that have the given components.
Definition: runtime_view.hpp:160
entt::basic_runtime_view::begin
iterator begin() const
Returns an iterator to the first entity that has the given components.
Definition: runtime_view.hpp:186
entt::sparse_set
Basic sparse set implementation.
Definition: sparse_set.hpp:43
entt::basic_runtime_view
Runtime view.
Definition: fwd.hpp:20
entt::sparse_set::iterator
sparse_set_iterator iterator
Random access iterator type.
Definition: sparse_set.hpp:182
entt::basic_runtime_view::each
void each(Func func) const
Iterates entities and applies the given function object to them.
Definition: runtime_view.hpp:235
entt::basic_runtime_view::end
iterator end() const
Returns an iterator that is past the last entity that has the given components.
Definition: runtime_view.hpp:205
entt
EnTT default namespace.
Definition: algorithm.hpp:13
entt::basic_runtime_view::empty
bool empty() const
Checks if the view is definitely empty.
Definition: runtime_view.hpp:168
entt::basic_runtime_view::size_type
std::size_t size_type
Unsigned integer type.
Definition: runtime_view.hpp:152
entt::basic_registry
Fast and reliable entity-component system.
Definition: fwd.hpp:12
entt::basic_runtime_view::entity_type
Entity entity_type
Underlying entity identifier.
Definition: runtime_view.hpp:150
entt::basic_runtime_view::contains
bool contains(const entity_type entt) const
Checks if a view contains an entity.
Definition: runtime_view.hpp:214
entt::operator!=
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition: hashed_string.hpp:227
entt::operator==
constexpr bool operator==(const Entity entity, null_t other) noexcept
Compares a null object and an entity identifier of any type.
Definition: entity.hpp:184
entt::entity
entity
Default entity identifier.
Definition: fwd.hpp:52
entt::basic_runtime_view::iterator
view_iterator iterator
Bidirectional iterator type.
Definition: runtime_view.hpp:154