ソースを参照

sparse_set: minor changes

Michele Caini 4 年 前
コミット
bb1b0e0581
2 ファイル変更10 行追加12 行削除
  1. 10 8
      src/entt/entity/sparse_set.hpp
  2. 0 4
      test/entt/entity/sparse_set.cpp

+ 10 - 8
src/entt/entity/sparse_set.hpp

@@ -163,10 +163,9 @@ class basic_sparse_set {
     using allocator_traits = std::allocator_traits<Allocator>;
     using alloc = typename allocator_traits::template rebind_alloc<Entity>;
     using alloc_traits = typename std::allocator_traits<alloc>;
-    using alloc_page = typename allocator_traits::template rebind_alloc<typename alloc_traits::pointer>;
 
     using entity_traits = entt_traits<Entity>;
-    using sparse_container_type = std::vector<typename alloc_traits::pointer, alloc_page>;
+    using sparse_container_type = std::vector<typename alloc_traits::pointer, typename alloc_traits::template rebind_alloc<typename alloc_traits::pointer>>;
     using packed_container_type = std::vector<Entity, alloc>;
 
     [[nodiscard]] auto sparse_ptr(const Entity entt) const {
@@ -182,11 +181,13 @@ class basic_sparse_set {
     }
 
     void release_sparse_pages() {
-        for(size_type pos{}, last = sparse.size(); pos < last; ++pos) {
-            if(sparse[pos]) {
-                std::destroy(sparse[pos], sparse[pos] + sparse_page_v);
-                alloc_traits::deallocate(packed.get_allocator(), sparse[pos], sparse_page_v);
-                sparse[pos] = nullptr;
+        auto page_allocator{packed.get_allocator()};
+
+        for(auto &&page: sparse) {
+            if(page != nullptr) {
+                std::destroy(page, page + sparse_page_v);
+                alloc_traits::deallocate(page_allocator, page, sparse_page_v);
+                page = nullptr;
             }
         }
     }
@@ -247,7 +248,8 @@ protected:
         }
 
         if(!sparse[page]) {
-            sparse[page] = alloc_traits::allocate(packed.get_allocator(), sparse_page_v);
+            auto page_allocator{packed.get_allocator()};
+            sparse[page] = alloc_traits::allocate(page_allocator, sparse_page_v);
             std::uninitialized_fill(sparse[page], sparse[page] + sparse_page_v, null);
         }
 

+ 0 - 4
test/entt/entity/sparse_set.cpp

@@ -1238,14 +1238,12 @@ TEST(SparseSet, ThrowingAllocator) {
 
     test::throwing_allocator<entt::entity>::trigger_on_allocate = true;
 
-    // strong exception safety
     ASSERT_THROW(set.reserve(1u), test::throwing_allocator<entt::entity>::exception_type);
     ASSERT_EQ(set.capacity(), 0u);
     ASSERT_EQ(set.extent(), 0u);
 
     test::throwing_allocator<entt::entity>::trigger_on_allocate = true;
 
-    // strong exception safety
     ASSERT_THROW(set.emplace(entt::entity{0}), test::throwing_allocator<entt::entity>::exception_type);
     ASSERT_EQ(set.extent(), ENTT_SPARSE_PAGE);
     ASSERT_EQ(set.capacity(), 0u);
@@ -1253,7 +1251,6 @@ TEST(SparseSet, ThrowingAllocator) {
     set.emplace(entt::entity{0});
     test::throwing_allocator<entt::entity>::trigger_on_allocate = true;
 
-    // strong exception safety
     ASSERT_THROW(set.reserve(2u), test::throwing_allocator<entt::entity>::exception_type);
     ASSERT_EQ(set.extent(), ENTT_SPARSE_PAGE);
     ASSERT_TRUE(set.contains(entt::entity{0}));
@@ -1262,7 +1259,6 @@ TEST(SparseSet, ThrowingAllocator) {
     entt::entity entities[2u]{entt::entity{1}, entt::entity{ENTT_SPARSE_PAGE}};
     test::throwing_allocator<entt::entity>::trigger_after_allocate = true;
 
-    // basic exception safety
     ASSERT_THROW(set.insert(std::begin(entities), std::end(entities)), test::throwing_allocator<entt::entity>::exception_type);
     ASSERT_EQ(set.extent(), 2 * ENTT_SPARSE_PAGE);
     ASSERT_TRUE(set.contains(entt::entity{0}));