Parcourir la source

storage: insert for empty types always appends elements

Michele Caini il y a 4 ans
Parent
commit
9eba103de9
2 fichiers modifiés avec 13 ajouts et 13 suppressions
  1. 8 8
      src/entt/entity/storage.hpp
  2. 5 5
      test/entt/entity/storage.cpp

+ 8 - 8
src/entt/entity/storage.hpp

@@ -852,17 +852,17 @@ public:
      * @tparam Args Types of optional arguments.
      * @param first An iterator to the first element of the range of entities.
      * @param last An iterator past the last element of the range of entities.
+     * @param args Parameters to use to construct an object for the entity.
      */
     template<typename It, typename... Args>
     void insert(It first, It last, Args &&...) {
-        for(const auto sz = base_type::size(); first != last && base_type::slot() != sz; ++first) {
-            emplace(*first);
-        }
-
-        base_type::reserve(base_type::size() + std::distance(first, last));
-
-        for(; first != last; ++first) {
-            emplace(*first);
+        if constexpr(std::is_invocable_v<decltype(&base_type::try_insert), base_type &, It, It>) {
+            base_type::try_insert(first, last);
+        } else {
+            for(; first != last; ++first) {
+                entity_type curr[1u]{*first};
+                base_type::try_insert(curr, curr + 1u);
+            }
         }
     }
 

+ 5 - 5
test/entt/entity/storage.cpp

@@ -310,11 +310,11 @@ TEST(Storage, InsertEmptyType) {
     const empty_stable_type values[2u]{};
     pool.insert(std::rbegin(entities), std::rend(entities), std::begin(values));
 
-    ASSERT_EQ(pool.size(), 2u);
-    ASSERT_EQ(pool.at(0u), entities[0u]);
-    ASSERT_EQ(pool.at(1u), entities[1u]);
-    ASSERT_EQ(pool.index(entities[0u]), 0u);
-    ASSERT_EQ(pool.index(entities[1u]), 1u);
+    ASSERT_EQ(pool.size(), 4u);
+    ASSERT_EQ(pool.at(2u), entities[1u]);
+    ASSERT_EQ(pool.at(3u), entities[0u]);
+    ASSERT_EQ(pool.index(entities[0u]), 3u);
+    ASSERT_EQ(pool.index(entities[1u]), 2u);
 }
 
 TEST(Storage, Erase) {