runtime_view.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #ifndef ENTT_ENTITY_RUNTIME_VIEW_HPP
  2. #define ENTT_ENTITY_RUNTIME_VIEW_HPP
  3. #include <algorithm>
  4. #include <cstddef>
  5. #include "../stl/iterator.hpp"
  6. #include "../stl/utility.hpp"
  7. #include "../stl/vector.hpp"
  8. #include "entity.hpp"
  9. #include "fwd.hpp"
  10. namespace entt {
  11. /*! @cond ENTT_INTERNAL */
  12. namespace internal {
  13. template<typename Set>
  14. class runtime_view_iterator final {
  15. using iterator_type = Set::iterator;
  16. using iterator_traits = stl::iterator_traits<iterator_type>;
  17. [[nodiscard]] bool valid() const {
  18. return (!tombstone_check || *it != tombstone)
  19. && std::all_of(++pools->begin(), pools->end(), [entt = *it](const auto *curr) { return curr->contains(entt); })
  20. && std::none_of(filter->cbegin(), filter->cend(), [entt = *it](const auto *curr) { return curr && curr->contains(entt); });
  21. }
  22. public:
  23. using value_type = iterator_traits::value_type;
  24. using pointer = iterator_traits::pointer;
  25. using reference = iterator_traits::reference;
  26. using difference_type = iterator_traits::difference_type;
  27. using iterator_category = stl::bidirectional_iterator_tag;
  28. constexpr runtime_view_iterator() noexcept
  29. : pools{},
  30. filter{},
  31. it{},
  32. tombstone_check{} {}
  33. runtime_view_iterator(const stl::vector<Set *> &cpools, iterator_type curr, const stl::vector<Set *> &ignore) noexcept
  34. : pools{&cpools},
  35. filter{&ignore},
  36. it{curr},
  37. tombstone_check{pools->size() == 1u && (*pools)[0u]->policy() == deletion_policy::in_place} {
  38. if(it != (*pools)[0]->end() && !valid()) {
  39. ++(*this);
  40. }
  41. }
  42. runtime_view_iterator &operator++() {
  43. ++it;
  44. for(const auto last = (*pools)[0]->end(); it != last && !valid(); ++it) {}
  45. return *this;
  46. }
  47. runtime_view_iterator operator++(int) {
  48. const runtime_view_iterator orig = *this;
  49. return ++(*this), orig;
  50. }
  51. runtime_view_iterator &operator--() {
  52. --it;
  53. for(const auto first = (*pools)[0]->begin(); it != first && !valid(); --it) {}
  54. return *this;
  55. }
  56. runtime_view_iterator operator--(int) {
  57. const runtime_view_iterator orig = *this;
  58. return operator--(), orig;
  59. }
  60. [[nodiscard]] pointer operator->() const noexcept {
  61. return it.operator->();
  62. }
  63. [[nodiscard]] reference operator*() const noexcept {
  64. return *operator->();
  65. }
  66. [[nodiscard]] constexpr bool operator==(const runtime_view_iterator &other) const noexcept {
  67. return it == other.it;
  68. }
  69. private:
  70. const stl::vector<Set *> *pools;
  71. const stl::vector<Set *> *filter;
  72. iterator_type it;
  73. bool tombstone_check;
  74. };
  75. } // namespace internal
  76. /*! @endcond */
  77. /**
  78. * @brief Generic runtime view.
  79. *
  80. * Runtime views iterate over those entities that are at least in the given
  81. * storage. During initialization, a runtime view looks at the number of
  82. * entities available for each element and uses the smallest set in order to get
  83. * a performance boost when iterating.
  84. *
  85. * @b Important
  86. *
  87. * Iterators aren't invalidated if:
  88. *
  89. * * New elements are added to the storage.
  90. * * The entity currently pointed is modified (for example, elements are added
  91. * or removed from it).
  92. * * The entity currently pointed is destroyed.
  93. *
  94. * In all other cases, modifying the storage iterated by the view in any way
  95. * invalidates all the iterators.
  96. *
  97. * @tparam Type Common base type.
  98. * @tparam Allocator Type of allocator used to manage memory and elements.
  99. */
  100. template<typename Type, typename Allocator>
  101. class basic_runtime_view {
  102. using alloc_traits = std::allocator_traits<Allocator>;
  103. static_assert(stl::is_same_v<typename alloc_traits::value_type, Type *>, "Invalid value type");
  104. using container_type = stl::vector<Type *, Allocator>;
  105. [[nodiscard]] auto offset() const noexcept {
  106. ENTT_ASSERT(!pools.empty(), "Invalid view");
  107. const auto &leading = *pools.front();
  108. return (leading.policy() == deletion_policy::swap_only) ? leading.free_list() : leading.size();
  109. }
  110. public:
  111. /*! @brief Allocator type. */
  112. using allocator_type = Allocator;
  113. /*! @brief Underlying entity identifier. */
  114. using entity_type = Type::entity_type;
  115. /*! @brief Unsigned integer type. */
  116. using size_type = std::size_t;
  117. /*! @brief Signed integer type. */
  118. using difference_type = std::ptrdiff_t;
  119. /*! @brief Common type among all storage types. */
  120. using common_type = Type;
  121. /*! @brief Bidirectional iterator type. */
  122. using iterator = internal::runtime_view_iterator<common_type>;
  123. /*! @brief Default constructor to use to create empty, invalid views. */
  124. basic_runtime_view() noexcept
  125. : basic_runtime_view{allocator_type{}} {}
  126. /**
  127. * @brief Constructs an empty, invalid view with a given allocator.
  128. * @param allocator The allocator to use.
  129. */
  130. explicit basic_runtime_view(const allocator_type &allocator)
  131. : pools{allocator},
  132. filter{allocator} {}
  133. /*! @brief Default copy constructor. */
  134. basic_runtime_view(const basic_runtime_view &) = default;
  135. /**
  136. * @brief Allocator-extended copy constructor.
  137. * @param other The instance to copy from.
  138. * @param allocator The allocator to use.
  139. */
  140. basic_runtime_view(const basic_runtime_view &other, const allocator_type &allocator)
  141. : pools{other.pools, allocator},
  142. filter{other.filter, allocator} {}
  143. /*! @brief Default move constructor. */
  144. basic_runtime_view(basic_runtime_view &&) noexcept = default;
  145. /**
  146. * @brief Allocator-extended move constructor.
  147. * @param other The instance to move from.
  148. * @param allocator The allocator to use.
  149. */
  150. basic_runtime_view(basic_runtime_view &&other, const allocator_type &allocator)
  151. : pools{std::move(other.pools), allocator},
  152. filter{std::move(other.filter), allocator} {}
  153. /*! @brief Default destructor. */
  154. ~basic_runtime_view() = default;
  155. /**
  156. * @brief Default copy assignment operator.
  157. * @return This runtime view.
  158. */
  159. basic_runtime_view &operator=(const basic_runtime_view &) = default;
  160. /**
  161. * @brief Default move assignment operator.
  162. * @return This runtime view.
  163. */
  164. basic_runtime_view &operator=(basic_runtime_view &&) noexcept = default;
  165. /**
  166. * @brief Exchanges the contents with those of a given view.
  167. * @param other View to exchange the content with.
  168. */
  169. void swap(basic_runtime_view &other) noexcept {
  170. using std::swap;
  171. swap(pools, other.pools);
  172. swap(filter, other.filter);
  173. }
  174. /**
  175. * @brief Returns the associated allocator.
  176. * @return The associated allocator.
  177. */
  178. [[nodiscard]] constexpr allocator_type get_allocator() const noexcept {
  179. return pools.get_allocator();
  180. }
  181. /*! @brief Clears the view. */
  182. void clear() {
  183. pools.clear();
  184. filter.clear();
  185. }
  186. /**
  187. * @brief Appends an opaque storage object to a runtime view.
  188. * @param base An opaque reference to a storage object.
  189. * @return This runtime view.
  190. */
  191. basic_runtime_view &iterate(common_type &base) {
  192. if(pools.empty() || !(base.size() < pools.front()->size())) {
  193. pools.push_back(&base);
  194. } else {
  195. pools.push_back(stl::exchange(pools.front(), &base));
  196. }
  197. return *this;
  198. }
  199. /**
  200. * @brief Adds an opaque storage object as a filter of a runtime view.
  201. * @param base An opaque reference to a storage object.
  202. * @return This runtime view.
  203. */
  204. basic_runtime_view &exclude(common_type &base) {
  205. filter.push_back(&base);
  206. return *this;
  207. }
  208. /**
  209. * @brief Estimates the number of entities iterated by the view.
  210. * @return Estimated number of entities iterated by the view.
  211. */
  212. [[nodiscard]] size_type size_hint() const {
  213. return pools.empty() ? size_type{} : offset();
  214. }
  215. /**
  216. * @brief Returns an iterator to the first entity that has the given
  217. * elements.
  218. *
  219. * If the view is empty, the returned iterator will be equal to `end()`.
  220. *
  221. * @return An iterator to the first entity that has the given elements.
  222. */
  223. [[nodiscard]] iterator begin() const {
  224. return pools.empty() ? iterator{} : iterator{pools, pools.front()->end() - static_cast<difference_type>(offset()), filter};
  225. }
  226. /**
  227. * @brief Returns an iterator that is past the last entity that has the
  228. * given elements.
  229. * @return An iterator to the entity following the last entity that has the
  230. * given elements.
  231. */
  232. [[nodiscard]] iterator end() const {
  233. return pools.empty() ? iterator{} : iterator{pools, pools.front()->end(), filter};
  234. }
  235. /**
  236. * @brief Checks whether a view is initialized or not.
  237. * @return True if the view is initialized, false otherwise.
  238. */
  239. [[nodiscard]] explicit operator bool() const noexcept {
  240. return !(pools.empty() && filter.empty());
  241. }
  242. /**
  243. * @brief Checks if a view contains an entity.
  244. * @param entt A valid identifier.
  245. * @return True if the view contains the given entity, false otherwise.
  246. */
  247. [[nodiscard]] bool contains(const entity_type entt) const {
  248. return !pools.empty()
  249. && std::all_of(pools.cbegin(), pools.cend(), [entt](const auto *curr) { return curr->contains(entt); })
  250. && std::none_of(filter.cbegin(), filter.cend(), [entt](const auto *curr) { return curr && curr->contains(entt); })
  251. && pools.front()->index(entt) < offset();
  252. }
  253. /**
  254. * @brief Iterates entities and applies the given function object to them.
  255. *
  256. * The function object is invoked for each entity. It is provided only with
  257. * the entity itself.<br/>
  258. * The signature of the function should be equivalent to the following:
  259. *
  260. * @code{.cpp}
  261. * void(const entity_type);
  262. * @endcode
  263. *
  264. * @tparam Func Type of the function object to invoke.
  265. * @param func A valid function object.
  266. */
  267. template<typename Func>
  268. void each(Func func) const {
  269. for(const auto entity: *this) {
  270. func(entity);
  271. }
  272. }
  273. private:
  274. container_type pools;
  275. container_type filter;
  276. };
  277. } // namespace entt
  278. #endif