diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index d45bcc262..150978d9c 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -114,43 +114,12 @@ class basic_registry { void(* stamp)(basic_registry &, const Entity, const sparse_set &, const Entity); }; - struct group_data { - const std::size_t extent[3]; - - group_data(const std::size_t osize, const std::size_t gsize, const std::size_t esize) - : extent{osize, gsize, esize} - {} - - virtual ~group_data() = default; - - virtual bool owned(const ENTT_ID_TYPE) ENTT_NOEXCEPT = 0; - virtual bool get(const ENTT_ID_TYPE) ENTT_NOEXCEPT = 0; - virtual bool exclude(const ENTT_ID_TYPE) ENTT_NOEXCEPT = 0; - }; - template struct group_handler; template - struct group_handler, get_t, Owned...>: group_data { - std::conditional_t, std::size_t> current; - - group_handler() - : group_data{sizeof...(Owned), sizeof...(Get), sizeof...(Exclude)}, - current{} - {} - - bool owned(const ENTT_ID_TYPE ctype) ENTT_NOEXCEPT override { - return ((ctype == type_info::id()) || ...); - } - - bool get(const ENTT_ID_TYPE ctype) ENTT_NOEXCEPT override { - return ((ctype == type_info::id()) || ...); - } - - bool exclude(const ENTT_ID_TYPE ctype) ENTT_NOEXCEPT override { - return ((ctype == type_info::id()) || ...); - } + struct group_handler, get_t, Owned...> { + std::conditional_t, std::size_t> current{}; template void maybe_valid_if(const Entity entt, basic_registry &owner) { @@ -187,6 +156,14 @@ class basic_registry { } }; + struct group_data { + std::size_t size; + std::unique_ptr group; + bool (* owned)(const ENTT_ID_TYPE) ENTT_NOEXCEPT; + bool (* get)(const ENTT_ID_TYPE) ENTT_NOEXCEPT; + bool (* exclude)(const ENTT_ID_TYPE) ENTT_NOEXCEPT; + }; + struct basic_variable { virtual ~basic_variable() = default; virtual ENTT_ID_TYPE id() const ENTT_NOEXCEPT = 0; @@ -1283,51 +1260,54 @@ public: using handler_type = group_handler, get_t...>, std::decay_t...>; - [[maybe_unused]] constexpr auto size = sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude); const auto cpools = std::forward_as_tuple(assure>()..., assure>()...); - const std::size_t extent[3]{sizeof...(Owned), sizeof...(Get), sizeof...(Exclude)}; + constexpr auto size = sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude); handler_type *handler = nullptr; - if(auto it = std::find_if(groups.cbegin(), groups.cend(), [&extent](const auto &curr) { - return std::equal(std::begin(extent), std::end(extent), std::begin(curr->extent)) - && (curr->owned(type_info>::id()) && ...) - && (curr->get(type_info>::id()) && ...) - && (curr->exclude(type_info::id()) && ...); + if(auto it = std::find_if(groups.cbegin(), groups.cend(), [](const auto &gdata) { + return gdata.size == size + && (gdata.owned(type_info>::id()) && ...) + && (gdata.get(type_info>::id()) && ...) + && (gdata.exclude(type_info::id()) && ...); }); it != groups.cend()) { - handler = static_cast(it->get()); + handler = static_cast(it->group.get()); } if(!handler) { - auto grp = std::make_unique(); + group_data gdata = { + size, + { new handler_type{}, [](void *instance) { delete static_cast(instance); } }, + [](const ENTT_ID_TYPE ctype) ENTT_NOEXCEPT { return ((ctype == type_info>::id()) || ...); }, + [](const ENTT_ID_TYPE ctype) ENTT_NOEXCEPT { return ((ctype == type_info>::id()) || ...); }, + [](const ENTT_ID_TYPE ctype) ENTT_NOEXCEPT { return ((ctype == type_info::id()) || ...); }, + }; + + handler = static_cast(gdata.group.get()); + const void *maybe_valid_if = nullptr; const void *discard_if = nullptr; if constexpr(sizeof...(Owned) == 0) { - handler = static_cast(groups.emplace_back(std::move(grp)).get()); + groups.push_back(std::move(gdata)); } else { - ENTT_ASSERT(std::all_of(groups.cbegin(), groups.cend(), [&extent](const auto &curr) { - const std::size_t diff[3]{ - (0u + ... + curr->owned(type_info>::id())), - (0u + ... + curr->get(type_info>::id())), - (0u + ... + curr->exclude(type_info::id())) - }; - - return !diff[0] || ((std::equal(std::begin(diff), std::end(diff), extent) || std::equal(std::begin(diff), std::end(diff), curr->extent))); + ENTT_ASSERT(std::all_of(groups.cbegin(), groups.cend(), [](const auto &gdata) { + const auto overlapping = (0u + ... + gdata.owned(type_info>::id())); + const auto sz = overlapping + (0u + ... + gdata.get(type_info>::id())) + (0u + ... + gdata.exclude(type_info::id())); + return !overlapping || ((sz == size) || (sz == gdata.size)); })); - const auto next = std::find_if_not(groups.cbegin(), groups.cend(), [&size](const auto &curr) { - const std::size_t diff = (0u + ... + curr->owned(type_info>::id())); - return !diff || (size > (curr->extent[0] + curr->extent[1] + curr->extent[2])); + const auto next = std::find_if_not(groups.cbegin(), groups.cend(), [&size](const auto &gdata) { + return !(0u + ... + gdata.owned(type_info>::id())) || (size > (gdata.size)); }); - const auto prev = std::find_if(std::make_reverse_iterator(next), groups.crend(), [](const auto &curr) { - return (0u + ... + curr->owned(type_info>::id())); + const auto prev = std::find_if(std::make_reverse_iterator(next), groups.crend(), [](const auto &gdata) { + return (0u + ... + gdata.owned(type_info>::id())); }); - maybe_valid_if = (next == groups.cend() ? maybe_valid_if : next->get()); - discard_if = (prev == groups.crend() ? discard_if : prev->get()); - handler = static_cast(groups.insert(next, std::move(grp))->get()); + maybe_valid_if = (next == groups.cend() ? maybe_valid_if : next->group.get()); + discard_if = (prev == groups.crend() ? discard_if : prev->group.get()); + groups.insert(next, std::move(gdata)); } ((std::get &>(cpools).super = std::max(std::get &>(cpools).super, size)), ...); @@ -1713,8 +1693,8 @@ public: } private: + std::vector groups{}; mutable std::vector pools{}; - std::vector> groups{}; std::vector> vars{}; std::vector entities{}; entity_type destroyed{null};