EnTT  3.8.0
iterator.hpp
1 #ifndef ENTT_ENTITY_ITERATOR_HPP
2 #define ENTT_ENTITY_ITERATOR_HPP
3 
4 
5 #include <array>
6 #include <cstddef>
7 #include <iterator>
8 #include <tuple>
9 #include <utility>
10 #include "../config/config.h"
11 #include "entity.hpp"
12 #include "sparse_set.hpp"
13 
14 
15 namespace entt {
16 
17 
24  template<typename Entity>
25  [[nodiscard]] static constexpr bool accept(const Entity entity) ENTT_NOEXCEPT {
26  return entity != tombstone;
27  }
32 };
33 
34 
41  template<typename Entity>
42  [[nodiscard]] static constexpr bool accept(const Entity) ENTT_NOEXCEPT {
43  return true;
44  }
49 };
50 
51 
52 template<typename Policy, typename It, std::size_t AllOf, std::size_t NoneOf>
53 class view_iterator final {
55 
56  [[nodiscard]] bool valid() const {
57  const auto entt = *it;
58  return Policy::accept(entt)
59  && std::apply([entt](const auto *... curr) { return (curr->contains(entt) && ...); }, pools)
60  && std::apply([entt](const auto *... curr) { return (!curr->contains(entt) && ...); }, filter);
61  }
62 
63 public:
64  using difference_type = typename std::iterator_traits<It>::difference_type;
65  using value_type = typename std::iterator_traits<It>::value_type;
66  using pointer = typename std::iterator_traits<It>::pointer;
67  using reference = typename std::iterator_traits<It>::reference;
68  using iterator_category = std::bidirectional_iterator_tag;
69 
70  view_iterator() ENTT_NOEXCEPT
71  : first{},
72  last{},
73  it{},
74  pools{},
75  filter{}
76  {}
77 
78  view_iterator(It from, It to, It curr, std::array<const basic_common_type *, AllOf> all_of, std::array<const basic_common_type *, NoneOf> none_of) ENTT_NOEXCEPT
79  : first{std::move(from)},
80  last{std::move(to)},
81  it{std::move(curr)},
82  pools{all_of},
83  filter{none_of}
84  {
85  if(it != last && !valid()) {
86  ++(*this);
87  }
88  }
89 
90  view_iterator & operator++() ENTT_NOEXCEPT {
91  while(++it != last && !valid());
92  return *this;
93  }
94 
95  view_iterator operator++(int) ENTT_NOEXCEPT {
96  view_iterator orig = *this;
97  return ++(*this), orig;
98  }
99 
100  view_iterator & operator--() ENTT_NOEXCEPT {
101  while(--it != first && !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;
120  }
121 
122  [[nodiscard]] reference operator*() const {
123  return *operator->();
124  }
125 
126 private:
127  It first;
128  It last;
129  It it;
130  std::array<const basic_common_type *, AllOf> pools;
131  std::array<const basic_common_type *, NoneOf> filter;
132 };
133 
134 
135 template<typename It>
136 class view_iterator<packed_storage_policy, It, 0u, 0u> final: public It {
137 public:
138  using It::It;
139 
140  view_iterator(It it)
141  : It{std::move(it)}
142  {}
143 };
144 
145 
146 }
147 
148 
149 #endif
Basic sparse set implementation.
Definition: sparse_set.hpp:53
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
Packed storage policy, aimed at faster linear iteration.
Definition: iterator.hpp:36
Stable storage policy, aimed at pointer stability.
Definition: iterator.hpp:19