EnTT  3.2.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 <cassert>
7 #include <vector>
8 #include <utility>
9 #include <algorithm>
10 #include <type_traits>
11 #include "../config/config.h"
12 #include "sparse_set.hpp"
13 #include "entity.hpp"
14 #include "fwd.hpp"
15 
16 
17 namespace entt {
18 
19 
58 template<typename Entity>
59 class basic_runtime_view {
61  friend class basic_registry<Entity>;
62 
63  using underlying_iterator_type = typename sparse_set<Entity>::iterator_type;
64 
65  class iterator {
66  friend class basic_runtime_view<Entity>;
67 
68  iterator(underlying_iterator_type first, underlying_iterator_type last, const sparse_set<Entity> * const *others, const sparse_set<Entity> * const *length) ENTT_NOEXCEPT
69  : begin{first},
70  end{last},
71  from{others},
72  to{length}
73  {
74  if(begin != end && !valid()) {
75  ++(*this);
76  }
77  }
78 
79  bool valid() const ENTT_NOEXCEPT {
80  return std::all_of(from, to, [entt = *begin](const auto *view) {
81  return view->has(entt);
82  });
83  }
84 
85  public:
86  using difference_type = typename underlying_iterator_type::difference_type;
87  using value_type = typename underlying_iterator_type::value_type;
88  using pointer = typename underlying_iterator_type::pointer;
89  using reference = typename underlying_iterator_type::reference;
90  using iterator_category = std::forward_iterator_tag;
91 
92  iterator() ENTT_NOEXCEPT = default;
93 
94  iterator & operator++() ENTT_NOEXCEPT {
95  return (++begin != end && !valid()) ? ++(*this) : *this;
96  }
97 
98  iterator operator++(int) ENTT_NOEXCEPT {
99  iterator orig = *this;
100  return ++(*this), orig;
101  }
102 
103  bool operator==(const iterator &other) const ENTT_NOEXCEPT {
104  return other.begin == begin;
105  }
106 
107  bool operator!=(const iterator &other) const ENTT_NOEXCEPT {
108  return !(*this == other);
109  }
110 
111  pointer operator->() const ENTT_NOEXCEPT {
112  return begin.operator->();
113  }
114 
115  reference operator*() const ENTT_NOEXCEPT {
116  return *operator->();
117  }
118 
119  private:
120  underlying_iterator_type begin;
121  underlying_iterator_type end;
122  const sparse_set<Entity> * const *from;
123  const sparse_set<Entity> * const *to;
124  };
125 
126  basic_runtime_view(std::vector<const sparse_set<Entity> *> others) ENTT_NOEXCEPT
127  : pools{std::move(others)}
128  {
129  const auto it = std::min_element(pools.begin(), pools.end(), [](const auto *lhs, const auto *rhs) {
130  return (!lhs && rhs) || (lhs && rhs && lhs->size() < rhs->size());
131  });
132 
133  // brings the best candidate (if any) on front of the vector
134  std::rotate(pools.begin(), it, pools.end());
135  }
136 
137  bool valid() const ENTT_NOEXCEPT {
138  return !pools.empty() && pools.front();
139  }
140 
141 public:
143  using entity_type = Entity;
145  using size_type = std::size_t;
147  using iterator_type = iterator;
148 
153  size_type size() const ENTT_NOEXCEPT {
154  return valid() ? pools.front()->size() : size_type{};
155  }
156 
161  bool empty() const ENTT_NOEXCEPT {
162  return !valid() || pools.front()->empty();
163  }
164 
179  iterator_type begin() const ENTT_NOEXCEPT {
180  iterator_type it{};
181 
182  if(valid()) {
183  const auto &pool = *pools.front();
184  const auto * const *data = pools.data();
185  it = { pool.begin(), pool.end(), data + 1, data + pools.size() };
186  }
187 
188  return it;
189  }
190 
206  iterator_type end() const ENTT_NOEXCEPT {
207  iterator_type it{};
208 
209  if(valid()) {
210  const auto &pool = *pools.front();
211  it = { pool.end(), pool.end(), nullptr, nullptr };
212  }
213 
214  return it;
215  }
216 
222  bool contains(const entity_type entt) const ENTT_NOEXCEPT {
223  return valid() && std::all_of(pools.cbegin(), pools.cend(), [entt](const auto *view) {
224  return view->find(entt) != view->end();
225  });
226  }
227 
243  template<typename Func>
244  void each(Func func) const {
245  std::for_each(begin(), end(), func);
246  }
247 
248 private:
249  std::vector<const sparse_set<Entity> *> pools;
250 };
251 
252 
253 }
254 
255 
256 #endif // ENTT_ENTITY_RUNTIME_VIEW_HPP
entt::basic_runtime_view::iterator_type
iterator iterator_type
Input iterator type.
Definition: runtime_view.hpp:147
entt::basic_runtime_view::size
size_type size() const ENTT_NOEXCEPT
Estimates the number of entities that have the given components.
Definition: runtime_view.hpp:153
entt::basic_runtime_view::begin
iterator_type begin() const ENTT_NOEXCEPT
Returns an iterator to the first entity that has the given components.
Definition: runtime_view.hpp:179
entt::sparse_set
Basic sparse set implementation.
Definition: sparse_set.hpp:50
entt::basic_runtime_view
Runtime view.
Definition: fwd.hpp:21
entt::view
basic_view< entity, Types... > view
Alias declaration for the most common use case.
Definition: fwd.hpp:76
entt::basic_runtime_view::contains
bool contains(const entity_type entt) const ENTT_NOEXCEPT
Checks if a view contains an entity.
Definition: runtime_view.hpp:222
entt::basic_runtime_view::each
void each(Func func) const
Iterates entities and applies the given function object to them.
Definition: runtime_view.hpp:244
entt
EnTT default namespace.
Definition: algorithm.hpp:12
entt::basic_runtime_view::end
iterator_type end() const ENTT_NOEXCEPT
Returns an iterator that is past the last entity that has the given components.
Definition: runtime_view.hpp:206
entt::basic_runtime_view::empty
bool empty() const ENTT_NOEXCEPT
Checks if the view is definitely empty.
Definition: runtime_view.hpp:161
entt::basic_runtime_view::size_type
std::size_t size_type
Unsigned integer type.
Definition: runtime_view.hpp:145
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:217
entt::basic_registry
Fast and reliable entity-component system.
Definition: fwd.hpp:13
entt::basic_runtime_view::entity_type
Entity entity_type
Underlying entity identifier.
Definition: runtime_view.hpp:143
entt::basic_view
View.
Definition: fwd.hpp:17
entt::sparse_set::iterator_type
iterator iterator_type
Random access iterator type.
Definition: sparse_set.hpp:182