1
0
Эх сурвалжийг харах

swap accepts entities, no longer raw positions

Michele Caini 6 жил өмнө
parent
commit
14bc73cde9

+ 3 - 2
TODO

@@ -14,7 +14,6 @@
 * add opaque input iterators to views and groups that return tuples <entity, T &...> (proxy), multi-pass guaranteed
 * add fast lane for raw iterations, extend mt doc to describe allowed add/remove with pre-allocations on fast lanes
 * registry.each<T...>(first, last) by iterators, entities/components guaranteed
-* multi component registry::remove and some others?
 * built-in support for dual (or N-) buffering
 * allow for custom stomp functions
 * deprecate/replace snapshot
@@ -24,7 +23,6 @@ TODO
 * make meta work across boundaries
   - inline variables are fine here, only the head represents a problem
   - we should always resolve by looking into the list of types when working across boundaries, no direct resolve
-* nested groups: AB/ABC/ABCD/... (hints: sort, check functions)
 * snapshot rework/deprecation
   - create(hint: entity) -> force-create
   - assign<T...>(first, last)
@@ -32,4 +30,7 @@ TODO
   - use direct access (pool-like) also for context variables
   - improves multi-stomp
 * improve registry::alive
+* nested groups: AB/ABC/ABCD/... (hints: sort, check functions)
+* multi component registry::remove and some others?
+* range based registry::remove and some others?
 * allocation-less and faster sort

+ 1 - 1
src/entt/entity/group.hpp

@@ -835,7 +835,7 @@ public:
         for(auto next = *length; next; --next) {
             const auto pos = next - 1;
             [[maybe_unused]] const auto entt = cpool->data()[pos];
-            (std::get<pool_type<Other> *>(pools)->swap(pos, std::get<pool_type<Other> *>(pools)->index(entt)), ...);
+            (std::get<pool_type<Other> *>(pools)->swap(std::get<pool_type<Other> *>(pools)->data()[pos], entt), ...);
         }
     }
 

+ 4 - 4
src/entt/entity/registry.hpp

@@ -167,7 +167,7 @@ class basic_registry {
                         && !(std::get<pool_type<Exclude> *>(cpools)->has(entt) || ...))
                 {
                     const auto pos = this->owned++;
-                    (std::get<pool_type<Owned> *>(cpools)->swap(std::get<pool_type<Owned> *>(cpools)->index(entt), pos), ...);
+                    (std::get<pool_type<Owned> *>(cpools)->swap(std::get<pool_type<Owned> *>(cpools)->data()[pos], entt), ...);
                 }
             } else if constexpr(std::disjunction_v<std::is_same<Exclude, Component>...>) {
                 if((std::get<pool_type<Owned> *>(cpools)->has(entt) && ...)
@@ -175,7 +175,7 @@ class basic_registry {
                         && ((std::is_same_v<Exclude, Component> || !std::get<pool_type<Exclude> *>(cpools)->has(entt)) && ...))
                 {
                     const auto pos = this->owned++;
-                    (std::get<pool_type<Owned> *>(cpools)->swap(std::get<pool_type<Owned> *>(cpools)->index(entt), pos), ...);
+                    (std::get<pool_type<Owned> *>(cpools)->swap(std::get<pool_type<Owned> *>(cpools)->data()[pos], entt), ...);
                 }
             }
         }
@@ -183,7 +183,7 @@ class basic_registry {
         void discard_if(const Entity entt) {
             if(std::get<0>(cpools)->has(entt) && std::get<0>(cpools)->index(entt) < this->owned) {
                 const auto pos = --this->owned;
-                (std::get<pool_type<Owned> *>(cpools)->swap(std::get<pool_type<Owned> *>(cpools)->index(entt), pos), ...);
+                (std::get<pool_type<Owned> *>(cpools)->swap(std::get<pool_type<Owned> *>(cpools)->data()[pos], entt), ...);
             }
         }
     };
@@ -1415,7 +1415,7 @@ public:
                     } else {
                         const auto pos = curr->owned++;
                         // useless this-> used to suppress a warning with clang
-                        (std::get<pool_type<Owned> *>(cpools)->swap(std::get<pool_type<Owned> *>(cpools)->index(entity), pos), ...);
+                        (std::get<pool_type<Owned> *>(cpools)->swap(std::get<pool_type<Owned> *>(cpools)->data()[pos], entity), ...);
                     }
                 }
             });

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

@@ -462,16 +462,16 @@ public:
      * An assertion will abort the execution at runtime in debug mode if the
      * sparse set doesn't contain the given entities.
      *
-     * @param lhs A valid position within the sparse set.
-     * @param rhs A valid position within the sparse set.
+     * @param lhs A valid entity identifier.
+     * @param rhs A valid entity identifier.
      */
-    virtual void swap(const size_type lhs, const size_type rhs) ENTT_NOEXCEPT {
-        ENTT_ASSERT(lhs < direct.size());
-        ENTT_ASSERT(rhs < direct.size());
-        auto [src_page, src_offset] = map(direct[lhs]);
-        auto [dst_page, dst_offset] = map(direct[rhs]);
-        std::swap(reverse[src_page][src_offset], reverse[dst_page][dst_offset]);
-        std::swap(direct[lhs], direct[rhs]);
+    virtual void swap(const entity_type lhs, const entity_type rhs) ENTT_NOEXCEPT {
+        auto [src_page, src_offset] = map(lhs);
+        auto [dst_page, dst_offset] = map(rhs);
+        auto &from = reverse[src_page][src_offset];
+        auto &to = reverse[dst_page][dst_offset];
+        std::swap(direct[size_type(from)], direct[size_type(to)]);
+        std::swap(from, to);
     }
 
     /**
@@ -534,7 +534,7 @@ public:
             auto next = copy[curr];
 
             while(curr != next) {
-                swap(copy[curr] + offset, copy[next] + offset);
+                swap(direct[copy[curr] + offset], direct[copy[next] + offset]);
                 copy[curr] = curr;
                 curr = next;
                 next = copy[curr];
@@ -571,7 +571,7 @@ public:
         while(pos && from != to) {
             if(has(*from)) {
                 if(*from != direct[pos]) {
-                    swap(pos, index(*from));
+                    swap(direct[pos], *from);
                 }
 
                 --pos;

+ 4 - 6
src/entt/entity/storage.hpp

@@ -392,13 +392,11 @@ public:
      * An assertion will abort the execution at runtime in debug mode if the
      * sparse set doesn't contain the given entities.
      *
-     * @param lhs A valid position within the sparse set.
-     * @param rhs A valid position within the sparse set.
+     * @param lhs A valid entity identifier.
+     * @param rhs A valid entity identifier.
      */
-    void swap(const size_type lhs, const size_type rhs) ENTT_NOEXCEPT override {
-        ENTT_ASSERT(lhs < instances.size());
-        ENTT_ASSERT(rhs < instances.size());
-        std::swap(instances[lhs], instances[rhs]);
+    void swap(const entity_type lhs, const entity_type rhs) ENTT_NOEXCEPT override {
+        std::swap(instances[underlying_type::index(lhs)], instances[underlying_type::index(rhs)]);
         underlying_type::swap(lhs, rhs);
     }