sparse_set.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #ifndef ENTT_COMPONENT_POOL_HPP
  2. #define ENTT_COMPONENT_POOL_HPP
  3. #include <algorithm>
  4. #include <utility>
  5. #include <numeric>
  6. #include <vector>
  7. #include <cstddef>
  8. #include <cassert>
  9. namespace entt {
  10. template<typename...>
  11. class SparseSet;
  12. template<typename Index>
  13. class SparseSet<Index> {
  14. struct SparseSetIterator;
  15. public:
  16. using index_type = Index;
  17. using pos_type = index_type;
  18. using size_type = std::size_t;
  19. using iterator_type = SparseSetIterator;
  20. private:
  21. struct SparseSetIterator {
  22. using value_type = index_type;
  23. SparseSetIterator(const std::vector<index_type> *direct, size_type pos)
  24. : direct{direct}, pos{pos}
  25. {}
  26. SparseSetIterator & operator++() noexcept {
  27. return --pos, *this;
  28. }
  29. SparseSetIterator operator++(int) noexcept {
  30. SparseSetIterator orig = *this;
  31. return ++(*this), orig;
  32. }
  33. bool operator==(const SparseSetIterator &other) const noexcept {
  34. return other.pos == pos && other.direct == direct;
  35. }
  36. bool operator!=(const SparseSetIterator &other) const noexcept {
  37. return !(*this == other);
  38. }
  39. value_type operator*() const noexcept {
  40. return (*direct)[pos-1];
  41. }
  42. private:
  43. const std::vector<index_type> *direct;
  44. size_type pos;
  45. };
  46. inline bool valid(Index idx) const noexcept {
  47. return idx < reverse.size() && reverse[idx] < direct.size() && direct[reverse[idx]] == idx;
  48. }
  49. public:
  50. explicit SparseSet() = default;
  51. SparseSet(const SparseSet &) = delete;
  52. SparseSet(SparseSet &&) = default;
  53. ~SparseSet() noexcept {
  54. assert(empty());
  55. }
  56. SparseSet & operator=(const SparseSet &) = delete;
  57. SparseSet & operator=(SparseSet &&) = default;
  58. size_type size() const noexcept {
  59. return direct.size();
  60. }
  61. size_t capacity() const noexcept {
  62. return direct.capacity();
  63. }
  64. bool empty() const noexcept {
  65. return direct.empty();
  66. }
  67. const index_type * data() const noexcept {
  68. return direct.data();
  69. }
  70. iterator_type begin() const noexcept {
  71. return SparseSetIterator{&direct, direct.size()};
  72. }
  73. iterator_type end() const noexcept {
  74. return SparseSetIterator{&direct, 0};
  75. }
  76. bool has(index_type idx) const noexcept {
  77. return valid(idx);
  78. }
  79. pos_type get(index_type idx) const noexcept {
  80. assert(valid(idx));
  81. return reverse[idx];
  82. }
  83. pos_type construct(index_type idx) {
  84. assert(!valid(idx));
  85. if(!(idx < reverse.size())) {
  86. reverse.resize(idx+1);
  87. }
  88. auto pos = pos_type(direct.size());
  89. reverse[idx] = pos;
  90. direct.emplace_back(idx);
  91. return pos;
  92. }
  93. pos_type destroy(index_type idx) {
  94. assert(valid(idx));
  95. auto last = direct.size() - 1;
  96. auto pos = reverse[idx];
  97. reverse[direct[last]] = pos;
  98. direct[pos] = direct[last];
  99. direct.pop_back();
  100. return pos;
  101. }
  102. void swap(index_type lhs, index_type rhs) {
  103. assert(valid(lhs));
  104. assert(valid(rhs));
  105. std::swap(direct[reverse[lhs]], direct[reverse[rhs]]);
  106. std::swap(reverse[lhs], reverse[rhs]);
  107. }
  108. void reset() {
  109. reverse.clear();
  110. direct.clear();
  111. }
  112. private:
  113. std::vector<pos_type> reverse;
  114. std::vector<index_type> direct;
  115. };
  116. template<typename Index, typename Type>
  117. class SparseSet<Index, Type> final: public SparseSet<Index> {
  118. template<typename Compare>
  119. void arrange(Compare compare) {
  120. const auto *data = SparseSet<Index>::data();
  121. const auto size = SparseSet<Index>::size();
  122. std::vector<pos_type> copy(size);
  123. std::iota(copy.begin(), copy.end(), pos_type{});
  124. std::sort(copy.begin(), copy.end(), compare);
  125. for(pos_type i = 0; i < copy.size(); ++i) {
  126. const auto target = i;
  127. auto curr = i;
  128. while(copy[curr] != target) {
  129. SparseSet<Index>::swap(*(data + copy[curr]), *(data + curr));
  130. std::swap(instances[copy[curr]], instances[curr]);
  131. std::swap(copy[curr], curr);
  132. }
  133. copy[curr] = curr;
  134. }
  135. }
  136. public:
  137. using type = Type;
  138. using index_type = typename SparseSet<Index>::index_type;
  139. using pos_type = typename SparseSet<Index>::pos_type;
  140. using size_type = typename SparseSet<Index>::size_type;
  141. using iterator_type = typename SparseSet<Index>::iterator_type;
  142. explicit SparseSet() = default;
  143. SparseSet(const SparseSet &) = delete;
  144. SparseSet(SparseSet &&) = default;
  145. SparseSet & operator=(const SparseSet &) = delete;
  146. SparseSet & operator=(SparseSet &&) = default;
  147. type * raw() noexcept {
  148. return instances.data();
  149. }
  150. const type * raw() const noexcept {
  151. return instances.data();
  152. }
  153. const type & get(index_type idx) const noexcept {
  154. return instances[SparseSet<Index>::get(idx)];
  155. }
  156. type & get(index_type idx) noexcept {
  157. return const_cast<type &>(const_cast<const SparseSet *>(this)->get(idx));
  158. }
  159. template<typename... Args>
  160. type & construct(index_type idx, Args&&... args) {
  161. SparseSet<Index>::construct(idx);
  162. instances.push_back({ std::forward<Args>(args)... });
  163. return instances.back();
  164. }
  165. void destroy(index_type idx) {
  166. auto pos = SparseSet<Index>::destroy(idx);
  167. instances[pos] = std::move(instances[SparseSet<Index>::size()]);
  168. instances.pop_back();
  169. }
  170. void swap(index_type lhs, index_type rhs) {
  171. std::swap(instances[SparseSet<Index>::get(lhs)], instances[SparseSet<Index>::get(rhs)]);
  172. }
  173. template<typename Compare>
  174. void sort(Compare compare) {
  175. arrange([this, compare = std::move(compare)](auto lhs, auto rhs) {
  176. return !compare(instances[lhs], instances[rhs]);
  177. });
  178. }
  179. template<typename Idx>
  180. void respect(const SparseSet<Idx> &other) {
  181. const auto *data = SparseSet<Index>::data();
  182. arrange([data, &other](auto lhs, auto rhs) {
  183. auto eLhs = *(data + lhs);
  184. auto eRhs = *(data + rhs);
  185. bool bLhs = other.has(eLhs);
  186. bool bRhs = other.has(eRhs);
  187. bool compare = false;
  188. if(bLhs && bRhs) {
  189. compare = other.get(eLhs) < other.get(eRhs);
  190. } else if(!bLhs && !bRhs) {
  191. compare = eLhs < eRhs;
  192. } else {
  193. compare = bRhs;
  194. }
  195. return compare;
  196. });
  197. }
  198. void reset() {
  199. SparseSet<Index>::reset();
  200. instances.clear();
  201. }
  202. private:
  203. std::vector<type> instances;
  204. };
  205. }
  206. #endif // ENTT_COMPONENT_POOL_HPP