Browse Source

sparse_set/storage: minor changes to make it easier to read for the future me :)

Michele Caini 4 years ago
parent
commit
b67cb4bd1f
2 changed files with 16 additions and 15 deletions
  1. 14 14
      src/entt/entity/sparse_set.hpp
  2. 2 1
      src/entt/entity/storage.hpp

+ 14 - 14
src/entt/entity/sparse_set.hpp

@@ -236,7 +236,7 @@ protected:
     virtual void swap_at(const std::size_t, const std::size_t) {}
     virtual void swap_at(const std::size_t, const std::size_t) {}
 
 
     /*! @brief Moves an entity in a sparse set. */
     /*! @brief Moves an entity in a sparse set. */
-    virtual void move_and_pop(const std::size_t, const std::size_t) {}
+    virtual void move_element(const std::size_t, const std::size_t) {}
 
 
     /**
     /**
      * @brief Erases an entity from a sparse set.
      * @brief Erases an entity from a sparse set.
@@ -761,23 +761,23 @@ public:
 
 
     /*! @brief Removes all tombstones from the packed array of a sparse set. */
     /*! @brief Removes all tombstones from the packed array of a sparse set. */
     void compact() {
     void compact() {
-        size_type next = packed.size();
-        for(; next && packed[next - 1u] == tombstone; --next) {}
-
-        for(auto *it = &free_list; *it != null && next; it = std::addressof(packed[entity_traits::to_entity(*it)])) {
-            if(const size_type pos = entity_traits::to_entity(*it); pos < next) {
-                --next;
-                move_and_pop(next, pos);
-                std::swap(packed[next], packed[pos]);
-                const auto entity = static_cast<typename entity_traits::entity_type>(pos);
-                sparse_ref(packed[pos]) = entity_traits::combine(entity, entity_traits::to_integral(packed[pos]));
-                *it = entity_traits::combine(static_cast<typename entity_traits::entity_type>(next), entity_traits::reserved);
-                for(; next && packed[next - 1u] == tombstone; --next) {}
+        size_type from = packed.size();
+        for(; from && packed[from - 1u] == tombstone; --from) {}
+
+        for(auto *it = &free_list; *it != null && from; it = std::addressof(packed[entity_traits::to_entity(*it)])) {
+            if(const size_type to = entity_traits::to_entity(*it); to < from) {
+                --from;
+                move_element(from, to);
+                std::swap(packed[from], packed[to]);
+                const auto entity = static_cast<typename entity_traits::entity_type>(to);
+                sparse_ref(packed[to]) = entity_traits::combine(entity, entity_traits::to_integral(packed[to]));
+                *it = entity_traits::combine(static_cast<typename entity_traits::entity_type>(from), entity_traits::reserved);
+                for(; from && packed[from - 1u] == tombstone; --from) {}
             }
             }
         }
         }
 
 
         free_list = tombstone;
         free_list = tombstone;
-        packed.resize(next);
+        packed.resize(from);
     }
     }
 
 
     /**
     /**

+ 2 - 1
src/entt/entity/storage.hpp

@@ -337,10 +337,11 @@ protected:
      * @param from A valid position of an element within a storage.
      * @param from A valid position of an element within a storage.
      * @param to A valid position of an element within a storage.
      * @param to A valid position of an element within a storage.
      */
      */
-    void move_and_pop(const std::size_t from, const std::size_t to) final {
+    void move_element(const std::size_t from, const std::size_t to) final {
         auto &elem = element_at(from);
         auto &elem = element_at(from);
         construct(assure_at_least(to), std::move(elem));
         construct(assure_at_least(to), std::move(elem));
         std::destroy_at(std::addressof(elem));
         std::destroy_at(std::addressof(elem));
+        base_type::move_element(from, to);
     }
     }
 
 
     /**
     /**