From 5a02aeaa89e05621af2e101c6065a0c884c8e707 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Tue, 21 May 2019 22:32:56 +0200 Subject: [PATCH] raw() is no longer in use --- TODO | 2 -- src/entt/entity/group.hpp | 28 ++-------------------------- src/entt/entity/registry.hpp | 24 +++++------------------- src/entt/entity/sparse_set.hpp | 2 +- src/entt/entity/storage.hpp | 19 +++++++++++++++++++ 5 files changed, 27 insertions(+), 48 deletions(-) diff --git a/TODO b/TODO index c4811805f..0e5b6545e 100644 --- a/TODO +++ b/TODO @@ -16,8 +16,6 @@ * add take functionality, eg registry.take(entity, other); where it takes the entity and all its components from registry and move them to other * add opaque input iterators to views and groups that return tuples (proxy), multi-pass guaranteed * add fast lane for raw iterations, extend mt doc to describe allowed add/remove with pre-allocations on fast lanes -* review sparse set to allow customization (mix pack in the spec, base is position only) - - non-owning groups can iterate pages and skip empty ones, this should mitigate the lack of the packed array * review 64 bit id: user defined area + dedicated member on the registry to set it * early out in views using bitmasks with bloom filter like access based on modulus - standard each, use bitmask to speed up the whole thing and avoid accessing the pools to test for the page diff --git a/src/entt/entity/group.hpp b/src/entt/entity/group.hpp index e443d6025..a6899d93b 100644 --- a/src/entt/entity/group.hpp +++ b/src/entt/entity/group.hpp @@ -457,30 +457,6 @@ class basic_group, Owned...> { pools{owned..., get...} {} - template - decltype(auto) from_index(const typename sparse_set::size_type index) { - static_assert(!std::is_empty_v); - - if constexpr(std::disjunction_v...>) { - return std::as_const(*std::get *>(pools)).raw()[index]; - } else { - return std::as_const(*std::get *>(pools)).get(data()[index]); - } - } - - template - inline auto swap(int, const std::size_t lhs, const std::size_t rhs) - -> decltype(std::declval>().raw(), void()) { - auto *cpool = std::get *>(pools); - std::swap(cpool->raw()[lhs], cpool->raw()[rhs]); - cpool->swap(lhs, rhs); - } - - template - inline void swap(char, const std::size_t lhs, const std::size_t rhs) { - std::get *>(pools)->swap(lhs, rhs); - } - template inline void traverse(Func func, type_list, type_list) const { auto raw = std::make_tuple((std::get *>(pools)->end() - *length)...); @@ -812,7 +788,7 @@ public: } else { algo(copy.rbegin(), copy.rend(), [compare = std::move(compare), this](const auto lhs, const auto rhs) { // useless this-> used to suppress a warning with clang - return compare(this->from_index(lhs)..., this->from_index(rhs)...); + return compare(this->get(lhs)..., this->get(rhs)...); }, std::forward(args)...); } @@ -823,7 +799,7 @@ public: while(curr != next) { const auto lhs = copy[curr]; const auto rhs = copy[next]; - (swap(0, lhs, rhs), ...); + (std::get *>(pools)->swap(lhs, rhs), ...); copy[curr] = curr; curr = next; next = copy[curr]; diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index aac698c5b..673239394 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -177,7 +177,7 @@ class basic_registry { && !(reg.pool()->has(entt) || ...)) { const auto pos = this->owned++; - (reg.swap(0, std::get *>(cpools), entt, pos), ...); + (std::get *>(cpools)->swap(std::get *>(cpools)->sparse_set::get(entt), pos), ...); } } else if constexpr(std::disjunction_v...>) { if((std::get *>(cpools)->has(entt) && ...) @@ -185,7 +185,7 @@ class basic_registry { && ((std::is_same_v || !reg.pool()->has(entt)) && ...)) { const auto pos = this->owned++; - (reg.swap(0, std::get *>(cpools), entt, pos), ...); + (std::get *>(cpools)->swap(std::get *>(cpools)->sparse_set::get(entt), pos), ...); } } } @@ -196,7 +196,7 @@ class basic_registry { if(std::get<0>(cpools)->has(entt) && std::get<0>(cpools)->sparse_set::get(entt) < this->owned) { const auto pos = --this->owned; - (reg.swap(0, std::get *>(cpools), entt, pos), ...); + (std::get *>(cpools)->swap(std::get *>(cpools)->sparse_set::get(entt), pos), ...); } } }; @@ -238,18 +238,6 @@ class basic_registry { ++available; } - template - inline auto swap(int, pool_type *cpool, const Entity entt, const std::size_t pos) - -> decltype(std::swap(cpool->get(entt), cpool->raw()[pos]), void()) { - std::swap(cpool->get(entt), cpool->raw()[pos]); - cpool->swap(cpool->sparse_set::get(entt), pos); - } - - template - inline void swap(char, pool_type *cpool, const Entity entt, const std::size_t pos) { - cpool->swap(cpool->sparse_set::get(entt), pos); - } - template inline const auto * pool() const ENTT_NOEXCEPT { const auto ctype = type(); @@ -1382,19 +1370,17 @@ public: }); // 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(), [curr, &cpools, this](const auto entity) { + std::for_each(cpool->data(), cpool->data() + cpool->size(), [curr, &cpools](const auto entity) { if((std::get *>(cpools)->has(entity) && ...) && (std::get *>(cpools)->has(entity) && ...) && !(std::get *>(cpools)->has(entity) || ...)) { if constexpr(sizeof...(Owned) == 0) { curr->construct(entity); - // suppress warnings - (void)this; } else { const auto pos = curr->owned++; // useless this-> used to suppress a warning with clang - (this->swap(0, std::get *>(cpools), entity, pos), ...); + (std::get *>(cpools)->swap(std::get *>(cpools)->sparse_set::get(entity), pos), ...); } } }); diff --git a/src/entt/entity/sparse_set.hpp b/src/entt/entity/sparse_set.hpp index 4280a180b..91fb755a0 100644 --- a/src/entt/entity/sparse_set.hpp +++ b/src/entt/entity/sparse_set.hpp @@ -454,7 +454,7 @@ public: } /** - * @brief Swaps the position of two entities in the internal packed array. + * @brief Swaps two entities in the internal packed array. * * For what it's worth, this function affects both the internal sparse array * and the internal packed array. Users should not care of that anyway. diff --git a/src/entt/entity/storage.hpp b/src/entt/entity/storage.hpp index 5ea0a1e18..911a7313b 100644 --- a/src/entt/entity/storage.hpp +++ b/src/entt/entity/storage.hpp @@ -375,6 +375,25 @@ public: underlying_type::destroy(entt); } + /** + * @brief Swaps entities and objects in the internal packed arrays. + * + * @warning + * Attempting to swap entities that don't belong to the sparse set results + * in undefined behavior.
+ * An assertion will abort the execution at runtime in debug mode if the + * sparse set doesn't contain the given entities. + * + * @param lhs A valid position within the sparse set. + * @param rhs A valid position within the sparse set. + */ + void swap(const size_type lhs, const size_type rhs) ENTT_NOEXCEPT { + ENTT_ASSERT(lhs < instances.size()); + ENTT_ASSERT(rhs < instances.size()); + std::swap(instances[lhs], instances[rhs]); + underlying_type::swap(lhs, rhs); + } + /** * @brief Sort instances according to the given comparison function. *