sparse_set: added ::at to get the entity at a given position, if any

This commit is contained in:
Michele Caini
2021-02-15 14:56:18 +01:00
parent 18832fcb37
commit 3118cd4fa5
3 changed files with 22 additions and 2 deletions

3
TODO
View File

@@ -18,6 +18,9 @@
- ...
WIP:
* HP: remove non-const registry::storage function?
* HP: weak reference wrapper example with custom storage.
* HP: review ENTT_PAGE_SIZE, seems "wrong" (see sparse_set)
* HP: as_ref should be a qualified function, not a global one (no breaking change, ADL makes it work anyway)
* HP: merge view and view pack
* HP: invalid view auto-refresh

View File

@@ -374,6 +374,15 @@ public:
return size_type{to_integral(sparse[page(entt)][offset(entt)])};
}
/**
* @brief Returns the entity that occupies a given position in the storage.
* @param pos The position for which to return the entity.
* @return The entity that occupies the given position in the storage.
*/
[[nodiscard]] entity_type at(const size_type pos) const {
return pos < packed.size() ? packed[pos] : null;
}
/**
* @brief Assigns an entity to a sparse set.
*

View File

@@ -26,8 +26,6 @@ TEST(SparseSet, Functionalities) {
set.emplace(entt::entity{42});
ASSERT_EQ(set.index(entt::entity{42}), 0u);
ASSERT_FALSE(set.empty());
ASSERT_EQ(set.size(), 1u);
ASSERT_NE(std::as_const(set).begin(), std::as_const(set).end());
@@ -35,6 +33,8 @@ TEST(SparseSet, Functionalities) {
ASSERT_FALSE(set.contains(entt::entity{0}));
ASSERT_TRUE(set.contains(entt::entity{42}));
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));
set.remove(entt::entity{42});
@@ -44,11 +44,15 @@ TEST(SparseSet, Functionalities) {
ASSERT_EQ(set.begin(), set.end());
ASSERT_FALSE(set.contains(entt::entity{0}));
ASSERT_FALSE(set.contains(entt::entity{42}));
ASSERT_EQ(set.at(0u), static_cast<entt::entity>(entt::null));
ASSERT_EQ(set.at(1u), static_cast<entt::entity>(entt::null));
set.emplace(entt::entity{42});
ASSERT_FALSE(set.empty());
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_TRUE(std::is_move_constructible_v<decltype(set)>);
ASSERT_TRUE(std::is_move_assignable_v<decltype(set)>);
@@ -59,8 +63,12 @@ TEST(SparseSet, Functionalities) {
other = std::move(set);
ASSERT_TRUE(set.empty());
ASSERT_EQ(set.at(0u), static_cast<entt::entity>(entt::null));
ASSERT_FALSE(other.empty());
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));
other.clear();