1
0
Michele Caini 8 жил өмнө
parent
commit
9358691901

+ 38 - 3
src/entt/entity/registry.hpp

@@ -154,6 +154,13 @@ class Registry {
         return *handlers[vtype];
         return *handlers[vtype];
     }
     }
 
 
+    Entity candidate() const noexcept {
+        auto entity = entity_type(entities.size());
+        assert(entity < traits_type::entity_mask);
+        assert((entity >> traits_type::entity_shift) == entity_type{});
+        return entity;
+    }
+
 public:
 public:
     /*! @brief Underlying entity identifier. */
     /*! @brief Underlying entity identifier. */
     using entity_type = typename traits_type::entity_type;
     using entity_type = typename traits_type::entity_type;
@@ -231,6 +238,36 @@ public:
         return entities.size() - available.size();
         return entities.size() - available.size();
     }
     }
 
 
+    /**
+     * @brief Increases the capacity of the pool for a given component.
+     *
+     * If the new capacity is greater than the current capacity, new storage is
+     * allocated, otherwise the method does nothing.
+     *
+     * @tparam Component Type of component for which to reserve storage.
+     * @tparam cap Desired capacity.
+     */
+    template<typename Component>
+    void reserve(size_type cap) {
+        ensure<Component>().reserve(cap);
+    }
+
+    /**
+     * @brief Increases the capacity of a registry in terms of entities.
+     *
+     * If the new capacity is greater than the current capacity, new storage is
+     * allocated, otherwise the method does nothing.
+     *
+     * @tparam cap Desired capacity.
+     */
+    void reserve(size_type cap) {
+        const auto last = entity_type(cap);
+
+        for(auto entity = candidate(); entity < last; ++entity) {
+            available.push_back(entity);
+        }
+    }
+
     /**
     /**
      * @brief Returns the number of entities ever created.
      * @brief Returns the number of entities ever created.
      * @return Number of entities ever created.
      * @return Number of entities ever created.
@@ -380,9 +417,7 @@ public:
         entity_type entity;
         entity_type entity;
 
 
         if(available.empty()) {
         if(available.empty()) {
-            entity = entity_type(entities.size());
-            assert(entity < traits_type::entity_mask);
-            assert((entity >> traits_type::entity_shift) == entity_type{});
+            entity = candidate();
             entities.push_back(entity);
             entities.push_back(entity);
         } else {
         } else {
             entity = available.back();
             entity = available.back();

+ 28 - 2
src/entt/entity/sparse_set.hpp

@@ -117,6 +117,19 @@ public:
     /*! @brief Default move assignment operator. @return This sparse set. */
     /*! @brief Default move assignment operator. @return This sparse set. */
     SparseSet & operator=(SparseSet &&) = default;
     SparseSet & operator=(SparseSet &&) = default;
 
 
+    /**
+     * @brief Increases the capacity of a sparse set.
+     *
+     * If the new capacity is greater than the current capacity, new storage is
+     * allocated, otherwise the method does nothing.
+     *
+     * @tparam cap Desired capacity.
+     */
+    void reserve(size_type cap) {
+        reverse.reserve(cap);
+        direct.reserve(cap);
+    }
+
     /**
     /**
      * @brief Returns the number of elements in a sparse set.
      * @brief Returns the number of elements in a sparse set.
      *
      *
@@ -400,6 +413,19 @@ public:
     /*! @brief Default move assignment operator. @return This sparse set. */
     /*! @brief Default move assignment operator. @return This sparse set. */
     SparseSet & operator=(SparseSet &&) = default;
     SparseSet & operator=(SparseSet &&) = default;
 
 
+    /**
+     * @brief Increases the capacity of a sparse set.
+     *
+     * If the new capacity is greater than the current capacity, new storage is
+     * allocated, otherwise the method does nothing.
+     *
+     * @tparam cap Desired capacity.
+     */
+    void reserve(size_type cap) {
+        underlying_type::reserve(cap);
+        instances.reserve(cap);
+    }
+
     /**
     /**
      * @brief Direct access to the array of objects.
      * @brief Direct access to the array of objects.
      *
      *
@@ -541,8 +567,8 @@ public:
             return compare(const_cast<const object_type &>(instances[rhs]), const_cast<const object_type &>(instances[lhs]));
             return compare(const_cast<const object_type &>(instances[rhs]), const_cast<const object_type &>(instances[lhs]));
         });
         });
 
 
-        for(pos_type i = 0; i < copy.size(); ++i) {
-            auto curr = i;
+        for(pos_type pos = 0, last = copy.size(); pos < last; ++pos) {
+            auto curr = pos;
             auto next = copy[curr];
             auto next = copy[curr];
 
 
             while(curr != next) {
             while(curr != next) {

+ 2 - 2
src/entt/process/scheduler.hpp

@@ -273,8 +273,8 @@ public:
     void update(Delta delta) {
     void update(Delta delta) {
         bool clean = false;
         bool clean = false;
 
 
-        for(auto i = handlers.size(); i > 0; --i) {
-            auto &handler = handlers[i-1];
+        for(auto pos = handlers.size(); pos > 0; --pos) {
+            auto &handler = handlers[pos-1];
             const bool dead = handler.update(handler, delta);
             const bool dead = handler.update(handler, delta);
             clean = clean || dead;
             clean = clean || dead;
         }
         }

+ 2 - 2
src/entt/signal/sigh.hpp

@@ -25,14 +25,14 @@ struct Invoker<Ret(Args...), Collector> {
     virtual ~Invoker() = default;
     virtual ~Invoker() = default;
 
 
     template<typename SFINAE = Ret>
     template<typename SFINAE = Ret>
-    typename std::enable_if<std::is_void<SFINAE>::value, bool>::type
+    typename std::enable_if_t<std::is_void<SFINAE>::value, bool>
     invoke(Collector &, proto_type proto, void *instance, Args... args) {
     invoke(Collector &, proto_type proto, void *instance, Args... args) {
         proto(instance, args...);
         proto(instance, args...);
         return true;
         return true;
     }
     }
 
 
     template<typename SFINAE = Ret>
     template<typename SFINAE = Ret>
-    typename std::enable_if<!std::is_void<SFINAE>::value, bool>::type
+    typename std::enable_if_t<!std::is_void<SFINAE>::value, bool>
     invoke(Collector &collector, proto_type proto, void *instance, Args... args) {
     invoke(Collector &collector, proto_type proto, void *instance, Args... args) {
         return collector(proto(instance, args...));
         return collector(proto(instance, args...));
     }
     }