Explorar o código

group: added front/back (close #393)

Michele Caini %!s(int64=6) %!d(string=hai) anos
pai
achega
0be7494042
Modificáronse 2 ficheiros con 84 adicións e 0 borrados
  1. 40 0
      src/entt/entity/group.hpp
  2. 44 0
      test/entt/entity/group.cpp

+ 40 - 0
src/entt/entity/group.hpp

@@ -232,6 +232,26 @@ public:
         return handler->end();
     }
 
+    /**
+     * @brief Returns the first entity that has the given components, if any.
+     * @return The first entity that has the given components if one exists, the
+     * null entity otherwise.
+     */
+    entity_type front() const {
+        const auto it = begin();
+        return it != end() ? *it : null;
+    }
+
+    /**
+     * @brief Returns the last entity that has the given components, if any.
+     * @return The last entity that has the given components if one exists, the
+     * null entity otherwise.
+     */
+    entity_type back() const {
+        const auto it = std::make_reverse_iterator(end());
+        return it != std::make_reverse_iterator(begin()) ? *it : null;
+    }
+
     /**
      * @brief Finds an entity.
      * @param entt A valid entity identifier.
@@ -648,6 +668,26 @@ public:
         return std::get<0>(pools)->sparse_set<entity_type>::end();
     }
 
+    /**
+     * @brief Returns the first entity that has the given components, if any.
+     * @return The first entity that has the given components if one exists, the
+     * null entity otherwise.
+     */
+    entity_type front() const {
+        const auto it = begin();
+        return it != end() ? *it : null;
+    }
+
+    /**
+     * @brief Returns the last entity that has the given components, if any.
+     * @return The last entity that has the given components if one exists, the
+     * null entity otherwise.
+     */
+    entity_type back() const {
+        const auto it = std::make_reverse_iterator(end());
+        return it != std::make_reverse_iterator(begin()) ? *it : null;
+    }
+
     /**
      * @brief Finds an entity.
      * @param entt A valid entity identifier.

+ 44 - 0
test/entt/entity/group.cpp

@@ -490,6 +490,28 @@ TEST(NonOwningGroup, Less) {
     registry.group(entt::get<int, char, double>).less([](const auto, int, char, double) { FAIL(); });
 }
 
+TEST(NonOwningGroup, FrontBack) {
+    entt::registry registry;
+    auto group = registry.group<>(entt::get<const int, const char>);
+
+    ASSERT_EQ(group.front(), static_cast<entt::entity>(entt::null));
+    ASSERT_EQ(group.back(), static_cast<entt::entity>(entt::null));
+
+    const auto e0 = registry.create();
+    registry.assign<int>(e0);
+    registry.assign<char>(e0);
+
+    const auto e1 = registry.create();
+    registry.assign<int>(e1);
+    registry.assign<char>(e1);
+
+    const auto entity = registry.create();
+    registry.assign<char>(entity);
+
+    ASSERT_EQ(group.front(), e1);
+    ASSERT_EQ(group.back(), e0);
+}
+
 TEST(NonOwningGroup, SignalRace) {
     entt::registry registry;
     registry.on_construct<double>().connect<&entt::registry::assign_or_replace<int>>();
@@ -1066,6 +1088,28 @@ TEST(OwningGroup, Less) {
     registry.group<double>(entt::get<int, char>).less([](const auto, double, int, char) { FAIL(); });
 }
 
+TEST(OwningGroup, FrontBack) {
+    entt::registry registry;
+    auto group = registry.group<const char>(entt::get<const int>);
+
+    ASSERT_EQ(group.front(), static_cast<entt::entity>(entt::null));
+    ASSERT_EQ(group.back(), static_cast<entt::entity>(entt::null));
+
+    const auto e0 = registry.create();
+    registry.assign<int>(e0);
+    registry.assign<char>(e0);
+
+    const auto e1 = registry.create();
+    registry.assign<int>(e1);
+    registry.assign<char>(e1);
+
+    const auto entity = registry.create();
+    registry.assign<char>(entity);
+
+    ASSERT_EQ(group.front(), e1);
+    ASSERT_EQ(group.back(), e0);
+}
+
 TEST(OwningGroup, SignalRace) {
     entt::registry registry;
     registry.on_construct<double>().connect<&entt::registry::assign_or_replace<int>>();