Просмотр исходного кода

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

Michele Caini 5 лет назад
Родитель
Сommit
3118cd4fa5
3 измененных файлов с 22 добавлено и 2 удалено
  1. 3 0
      TODO
  2. 9 0
      src/entt/entity/sparse_set.hpp
  3. 10 2
      test/entt/entity/sparse_set.cpp

+ 3 - 0
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

+ 9 - 0
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.
      *

+ 10 - 2
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::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();