diff --git a/TODO b/TODO index 0681e1a2d..5933aeaa2 100644 --- a/TODO +++ b/TODO @@ -23,6 +23,7 @@ * meta: opaque references and pointers TODO -* registry::sort also for types that are part of a group (untracked items only) * add sort by type to the non-owning group * custom (decoupled) pools ==> double buffering, shared components, multi-model +* registry.each(first, last) by iterators, entities/components guaranteed +* non-owning groups shouldn't store entities, but positions => optimized each diff --git a/docs/md/entity.md b/docs/md/entity.md index c450ef282..d21542e7d 100644 --- a/docs/md/entity.md +++ b/docs/md/entity.md @@ -1373,9 +1373,11 @@ them as fast as possible. Sorting owned components is no longer allowed once the group has been created. However, full-owning groups can be sorted by means of their `sort` member -functions, if required. Sorting a full-owning group affects all the instance of +functions, if required. Sorting a full-owning group affects all the instances of the same group (it means that users don't have to call `sort` on each instance -to sort all of them because they share the underlying data structure). +to sort all of them because they share the underlying data structure).
+The elements that aren't part of the group can still be sorted separately for +each pool using the `sort` member function of the registry. ### Partial-owning groups @@ -1404,9 +1406,13 @@ doesn't pass to the group instead. Sorting owned components is no longer allowed once the group has been created. However, partial-owning groups can be sorted by means of their `sort` member -functions, if required. Sorting a partial-owning group affects all the instance +functions, if required. Sorting a partial-owning group affects all the instances of the same group (it means that users don't have to call `sort` on each -instance to sort all of them because they share the underlying data structure). +instance to sort all of them because they share the underlying data +structure).
+Regarding the owned types, the elements that aren't part of the group can still +be sorted separately for each pool using the `sort` member function of the +registry. ### Non-owning groups diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index c22114bcc..0bf526702 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -45,9 +45,13 @@ class basic_registry { using component_family = family; using traits_type = entt_traits>; + struct group_type { + std::size_t owned{}; + }; + template struct pool_handler: storage { - void *group{}; + group_type *group{}; pool_handler() ENTT_NOEXCEPT = default; @@ -164,9 +168,8 @@ class basic_registry { }; template - struct group_handler, type_list, Owned...> { + struct group_handler, type_list, Owned...>: group_type { std::tuple *..., pool_type *..., pool_type *...> cpools{}; - std::size_t owned{}; template void maybe_valid_if(const basic_registry &, const Entity entt) { @@ -1019,9 +1022,10 @@ public: * this member function. * * @warning - * Pools of components that are owned by a group cannot be sorted.
- * An assertion will abort the execution at runtime in debug mode in case - * the pool is owned by a group. + * Pools of components owned by a group are only partially sorted.
+ * In other words, only the elements that aren't part of the group are + * sorted by this function. Use the `sort` member function of a group to + * sort the other half of the pool. * * @tparam Component Type of components to sort. * @tparam Compare Type of comparison function object. @@ -1033,9 +1037,12 @@ public: */ template void sort(Compare compare, Sort algo = Sort{}, Args &&... args) { - ENTT_ASSERT(!owned()); - auto *cpool = assure(); - cpool->sort(cpool->begin(), cpool->end(), std::move(compare), std::move(algo), std::forward(args)...); + if(auto *cpool = assure(); cpool->group) { + const auto last = cpool->end() - cpool->group->owned; + cpool->sort(cpool->begin(), last, std::move(compare), std::move(algo), std::forward(args)...); + } else { + cpool->sort(cpool->begin(), cpool->end(), std::move(compare), std::move(algo), std::forward(args)...); + } } /** @@ -1066,7 +1073,7 @@ public: * Any subsequent change to `B` won't affect the order in `A`. * * @warning - * Pools of components that are owned by a group cannot be sorted.
+ * Pools of components owned by a group cannot be sorted this way.
* An assertion will abort the execution at runtime in debug mode in case * the pool is owned by a group. * diff --git a/src/entt/entity/storage.hpp b/src/entt/entity/storage.hpp index 261198689..585d7088a 100644 --- a/src/entt/entity/storage.hpp +++ b/src/entt/entity/storage.hpp @@ -451,7 +451,7 @@ public: static_assert(!std::is_empty_v); algo(copy.rbegin(), copy.rend(), [this, offset, compare = std::move(compare)](const auto lhs, const auto rhs) { - return compare(std::as_const(instances[lhs+offset]), std::as_const(instances[rhs+offset])); + return compare(std::as_const(instances[lhs + offset]), std::as_const(instances[rhs + offset])); }, std::forward(args)...); } else { algo(copy.rbegin(), copy.rend(), [offset, compare = std::move(compare), data = underlying_type::data()](const auto lhs, const auto rhs) { @@ -464,7 +464,7 @@ public: auto next = copy[curr]; while(curr != next) { - swap(copy[curr]+offset, copy[next]+offset); + swap(copy[curr] + offset, copy[next] + offset); copy[curr] = curr; curr = next; next = copy[curr]; diff --git a/test/entt/entity/registry.cpp b/test/entt/entity/registry.cpp index 11b3a9555..f6d3bb9c6 100644 --- a/test/entt/entity/registry.cpp +++ b/test/entt/entity/registry.cpp @@ -784,6 +784,75 @@ TEST(Registry, SortMulti) { } } +TEST(Registry, SortOwned) { + entt::registry registry; + registry.group(entt::get); + + for(auto i = 0; i < 5; ++i) { + const auto entity = registry.create(); + registry.assign(entity, i); + + if(i < 2) { + registry.assign(entity); + } + } + + ASSERT_TRUE((registry.has(*(registry.data()+0)))); + ASSERT_TRUE((registry.has(*(registry.data()+1)))); + + ASSERT_EQ(*(registry.raw()+0), 0); + ASSERT_EQ(*(registry.raw()+1), 1); + ASSERT_EQ(*(registry.raw()+2), 2); + ASSERT_EQ(*(registry.raw()+3), 3); + ASSERT_EQ(*(registry.raw()+4), 4); + + registry.sort([](const int lhs, const int rhs) { + return lhs < rhs; + }); + + ASSERT_EQ(*(registry.raw()+0), 0); + ASSERT_EQ(*(registry.raw()+1), 1); + ASSERT_EQ(*(registry.raw()+2), 4); + ASSERT_EQ(*(registry.raw()+3), 3); + ASSERT_EQ(*(registry.raw()+4), 2); + + registry.reset(); + registry.sort([](const int lhs, const int rhs) { + return lhs < rhs; + }); + + ASSERT_EQ(*(registry.raw()+0), 4); + ASSERT_EQ(*(registry.raw()+1), 3); + ASSERT_EQ(*(registry.raw()+2), 2); + ASSERT_EQ(*(registry.raw()+3), 1); + ASSERT_EQ(*(registry.raw()+4), 0); + + registry.each([®istry](const auto entity) { + registry.assign(entity); + }); + + registry.sort([](const int lhs, const int rhs) { + return lhs > rhs; + }); + + ASSERT_EQ(*(registry.raw()+0), 4); + ASSERT_EQ(*(registry.raw()+1), 3); + ASSERT_EQ(*(registry.raw()+2), 2); + ASSERT_EQ(*(registry.raw()+3), 1); + ASSERT_EQ(*(registry.raw()+4), 0); + + registry.reset(); + registry.sort([](const int lhs, const int rhs) { + return lhs > rhs; + }); + + ASSERT_EQ(*(registry.raw()+0), 0); + ASSERT_EQ(*(registry.raw()+1), 1); + ASSERT_EQ(*(registry.raw()+2), 2); + ASSERT_EQ(*(registry.raw()+3), 3); + ASSERT_EQ(*(registry.raw()+4), 4); +} + TEST(Registry, ComponentsWithTypesFromStandardTemplateLibrary) { // see #37 - the test shouldn't crash, that's all entt::registry registry;