diff --git a/TODO b/TODO index a621cd337..157ada9dc 100644 --- a/TODO +++ b/TODO @@ -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 diff --git a/src/entt/entity/sparse_set.hpp b/src/entt/entity/sparse_set.hpp index 6d738cb17..50d6597b7 100644 --- a/src/entt/entity/sparse_set.hpp +++ b/src/entt/entity/sparse_set.hpp @@ -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. * diff --git a/test/entt/entity/sparse_set.cpp b/test/entt/entity/sparse_set.cpp index ee32e5c5e..b1e34bcd3 100644 --- a/test/entt/entity/sparse_set.cpp +++ b/test/entt/entity/sparse_set.cpp @@ -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::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::null)); + ASSERT_EQ(set.at(1u), static_cast(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::null)); ASSERT_TRUE(std::is_move_constructible_v); ASSERT_TRUE(std::is_move_assignable_v); @@ -59,8 +63,12 @@ TEST(SparseSet, Functionalities) { other = std::move(set); ASSERT_TRUE(set.empty()); + ASSERT_EQ(set.at(0u), static_cast(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::null)); other.clear();