Michele Caini 6 лет назад
Родитель
Сommit
df55f338ca
2 измененных файлов с 79 добавлено и 0 удалено
  1. 40 0
      src/entt/entity/view.hpp
  2. 39 0
      test/entt/entity/view.cpp

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

@@ -333,6 +333,26 @@ public:
         return iterator_type{view, unchecked(view), ignore, view->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.
@@ -654,6 +674,26 @@ public:
         return pool->sparse_set<Entity>::end();
     }
 
+    /**
+     * @brief Returns the first entity that has the given component, if any.
+     * @return The first entity that has the given component 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 component, if any.
+     * @return The last entity that has the given component 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.

+ 39 - 0
test/entt/entity/view.cpp

@@ -220,6 +220,23 @@ TEST(SingleComponentView, Less) {
     });
 }
 
+TEST(SingleComponentView, FrontBack) {
+    entt::registry registry;
+    auto view = registry.view<const int>();
+
+    ASSERT_EQ(view.front(), static_cast<entt::entity>(entt::null));
+    ASSERT_EQ(view.back(), static_cast<entt::entity>(entt::null));
+
+    const auto e0 = registry.create();
+    registry.assign<int>(e0);
+
+    const auto e1 = registry.create();
+    registry.assign<int>(e1);
+
+    ASSERT_EQ(view.front(), e1);
+    ASSERT_EQ(view.back(), e0);
+}
+
 TEST(MultiComponentView, Functionalities) {
     entt::registry registry;
     auto view = registry.view<int, char>();
@@ -569,3 +586,25 @@ TEST(MultiComponentView, Less) {
         ASSERT_EQ(entity, entt);
     });
 }
+
+TEST(MultiComponentView, FrontBack) {
+    entt::registry registry;
+    auto view = registry.view<const int, const char>();
+
+    ASSERT_EQ(view.front(), static_cast<entt::entity>(entt::null));
+    ASSERT_EQ(view.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(view.front(), e1);
+    ASSERT_EQ(view.back(), e0);
+}