]> git.seodisparate.com - EntityComponentMetaSystem/commitdiff
Replace vector with deque for Component storage
authorStephen Seo <seo.disparate@gmail.com>
Wed, 6 Nov 2019 06:47:16 +0000 (15:47 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 6 Nov 2019 06:47:16 +0000 (15:47 +0900)
When reallocating more data, vector moves the original data to a new
buffer. Deque preserves the location of the original data, preventing
invalidated pointers to existing data when additional allocation is
required.

src/EC/Manager.hpp

index 5f10fd6feabfaa9ca5b9e8e95ae63601d2010a38..bca25fad189a9d4ed7e826915a5abd5c460feddd 100644 (file)
@@ -12,6 +12,7 @@
 
 #include <cstddef>
 #include <vector>
+#include <deque>
 #include <tuple>
 #include <utility>
 #include <functional>
@@ -67,14 +68,14 @@ namespace EC
         template <typename... Types>
         struct Storage
         {
-            using type = std::tuple<std::vector<Types>..., std::vector<char> >;
+            using type = std::tuple<std::deque<Types>..., std::deque<char> >;
         };
         using ComponentsStorage =
             typename EC::Meta::Morph<ComponentsList, Storage<> >::type;
 
         // Entity: isAlive, ComponentsTags Info
         using EntitiesTupleType = std::tuple<bool, BitsetType>;
-        using EntitiesType = std::vector<EntitiesTupleType>;
+        using EntitiesType = std::deque<EntitiesTupleType>;
 
         EntitiesType entities;
         ComponentsStorage componentsStorage;
@@ -103,7 +104,7 @@ namespace EC
             }
 
             EC::Meta::forEach<ComponentsList>([this, newCapacity] (auto t) {
-                std::get<std::vector<decltype(t)> >(
+                std::get<std::deque<decltype(t)> >(
                     this->componentsStorage).resize(newCapacity);
             });
 
@@ -410,10 +411,10 @@ namespace EC
             constexpr auto index =
                 EC::Meta::IndexOf<Component, Components>::value;
 
-            // Cast required due to compiler thinking that vector<char> at
+            // Cast required due to compiler thinking that deque<char> at
             // index = Components::size is being used, even if the previous
             // if statement will prevent this from ever happening.
-            (*((std::vector<Component>*)(&std::get<index>(
+            (*((std::deque<Component>*)(&std::get<index>(
                 componentsStorage
             ))))[entityID] = std::move(component);
         }