diff --git a/src/entt/entity/sparse_set.hpp b/src/entt/entity/sparse_set.hpp index dad342e01..125ba755d 100644 --- a/src/entt/entity/sparse_set.hpp +++ b/src/entt/entity/sparse_set.hpp @@ -978,14 +978,14 @@ public: const auto to = other.end(); auto from = other.begin(); - for(size_type pos = packed.size() - 1; pos && from != to; ++from) { - if(contains(*from)) { - if(*from != packed[pos]) { + for(auto it = begin(); it.index() && from != to; ++from) { + if(const auto curr = *from; contains(curr)) { + if(const auto entt = *it; entt != curr) { // basic no-leak guarantee (with invalid state) if swapping throws - swap_elements(packed[pos], *from); + swap_elements(entt, curr); } - --pos; + ++it; } } }