From 036ef25da9d9824d2c5a8b9c8a05e8631efdc8d3 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Thu, 26 Dec 2019 00:07:24 +0100 Subject: [PATCH] registry: uses views to initialize groups --- TODO | 3 +-- src/entt/entity/registry.hpp | 26 ++++++++------------------ 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/TODO b/TODO index 833531de3..160f79e9f 100644 --- a/TODO +++ b/TODO @@ -22,5 +22,4 @@ * remove duktape example (and eventually provide a new one)? * review and suppress warnings, if any * extract only the type within type_info, hash its name (more portable) -* registry - - ::group improve, reduce code +* registry: groups can break after a discard -> cleanup them diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index 99c04e1dc..f8319308d 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -1333,26 +1333,16 @@ public: (std::get &>(cpools).on_destroy().before(discard_if).template connect<&handler_type::discard_if>(*handler), ...); (std::get &>(cpools).on_construct().before(discard_if).template connect<&handler_type::discard_if>(*handler), ...); - const auto &cpool = std::min({ - static_cast &>(std::get &>(cpools))..., - static_cast &>(std::get &>(cpools))... - }, [](const auto &lhs, const auto &rhs) { - return lhs.size() < rhs.size(); - }); + auto init = view(entt::exclude); // we cannot iterate backwards because we want to leave behind valid entities in case of owned types - std::for_each(cpool.data(), cpool.data() + cpool.size(), [cpools, handler](const auto entity) { - if((std::get &>(cpools).has(entity) && ...) - && (std::get &>(cpools).has(entity) && ...) - && !(std::get &>(cpools).has(entity) || ...)) - { - if constexpr(sizeof...(Owned) == 0) { - handler->owned.construct(entity); - } else { - if(!(std::get<0>(cpools).index(entity) < handler->owned)) { - const auto pos = handler->owned++; - (std::get &>(cpools).swap(std::get &>(cpools).data()[pos], entity), ...); - } + std::for_each(std::make_reverse_iterator(init.end()), std::make_reverse_iterator(init.begin()), [handler](const auto entity) { + if constexpr(sizeof...(Owned) == 0) { + handler->owned.construct(entity); + } else { + if(!(std::get<0>(handler->cpools).index(entity) < handler->owned)) { + const auto pos = handler->owned++; + (std::get &>(handler->cpools).swap(std::get &>(handler->cpools).data()[pos], entity), ...); } } });