EnTT  3.3.2
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 "entity.hpp"
13 #include "fwd.hpp"
14 
15 
16 namespace entt {
17 
18 
57 template<typename Entity>
58 class basic_runtime_view {
60  friend class basic_registry<Entity>;
61 
62  using underlying_iterator_type = typename sparse_set<Entity>::iterator_type;
63 
64  class iterator {
65  friend class basic_runtime_view<Entity>;
66 
67  using direct_type = std::vector<const sparse_set<Entity> *>;
68 
69  iterator(const direct_type *all, underlying_iterator_type curr) ENTT_NOEXCEPT
70  : pools{all},
71  it{curr}
72  {
73  if(it != (*pools)[0]->end() && !valid()) {
74  ++(*this);
75  }
76  }
77 
78  bool valid() const {
79  return std::all_of(pools->begin()++, pools->end(), [entt = *it](const auto *curr) {
80  return curr->has(entt);
81  });
82  }
83 
84  public:
85  using difference_type = typename underlying_iterator_type::difference_type;
86  using value_type = typename underlying_iterator_type::value_type;
87  using pointer = typename underlying_iterator_type::pointer;
88  using reference = typename underlying_iterator_type::reference;
89  using iterator_category = std::bidirectional_iterator_tag;
90 
91  iterator() ENTT_NOEXCEPT = default;
92 
93  iterator & operator++() {
94  while(++it != (*pools)[0]->end() && !valid());
95  return *this;
96  }
97 
98  iterator operator++(int) {
99  iterator orig = *this;
100  return operator++(), orig;
101  }
102 
103  iterator & operator--() ENTT_NOEXCEPT {
104  while(--it != (*pools)[0]->begin() && !valid());
105  return *this;
106  }
107 
108  iterator operator--(int) ENTT_NOEXCEPT {
109  iterator orig = *this;
110  return operator--(), orig;
111  }
112 
113  bool operator==(const iterator &other) const ENTT_NOEXCEPT {
114  return other.it == it;
115  }
116 
117  bool operator!=(const iterator &other) const ENTT_NOEXCEPT {
118  return !(*this == other);
119  }
120 
121  pointer operator->() const {
122  return it.operator->();
123  }
124 
125  reference operator*() const {
126  return *operator->();
127  }
128 
129  private:
130  const direct_type *pools;
131  underlying_iterator_type it;
132  };
133 
134  basic_runtime_view(std::vector<const sparse_set<Entity> *> others) ENTT_NOEXCEPT
135  : pools{std::move(others)}
136  {
137  const auto it = std::min_element(pools.begin(), pools.end(), [](const auto *lhs, const auto *rhs) {
138  return (!lhs && rhs) || (lhs && rhs && lhs->size() < rhs->size());
139  });
140 
141  // brings the best candidate (if any) on front of the vector
142  std::rotate(pools.begin(), it, pools.end());
143  }
144 
145  bool valid() const {
146  return !pools.empty() && pools.front();
147  }
148 
149 public:
151  using entity_type = Entity;
153  using size_type = std::size_t;
155  using iterator_type = iterator;
156 
161  size_type size() const {
162  return valid() ? pools.front()->size() : size_type{};
163  }
164 
169  bool empty() const {
170  return !valid() || pools.front()->empty();
171  }
172 
188  iterator_type it{};
189 
190  if(valid()) {
191  it = { &pools, pools[0]->begin() };
192  }
193 
194  return it;
195  }
196 
212  iterator_type end() const {
213  iterator_type it{};
214 
215  if(valid()) {
216  it = { &pools, pools[0]->end() };
217  }
218 
219  return it;
220  }
221 
227  bool contains(const entity_type entt) const {
228  return valid() && std::all_of(pools.cbegin(), pools.cend(), [entt](const auto *view) {
229  return view->find(entt) != view->end();
230  });
231  }
232 
248  template<typename Func>
249  void each(Func func) const {
250  for(const auto entity: *this) {
251  func(entity);
252  }
253  }
254 
255 private:
256  std::vector<const sparse_set<Entity> *> pools;
257 };
258 
259 
260 }
261 
262 
263 #endif
entt::basic_runtime_view::iterator_type
iterator iterator_type
Input iterator type.
Definition: runtime_view.hpp:155
entt::basic_runtime_view::size
size_type size() const
Estimates the number of entities that have the given components.
Definition: runtime_view.hpp:161
entt::basic_runtime_view::end
iterator_type end() const
Returns an iterator that is past the last entity that has the given components.
Definition: runtime_view.hpp:212
entt::basic_runtime_view::begin
iterator_type begin() const
Returns an iterator to the first entity that has the given components.
Definition: runtime_view.hpp:187
entt::basic_runtime_view
Runtime view.
Definition: fwd.hpp:54
entt::view
basic_view< entity, Types... > view
Alias declaration for the most common use case.
Definition: fwd.hpp:106
entt::basic_runtime_view::each
void each(Func func) const
Iterates entities and applies the given function object to them.
Definition: runtime_view.hpp:249
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:169
entt::basic_runtime_view::size_type
std::size_t size_type
Unsigned integer type.
Definition: runtime_view.hpp:153
entt::operator!=
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) ENTT_NOEXCEPT
Compares two hashed strings.
Definition: hashed_string.hpp:226
entt::basic_registry
Fast and reliable entity-component system.
Definition: fwd.hpp:46
entt::basic_runtime_view::entity_type
Entity entity_type
Underlying entity identifier.
Definition: runtime_view.hpp:151
entt::basic_runtime_view::contains
bool contains(const entity_type entt) const
Checks if a view contains an entity.
Definition: runtime_view.hpp:227
entt::basic_view
View.
Definition: fwd.hpp:50
entt::sparse_set::iterator_type
iterator iterator_type
Random access iterator type.
Definition: sparse_set.hpp:184