From df55f338ca41c4d6359ee4b1c03288367b648101 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Wed, 5 Feb 2020 23:40:32 +0100 Subject: [PATCH] view: added front/back (see #393) --- src/entt/entity/view.hpp | 40 +++++++++++++++++++++++++++++++++++++++ test/entt/entity/view.cpp | 39 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/src/entt/entity/view.hpp b/src/entt/entity/view.hpp index 827673554..bdf8c21b2 100644 --- a/src/entt/entity/view.hpp +++ b/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::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. diff --git a/test/entt/entity/view.cpp b/test/entt/entity/view.cpp index 5f51c3858..476141782 100644 --- a/test/entt/entity/view.cpp +++ b/test/entt/entity/view.cpp @@ -220,6 +220,23 @@ TEST(SingleComponentView, Less) { }); } +TEST(SingleComponentView, FrontBack) { + entt::registry registry; + auto view = registry.view(); + + ASSERT_EQ(view.front(), static_cast(entt::null)); + ASSERT_EQ(view.back(), static_cast(entt::null)); + + const auto e0 = registry.create(); + registry.assign(e0); + + const auto e1 = registry.create(); + registry.assign(e1); + + ASSERT_EQ(view.front(), e1); + ASSERT_EQ(view.back(), e0); +} + TEST(MultiComponentView, Functionalities) { entt::registry registry; auto view = registry.view(); @@ -569,3 +586,25 @@ TEST(MultiComponentView, Less) { ASSERT_EQ(entity, entt); }); } + +TEST(MultiComponentView, FrontBack) { + entt::registry registry; + auto view = registry.view(); + + ASSERT_EQ(view.front(), static_cast(entt::null)); + ASSERT_EQ(view.back(), static_cast(entt::null)); + + const auto e0 = registry.create(); + registry.assign(e0); + registry.assign(e0); + + const auto e1 = registry.create(); + registry.assign(e1); + registry.assign(e1); + + const auto entity = registry.create(); + registry.assign(entity); + + ASSERT_EQ(view.front(), e1); + ASSERT_EQ(view.back(), e0); +}