소스 검색

sparse_set: a better swap

Michele Caini 5 년 전
부모
커밋
effed56653
2개의 변경된 파일10개의 추가작업 그리고 24개의 파일을 삭제
  1. 9 8
      src/entt/entity/sparse_set.hpp
  2. 1 16
      src/entt/entity/storage.hpp

+ 9 - 8
src/entt/entity/sparse_set.hpp

@@ -149,8 +149,6 @@ class basic_sparse_set {
         index_type index;
     };
 
-    virtual void swap(const std::size_t, const std::size_t) {}
-
     [[nodiscard]] auto page(const Entity entt) const ENTT_NOEXCEPT {
         return size_type{(to_integral(entt) & traits_type::entity_mask) / entt_per_page};
     }
@@ -175,6 +173,8 @@ class basic_sparse_set {
         return sparse[pos];
     }
 
+    virtual void swap_at(const std::size_t, const std::size_t) {}
+
 public:
     /*! @brief Underlying entity identifier. */
     using entity_type = Entity;
@@ -460,11 +460,12 @@ public:
      * @param lhs A valid entity identifier.
      * @param rhs A valid entity identifier.
      */
-    virtual void swap(const entity_type lhs, const entity_type rhs) {
-        auto &from = sparse[page(lhs)][offset(lhs)];
-        auto &to = sparse[page(rhs)][offset(rhs)];
-        std::swap(packed[size_type{to_integral(from)}], packed[size_type{to_integral(to)}]);
-        std::swap(from, to);
+    void swap(const entity_type lhs, const entity_type rhs) {
+        const auto from = index(lhs);
+        const auto to = index(rhs);
+        swap_at(from, to);
+        std::swap(sparse[page(lhs)][offset(lhs)], sparse[page(rhs)][offset(rhs)]);
+        std::swap(packed[from], packed[to]);
     }
 
     /**
@@ -508,7 +509,7 @@ public:
             auto next = index(packed[curr]);
 
             while(curr != next) {
-                swap(next, index(packed[next]));
+                swap_at(next, index(packed[next]));
                 sparse[page(packed[curr])][offset(packed[curr])] = entity_type{static_cast<typename traits_type::entity_type>(curr)};
 
                 curr = next;

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

@@ -156,7 +156,7 @@ class basic_storage: public basic_sparse_set<Entity> {
         index_type index;
     };
 
-    void swap(const std::size_t lhs, const std::size_t rhs) final {
+    void swap_at(const std::size_t lhs, const std::size_t rhs) final {
         std::swap(instances[lhs], instances[rhs]);
     }
 
@@ -440,21 +440,6 @@ public:
         underlying_type::remove(first, last);
     }
 
-    /**
-     * @brief Swaps entities and objects in the internal packed arrays.
-     *
-     * @warning
-     * Attempting to swap entities that don't belong to the sparse set results
-     * in undefined behavior.
-     *
-     * @param lhs A valid entity identifier.
-     * @param rhs A valid entity identifier.
-     */
-    void swap(const entity_type lhs, const entity_type rhs) final {
-        swap(underlying_type::index(lhs), underlying_type::index(rhs));
-        underlying_type::swap(lhs, rhs);
-    }
-
     /**
      * @brief Sort elements according to the given comparison function.
      *