From 7fe035ce4b95bcba7f8e6d2b3c86ce2a13d0cadd Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Tue, 14 Mar 2023 11:38:41 +0100 Subject: [PATCH] group: move group size from registry group_data to basic_group_handler --- src/entt/entity/group.hpp | 25 ++++++++++++++++++++----- src/entt/entity/registry.hpp | 21 +++++++-------------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/entt/entity/group.hpp b/src/entt/entity/group.hpp index 17694280e..bdb214418 100644 --- a/src/entt/entity/group.hpp +++ b/src/entt/entity/group.hpp @@ -90,7 +90,19 @@ template return !(lhs == rhs); } -struct basic_group_handler {}; +struct basic_group_handler { + using size_type = std::size_t; + + basic_group_handler(const size_type cnt) + : sz{cnt} {} + + size_type size() const noexcept { + return sz; + } + +private: + const size_type sz; +}; template class group_handler; @@ -112,8 +124,10 @@ public: using entity_type = underlying_type; group_handler(Owned &...opool, Get &...gpool, Exclude &...epool) - : pools{&opool..., &gpool...}, - filter{&epool...} {} + : basic_group_handler{sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude)}, + pools{&opool..., &gpool...}, + filter{&epool...}, + len{} {} template void push_if(const entity_type entt) { @@ -137,7 +151,7 @@ public: private: std::tuple pools; std::tuple filter; - std::size_t len{}; + std::size_t len; }; template @@ -151,7 +165,8 @@ public: template group_handler(const Alloc &alloc, Get &...gpool, Exclude &...epool) - : pools{&gpool...}, + : basic_group_handler{sizeof...(Get) + sizeof...(Exclude)}, + pools{&gpool...}, filter{&epool...}, elem{alloc} {} diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index cc2f9a31d..66366e791 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -247,7 +247,6 @@ class basic_registry { using storage_for_type = typename storage_for>>::type; struct group_data { - std::size_t size; std::shared_ptr handler; bool (*owned)(const id_type) noexcept; bool (*get)(const id_type) noexcept; @@ -1212,15 +1211,12 @@ public: [[nodiscard]] basic_group, storage_for_type...>, get_t...>, exclude_t...>> group(get_t = {}, exclude_t = {}) { using handler_type = typename basic_group, storage_for_type...>, get_t...>, exclude_t...>>::handler; - - constexpr auto size = 1u + sizeof...(Other) + sizeof...(Get) + sizeof...(Exclude); handler_type *handler = nullptr; if(auto it = groups.find(type_hash::value()); it != groups.cend()) { handler = static_cast(it->second.handler.get()); } else { group_data candidate = { - size, std::allocate_shared(get_allocator(), assure>(), assure>()..., assure>()..., assure>()...), []([[maybe_unused]] const id_type ctype) noexcept { return ((ctype == type_hash>::value()) || ... || (ctype == type_hash>::value())); }, []([[maybe_unused]] const id_type ctype) noexcept { return ((ctype == type_hash>::value()) || ...); }, @@ -1230,10 +1226,10 @@ public: handler = static_cast(candidate.handler.get()); groups.emplace(type_hash::value(), std::move(candidate)); - ENTT_ASSERT(std::all_of(groups.cbegin(), groups.cend(), [size](const auto &data) { + ENTT_ASSERT(std::all_of(groups.cbegin(), groups.cend(), [size = handler->size()](const auto &data) { const auto overlapping = (data.second.owned(type_hash>::value()) + ... + data.second.owned(type_hash>::value())); const auto sz = overlapping + (0u + ... + data.second.get(type_hash>::value())) + (0u + ... + data.second.exclude(type_hash>::value())); - return !overlapping || ((sz == size) || (sz == data.second.size)); + return !overlapping || ((sz == size) || (sz == data.second.handler->size())); }), "Conflicting groups"); @@ -1245,14 +1241,14 @@ public: for(auto &&data: groups) { if((data.second.owned(type_hash>::value()) + ... + data.second.owned(type_hash>::value()))) { - if(data.second.size < size && (prev == nullptr || prev_len < data.second.size)) { - prev_len = data.second.size; + if(const auto sz = data.second.handler->size(); sz < handler->size() && (prev == nullptr || prev_len < sz)) { prev = data.second.handler.get(); + prev_len = sz; } - if(data.second.size > size && (next == nullptr || next_len > data.second.size)) { - next_len = data.second.size; + if(const auto sz = data.second.handler->size(); sz > handler->size() && (next == nullptr || next_len > sz)) { next = data.second.handler.get(); + next_len = sz; } } } @@ -1289,15 +1285,12 @@ public: [[nodiscard]] basic_group, get_t, storage_for_type...>, exclude_t...>> group(get_t, exclude_t = {}) { using handler_type = typename basic_group, get_t, storage_for_type...>, exclude_t...>>::handler; - - constexpr auto size = 1u + sizeof...(Other) + sizeof...(Exclude); handler_type *handler = nullptr; if(auto it = groups.find(type_hash::value()); it != groups.cend()) { handler = static_cast(it->second.handler.get()); } else { group_data candidate = { - size, std::allocate_shared(get_allocator(), get_allocator(), assure>(), assure>()..., assure>()...), []([[maybe_unused]] const id_type ctype) noexcept { return false; }, []([[maybe_unused]] const id_type ctype) noexcept { return ((ctype == type_hash>::value()) || ... || (ctype == type_hash>::value())); }, @@ -1357,7 +1350,7 @@ public: template [[nodiscard]] bool sortable(const basic_group, get_t, exclude_t> &) noexcept { constexpr auto size = sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude); - auto pred = [size](const auto &data) { return (0u + ... + data.second.owned(type_hash::value())) && (size < data.second.size); }; + auto pred = [size](const auto &data) { return (0u + ... + data.second.owned(type_hash::value())) && (size < data.second.handler->size()); }; return std::find_if(groups.cbegin(), groups.cend(), std::move(pred)) == groups.cend(); }