瀏覽代碼

sparse_set: added unchecked operator[](pos)

Michele Caini 5 年之前
父節點
當前提交
86e18f68c4
共有 3 個文件被更改,包括 17 次插入3 次删除
  1. 12 2
      src/entt/entity/sparse_set.hpp
  2. 2 1
      src/entt/entity/storage.hpp
  3. 3 0
      test/entt/entity/sparse_set.cpp

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

@@ -379,14 +379,24 @@ public:
     }
 
     /**
-     * @brief Returns the entity that occupies a given position in the storage.
+     * @brief Returns the entity at specified location, with bounds checking.
      * @param pos The position for which to return the entity.
-     * @return The entity that occupies the given position in the storage.
+     * @return The entity at specified location if any, a null entity otherwise.
      */
     [[nodiscard]] entity_type at(const size_type pos) const {
         return pos < packed.size() ? packed[pos] : null;
     }
 
+    /**
+     * @brief Returns the entity at specified location, without bounds checking.
+     * @param pos The position for which to return the entity.
+     * @return The entity at specified location.
+     */
+    [[nodiscard]] entity_type operator[](const size_type pos) const {
+        ENTT_ASSERT(pos < packed.size());
+        return packed[pos];
+    }
+
     /**
      * @brief Assigns an entity to a sparse set.
      *

+ 2 - 1
src/entt/entity/storage.hpp

@@ -651,7 +651,8 @@ class sigh_storage_mixin final: public Type {
      */
     void swap_and_pop(const std::size_t pos, void *ud) final {
         ENTT_ASSERT(ud != nullptr);
-        destruction.publish(*static_cast<basic_registry<typename Type::entity_type> *>(ud), this->data()[pos]);
+        const auto entity = basic_sparse_set<typename Type::entity_type>::operator[](pos);
+        destruction.publish(*static_cast<basic_registry<typename Type::entity_type> *>(ud), entity);
         Type::swap_and_pop(pos, ud);
     }
 

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

@@ -35,6 +35,7 @@ TEST(SparseSet, Functionalities) {
     ASSERT_EQ(set.index(entt::entity{42}), 0u);
     ASSERT_EQ(set.at(0u), entt::entity{42});
     ASSERT_EQ(set.at(1u), static_cast<entt::entity>(entt::null));
+    ASSERT_EQ(set[0u], entt::entity{42});
 
     set.remove(entt::entity{42});
 
@@ -53,6 +54,7 @@ TEST(SparseSet, Functionalities) {
     ASSERT_EQ(set.index(entt::entity{42}), 0u);
     ASSERT_EQ(set.at(0u), entt::entity{42});
     ASSERT_EQ(set.at(1u), static_cast<entt::entity>(entt::null));
+    ASSERT_EQ(set[0u], entt::entity{42});
 
     ASSERT_TRUE(std::is_move_constructible_v<decltype(set)>);
     ASSERT_TRUE(std::is_move_assignable_v<decltype(set)>);
@@ -69,6 +71,7 @@ TEST(SparseSet, Functionalities) {
     ASSERT_EQ(other.index(entt::entity{42}), 0u);
     ASSERT_EQ(other.at(0u), entt::entity{42});
     ASSERT_EQ(other.at(1u), static_cast<entt::entity>(entt::null));
+    ASSERT_EQ(other[0u], entt::entity{42});
 
     other.clear();