sparse_set: added unchecked operator[](pos)

This commit is contained in:
Michele Caini
2021-02-23 15:01:05 +01:00
parent cc7d9e03d9
commit 86e18f68c4
3 changed files with 17 additions and 3 deletions

View File

@@ -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.
*

View File

@@ -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);
}

View File

@@ -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();