entt  2.5.0
sparse_set.hpp
1 #ifndef ENTT_ENTITY_SPARSE_SET_HPP
2 #define ENTT_ENTITY_SPARSE_SET_HPP
3 
4 
5 #include <algorithm>
6 #include <iterator>
7 #include <numeric>
8 #include <utility>
9 #include <vector>
10 #include <cstddef>
11 #include <cassert>
12 #include <type_traits>
13 #include "entt_traits.hpp"
14 
15 
16 namespace entt {
17 
18 
25 template<typename...>
26 class SparseSet;
27 
28 
56 template<typename Entity>
59 
60  struct Iterator final {
61  using difference_type = std::size_t;
62  using value_type = Entity;
63  using pointer = value_type *;
64  using reference = value_type;
65  using iterator_category = std::input_iterator_tag;
66 
67  Iterator(const std::vector<value_type> &direct, std::size_t pos)
68  : direct{direct}, pos{pos}
69  {}
70 
71  Iterator & operator++() noexcept {
72  return --pos, *this;
73  }
74 
75  Iterator operator++(int) noexcept {
76  Iterator orig = *this;
77  return ++(*this), orig;
78  }
79 
80  Iterator & operator+=(difference_type value) noexcept {
81  pos -= value;
82  return *this;
83  }
84 
85  Iterator operator+(difference_type value) noexcept {
86  return Iterator{direct, pos-value};
87  }
88 
89  bool operator==(const Iterator &other) const noexcept {
90  return other.pos == pos;
91  }
92 
93  bool operator!=(const Iterator &other) const noexcept {
94  return !(*this == other);
95  }
96 
97  reference operator*() const noexcept {
98  return direct[pos-1];
99  }
100 
101  private:
102  const std::vector<value_type> &direct;
103  std::size_t pos;
104  };
105 
106  static constexpr Entity in_use = (Entity{1} << traits_type::entity_shift);
107 
108 public:
110  using entity_type = Entity;
114  using size_type = std::size_t;
116  using iterator_type = Iterator;
117 
119  SparseSet() noexcept = default;
120 
122  virtual ~SparseSet() noexcept = default;
123 
125  SparseSet(const SparseSet &) = delete;
127  SparseSet(SparseSet &&) = default;
128 
130  SparseSet & operator=(const SparseSet &) = delete;
132  SparseSet & operator=(SparseSet &&) = default;
133 
142  void reserve(size_type cap) {
143  direct.reserve(cap);
144  }
145 
156  size_type extent() const noexcept {
157  return reverse.size();
158  }
159 
170  size_type size() const noexcept {
171  return direct.size();
172  }
173 
178  bool empty() const noexcept {
179  return direct.empty();
180  }
181 
197  const entity_type * data() const noexcept {
198  return direct.data();
199  }
200 
213  iterator_type begin() const noexcept {
214  return Iterator{direct, direct.size()};
215  }
216 
230  iterator_type end() const noexcept {
231  return Iterator{direct, 0};
232  }
233 
239  bool has(entity_type entity) const noexcept {
240  const auto pos = size_type(entity & traits_type::entity_mask);
241  // the in-use control bit permits to avoid accessing the direct vector
242  return (pos < reverse.size()) && (reverse[pos] & in_use);
243  }
244 
262  bool fast(entity_type entity) const noexcept {
263  const auto pos = size_type(entity & traits_type::entity_mask);
264  assert(pos < reverse.size());
265  // the in-use control bit permits to avoid accessing the direct vector
266  return (reverse[pos] & in_use);
267  }
268 
281  pos_type get(entity_type entity) const noexcept {
282  assert(has(entity));
283  const auto entt = entity & traits_type::entity_mask;
284  // we must get rid of the in-use bit for it's not part of the position
285  return reverse[entt] & traits_type::entity_mask;
286  }
287 
299  void construct(entity_type entity) {
300  assert(!has(entity));
301  const auto pos = size_type(entity & traits_type::entity_mask);
302 
303  if(!(pos < reverse.size())) {
304  reverse.resize(pos+1, pos_type{});
305  }
306 
307  // we exploit the fact that pos_type is equal to entity_type and pos has
308  // traits_type::version_mask bits unused we can use to mark it as in-use
309  reverse[pos] = pos_type(direct.size()) | in_use;
310  direct.emplace_back(entity);
311  }
312 
324  virtual void destroy(entity_type entity) {
325  assert(has(entity));
326  const auto entt = entity & traits_type::entity_mask;
327  const auto back = direct.back() & traits_type::entity_mask;
328  // we must get rid of the in-use bit for it's not part of the position
329  const auto pos = reverse[entt] & traits_type::entity_mask;
330  reverse[back] = reverse[entt];
331  reverse[entt] = pos;
332  // swapping isn't required here, we are getting rid of the last element
333  direct[pos] = direct.back();
334  direct.pop_back();
335  }
336 
352  void swap(pos_type lhs, pos_type rhs) noexcept {
353  assert(lhs < direct.size());
354  assert(rhs < direct.size());
355  const auto src = direct[lhs] & traits_type::entity_mask;
356  const auto dst = direct[rhs] & traits_type::entity_mask;
357  std::swap(reverse[src], reverse[dst]);
358  std::swap(direct[lhs], direct[rhs]);
359  }
360 
380  void respect(const SparseSet<Entity> &other) noexcept {
381  auto from = other.begin();
382  auto to = other.end();
383 
384  pos_type pos = direct.size() - 1;
385 
386  while(pos && from != to) {
387  if(has(*from)) {
388  if(*from != direct[pos]) {
389  swap(pos, get(*from));
390  }
391 
392  --pos;
393  }
394 
395  ++from;
396  }
397  }
398 
402  virtual void reset() {
403  reverse.clear();
404  direct.clear();
405  }
406 
407 private:
408  std::vector<pos_type> reverse;
409  std::vector<entity_type> direct;
410 };
411 
412 
435 template<typename Entity, typename Type>
438 
439  struct Iterator final {
440  using difference_type = std::size_t;
441  using value_type = Type;
442  using pointer = value_type *;
443  using reference = value_type &;
444  using iterator_category = std::input_iterator_tag;
445 
446  Iterator(std::vector<value_type> &instances, std::size_t pos)
447  : instances{instances}, pos{pos}
448  {}
449 
450  Iterator & operator++() noexcept {
451  return --pos, *this;
452  }
453 
454  Iterator operator++(int) noexcept {
455  Iterator orig = *this;
456  return ++(*this), orig;
457  }
458 
459  Iterator & operator+=(difference_type value) noexcept {
460  pos -= value;
461  return *this;
462  }
463 
464  Iterator operator+(difference_type value) noexcept {
465  return Iterator{instances, pos-value};
466  }
467 
468  bool operator==(const Iterator &other) const noexcept {
469  return other.pos == pos;
470  }
471 
472  bool operator!=(const Iterator &other) const noexcept {
473  return !(*this == other);
474  }
475 
476  reference operator*() noexcept {
477  return instances[pos-1];
478  }
479 
480  pointer operator->() noexcept {
481  return &instances.data()[pos-1];
482  }
483 
484  private:
485  std::vector<value_type> &instances;
486  std::size_t pos;
487  };
488 
489 public:
491  using object_type = Type;
499  using iterator_type = Iterator;
500 
502  SparseSet() noexcept = default;
503 
505  SparseSet(const SparseSet &) = delete;
507  SparseSet(SparseSet &&) = default;
508 
510  SparseSet & operator=(const SparseSet &) = delete;
512  SparseSet & operator=(SparseSet &&) = default;
513 
522  void reserve(size_type cap) {
523  underlying_type::reserve(cap);
524  instances.reserve(cap);
525  }
526 
542  const object_type * raw() const noexcept {
543  return instances.data();
544  }
545 
561  object_type * raw() noexcept {
562  return instances.data();
563  }
564 
577  iterator_type begin() noexcept {
578  return Iterator{instances, instances.size()};
579  }
580 
595  iterator_type end() noexcept {
596  return Iterator{instances, 0};
597  }
598 
611  const object_type & get(entity_type entity) const noexcept {
612  return instances[underlying_type::get(entity)];
613  }
614 
627  object_type & get(entity_type entity) noexcept {
628  return const_cast<object_type &>(const_cast<const SparseSet *>(this)->get(entity));
629  }
630 
651  template<typename... Args>
652  std::enable_if_t<std::is_constructible<Type, Args...>::value, object_type &>
653  construct(entity_type entity, Args &&... args) {
654  underlying_type::construct(entity);
655  instances.emplace_back(std::forward<Args>(args)...);
656  return instances.back();
657  }
658 
679  template<typename... Args>
680  std::enable_if_t<!std::is_constructible<Type, Args...>::value, object_type &>
681  construct(entity_type entity, Args &&... args) {
682  underlying_type::construct(entity);
683  instances.emplace_back(Type{std::forward<Args>(args)...});
684  return instances.back();
685  }
686 
698  void destroy(entity_type entity) override {
699  // swapping isn't required here, we are getting rid of the last element
700  // however, we must protect ourselves from self assignments (see #37)
701  auto tmp = std::move(instances.back());
702  instances[underlying_type::get(entity)] = std::move(tmp);
703  instances.pop_back();
704  underlying_type::destroy(entity);
705  }
706 
730  template<typename Compare>
731  void sort(Compare compare) {
732  std::vector<pos_type> copy(instances.size());
733  std::iota(copy.begin(), copy.end(), 0);
734 
735  std::sort(copy.begin(), copy.end(), [this, compare = std::move(compare)](auto lhs, auto rhs) {
736  return compare(const_cast<const object_type &>(instances[rhs]), const_cast<const object_type &>(instances[lhs]));
737  });
738 
739  for(pos_type pos = 0, last = copy.size(); pos < last; ++pos) {
740  auto curr = pos;
741  auto next = copy[curr];
742 
743  while(curr != next) {
744  const auto lhs = copy[curr];
745  const auto rhs = copy[next];
746  std::swap(instances[lhs], instances[rhs]);
747  underlying_type::swap(lhs, rhs);
748  copy[curr] = curr;
749  curr = next;
750  next = copy[curr];
751  }
752  }
753  }
754 
778  void respect(const SparseSet<Entity> &other) noexcept {
779  auto from = other.begin();
780  auto to = other.end();
781 
782  pos_type pos = underlying_type::size() - 1;
783  const auto *local = underlying_type::data();
784 
785  while(pos && from != to) {
786  const auto curr = *from;
787 
788  if(underlying_type::has(curr)) {
789  if(curr != *(local + pos)) {
790  auto candidate = underlying_type::get(curr);
791  std::swap(instances[pos], instances[candidate]);
792  underlying_type::swap(pos, candidate);
793  }
794 
795  --pos;
796  }
797 
798  ++from;
799  }
800  }
801 
805  void reset() override {
806  underlying_type::reset();
807  instances.clear();
808  }
809 
810 private:
811  std::vector<object_type> instances;
812 };
813 
814 
815 }
816 
817 
818 #endif // ENTT_ENTITY_SPARSE_SET_HPP
size_type extent() const noexcept
Returns the extent of a sparse set.
Definition: sparse_set.hpp:156
void respect(const SparseSet< Entity > &other) noexcept
Sort entities according to their order in another sparse set.
Definition: sparse_set.hpp:380
void sort(Compare compare)
Sort components according to the given comparison function.
Definition: sparse_set.hpp:731
virtual void reset()
Resets a sparse set.
Definition: sparse_set.hpp:402
EnTT default namespace.
Definition: family.hpp:10
void reset() override
Resets a sparse set.
Definition: sparse_set.hpp:805
const object_type * raw() const noexcept
Direct access to the array of objects.
Definition: sparse_set.hpp:542
bool empty() const noexcept
Checks whether a sparse set is empty.
Definition: sparse_set.hpp:178
Sparse set.
Definition: sparse_set.hpp:26
std::size_t size_type
Unsigned integer type.
Definition: sparse_set.hpp:114
Extended sparse set implementation.
Definition: sparse_set.hpp:436
void destroy(entity_type entity) override
Removes an entity from a sparse set and destroies its object.
Definition: sparse_set.hpp:698
Entity entity_type
Underlying entity identifier.
Definition: sparse_set.hpp:110
Iterator iterator_type
Input iterator type.
Definition: sparse_set.hpp:116
Basic sparse set implementation.
Definition: sparse_set.hpp:57
std::enable_if_t< std::is_constructible< Type, Args... >::value, object_type & > construct(entity_type entity, Args &&... args)
Assigns an entity to a sparse set and constructs its object.
Definition: sparse_set.hpp:653
bool has(entity_type entity) const noexcept
Checks if a sparse set contains an entity.
Definition: sparse_set.hpp:239
std::enable_if_t<!std::is_constructible< Type, Args... >::value, object_type & > construct(entity_type entity, Args &&... args)
Assigns an entity to a sparse set and constructs its object.
Definition: sparse_set.hpp:681
entity_type pos_type
Entity dependent position type.
Definition: sparse_set.hpp:112
Type object_type
Type of the objects associated to the entities.
Definition: sparse_set.hpp:491
const entity_type * data() const noexcept
Direct access to the internal packed array.
Definition: sparse_set.hpp:197
iterator_type end() const noexcept
Returns an iterator to the end.
Definition: sparse_set.hpp:230
virtual void destroy(entity_type entity)
Removes an entity from a sparse set.
Definition: sparse_set.hpp:324
iterator_type begin() noexcept
Returns an iterator to the beginning.
Definition: sparse_set.hpp:577
bool fast(entity_type entity) const noexcept
Checks if a sparse set contains an entity (unsafe).
Definition: sparse_set.hpp:262
iterator_type begin() const noexcept
Returns an iterator to the beginning.
Definition: sparse_set.hpp:213
iterator_type end() noexcept
Returns an iterator to the end.
Definition: sparse_set.hpp:595
void construct(entity_type entity)
Assigns an entity to a sparse set.
Definition: sparse_set.hpp:299
size_type size() const noexcept
Returns the number of elements in a sparse set.
Definition: sparse_set.hpp:170
void respect(const SparseSet< Entity > &other) noexcept
Sort components according to the order of the entities in another sparse set.
Definition: sparse_set.hpp:778
constexpr bool operator!=(const HashedString &lhs, const HashedString &rhs) noexcept
Compares two hashed strings.
object_type * raw() noexcept
Direct access to the array of objects.
Definition: sparse_set.hpp:561
void swap(pos_type lhs, pos_type rhs) noexcept
Swaps the position of two entities in the internal packed array.
Definition: sparse_set.hpp:352
Entity traits.
Definition: entt_traits.hpp:18