Browse Source

review: capacity

Michele Caini 7 years ago
parent
commit
a66fa9d844

+ 0 - 1
TODO

@@ -9,7 +9,6 @@
 * ease the assignment of tags as string (type-less assign member function + user defined literal for hashed strings)
 * break snapshot <-> registry dependency (and get rid of utility.hpp)
 * family -> std::uint32_t (or some other fixed and known size type)
-* capacity extended to types of components
 * work stealing job system (see #100)
 * more on runtime stuff
 * C++17. That's all.

+ 18 - 7
src/entt/entity/registry.hpp

@@ -195,7 +195,7 @@ public:
     }
 
     /**
-     * @brief Increases the capacity of the pool for a given component.
+     * @brief Increases the capacity of the pool for the given component.
      *
      * If the new capacity is greater than the current capacity, new storage is
      * allocated, otherwise the method does nothing.
@@ -222,17 +222,28 @@ public:
     }
 
     /**
-     * @brief Returns the number of entities ever created.
-     * @return Number of entities ever created.
+     * @brief Returns the capacity of the pool for the given component.
+     * @tparam Component Type of component in which one is interested.
+     * @return Capacity of the pool of the given component.
+     */
+    template<typename Component>
+    size_type capacity() const ENTT_NOEXCEPT {
+        return managed<Component>() ? pool<Component>().capacity() : size_type{};
+    }
+
+    /**
+     * @brief Returns the number of entities that a registry has currently
+     * allocated space for.
+     * @return Capacity of the registry.
      */
     size_type capacity() const ENTT_NOEXCEPT {
-        return entities.size();
+        return entities.capacity();
     }
 
     /**
-     * @brief Checks whether the pool for the given component is empty.
+     * @brief Checks whether the pool of the given component is empty.
      * @tparam Component Type of component in which one is interested.
-     * @return True if the pool for the given component is empty, false
+     * @return True if the pool of the given component is empty, false
      * otherwise.
      */
     template<typename Component>
@@ -1029,7 +1040,7 @@ public:
      * maximize the performance during iterations and users should not make any
      * assumption on the order.<br/>
      * This function can be used to impose an order to the elements in the pool
-     * for the given component. The order is kept valid until a component of the
+     * of the given component. The order is kept valid until a component of the
      * given type is assigned or removed from an entity.
      *
      * The comparison function object must return `true` if the first element

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

@@ -200,6 +200,15 @@ public:
         direct.reserve(cap);
     }
 
+    /**
+     * @brief Returns the number of elements that a sparse set has currently
+     * allocated space for.
+     * @return Capacity of the sparse set.
+     */
+    size_type capacity() const ENTT_NOEXCEPT {
+        return direct.capacity();
+    }
+
     /**
      * @brief Returns the extent of a sparse set.
      *

+ 3 - 4
test/entt/entity/registry.cpp

@@ -64,7 +64,9 @@ TEST(DefaultRegistry, Functionalities) {
     ASSERT_NO_THROW(registry.reserve<char>(8));
     ASSERT_TRUE(registry.empty());
 
-    ASSERT_EQ(registry.capacity(), entt::DefaultRegistry::size_type{0});
+    ASSERT_EQ(registry.capacity(), entt::DefaultRegistry::size_type{42});
+    ASSERT_EQ(registry.capacity<int>(), entt::DefaultRegistry::size_type{8});
+    ASSERT_EQ(registry.capacity<char>(), entt::DefaultRegistry::size_type{8});
     ASSERT_EQ(registry.size<int>(), entt::DefaultRegistry::size_type{0});
     ASSERT_EQ(registry.size<char>(), entt::DefaultRegistry::size_type{0});
     ASSERT_TRUE(registry.empty<int>());
@@ -79,7 +81,6 @@ TEST(DefaultRegistry, Functionalities) {
     ASSERT_TRUE(registry.has<>(e0));
     ASSERT_TRUE(registry.has<>(e1));
 
-    ASSERT_EQ(registry.capacity(), entt::DefaultRegistry::size_type{2});
     ASSERT_EQ(registry.size<int>(), entt::DefaultRegistry::size_type{1});
     ASSERT_EQ(registry.size<char>(), entt::DefaultRegistry::size_type{1});
     ASSERT_FALSE(registry.empty<int>());
@@ -137,9 +138,7 @@ TEST(DefaultRegistry, Functionalities) {
 
     ASSERT_EQ(registry.version(e2), entt::DefaultRegistry::version_type{0});
     ASSERT_EQ(registry.current(e2), entt::DefaultRegistry::version_type{0});
-    ASSERT_EQ(registry.capacity(), entt::DefaultRegistry::size_type{3});
     ASSERT_NO_THROW(registry.destroy(e2));
-    ASSERT_EQ(registry.capacity(), entt::DefaultRegistry::size_type{3});
     ASSERT_EQ(registry.version(e2), entt::DefaultRegistry::version_type{0});
     ASSERT_EQ(registry.current(e2), entt::DefaultRegistry::version_type{1});
 

+ 6 - 2
test/entt/entity/sparse_set.cpp

@@ -6,7 +6,9 @@ TEST(SparseSetNoType, Functionalities) {
     entt::SparseSet<std::uint64_t> set;
     const auto &cset = set;
 
-    ASSERT_NO_THROW(set.reserve(42));
+    set.reserve(42);
+
+    ASSERT_EQ(set.capacity(), 42);
     ASSERT_TRUE(set.empty());
     ASSERT_EQ(set.size(), 0u);
     ASSERT_EQ(cset.begin(), cset.end());
@@ -339,7 +341,9 @@ TEST(SparseSetWithType, Functionalities) {
     entt::SparseSet<std::uint64_t, int> set;
     const auto &cset = set;
 
-    ASSERT_NO_THROW(set.reserve(42));
+    set.reserve(42);
+
+    ASSERT_EQ(set.capacity(), 42);
     ASSERT_TRUE(set.empty());
     ASSERT_EQ(set.size(), 0u);
     ASSERT_EQ(cset.begin(), cset.end());