Skip to content

Commit ed4c675

Browse files
committed
sparse_set/storage: drop move_element
1 parent f157898 commit ed4c675

File tree

3 files changed

+58
-15
lines changed

3 files changed

+58
-15
lines changed

src/entt/entity/sparse_set.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ class basic_sparse_set {
226226
}
227227

228228
virtual void swap_at(const std::size_t, const std::size_t) {}
229-
virtual void move_element(const std::size_t, const std::size_t) {}
230229

231230
protected:
232231
/*! @brief Random access iterator type. */
@@ -785,7 +784,7 @@ class basic_sparse_set {
785784
for(auto *it = &free_list; *it != null && from; it = std::addressof(packed[traits_type::to_entity(*it)])) {
786785
if(const size_type to = traits_type::to_entity(*it); to < from) {
787786
--from;
788-
move_element(from, to);
787+
swap_at(from, to);
789788

790789
using std::swap;
791790
swap(packed[from], packed[to]);
@@ -825,6 +824,7 @@ class basic_sparse_set {
825824

826825
// basic no-leak guarantee (with invalid state) if swapping throws
827826
swap_at(static_cast<size_type>(from), static_cast<size_type>(to));
827+
828828
entt = traits_type::combine(to, traits_type::to_integral(packed[from]));
829829
other = traits_type::combine(from, traits_type::to_integral(packed[to]));
830830

src/entt/entity/storage.hpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -305,24 +305,23 @@ class basic_storage: public basic_sparse_set<Entity, typename std::allocator_tra
305305
return std::addressof(element_at(pos));
306306
}
307307

308-
void swap_at([[maybe_unused]] const std::size_t lhs, [[maybe_unused]] const std::size_t rhs) final {
309-
// use a runtime value to avoid compile-time suppression that drives the code coverage tool crazy
310-
ENTT_ASSERT((lhs + 1u) && !is_pinned_type_v, "Pinned type");
311-
312-
if constexpr(!is_pinned_type_v) {
313-
using std::swap;
314-
swap(element_at(lhs), element_at(rhs));
315-
}
316-
}
317-
318-
void move_element([[maybe_unused]] const std::size_t from, [[maybe_unused]] const std::size_t to) final {
308+
void swap_at([[maybe_unused]] const std::size_t from, [[maybe_unused]] const std::size_t to) final {
319309
// use a runtime value to avoid compile-time suppression that drives the code coverage tool crazy
320310
ENTT_ASSERT((from + 1u) && !is_pinned_type_v, "Pinned type");
321311

322312
if constexpr(!is_pinned_type_v) {
323313
auto &elem = element_at(from);
324-
entt::uninitialized_construct_using_allocator(to_address(assure_at_least(to)), get_allocator(), std::move(elem));
325-
std::destroy_at(std::addressof(elem));
314+
315+
if constexpr(traits_type::in_place_delete) {
316+
if(base_type::operator[](to) == tombstone) {
317+
entt::uninitialized_construct_using_allocator(to_address(assure_at_least(to)), get_allocator(), std::move(elem));
318+
std::destroy_at(std::addressof(elem));
319+
return;
320+
}
321+
}
322+
323+
using std::swap;
324+
swap(elem, element_at(to));
326325
}
327326
}
328327

test/entt/entity/storage.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,50 @@ TEST(Storage, Raw) {
13641364
ASSERT_EQ(pool.raw()[0u][2u], 9);
13651365
}
13661366

1367+
TEST(Storage, SwapElements) {
1368+
entt::storage<int> pool;
1369+
1370+
pool.emplace(entt::entity{3}, 3);
1371+
pool.emplace(entt::entity{12}, 6);
1372+
pool.emplace(entt::entity{42}, 9);
1373+
1374+
pool.erase(entt::entity{12});
1375+
1376+
ASSERT_EQ(pool.get(entt::entity{3}), 3);
1377+
ASSERT_EQ(pool.get(entt::entity{42}), 9);
1378+
ASSERT_EQ(pool.index(entt::entity{3}), 0u);
1379+
ASSERT_EQ(pool.index(entt::entity{42}), 1u);
1380+
1381+
pool.swap_elements(entt::entity{3}, entt::entity{42});
1382+
1383+
ASSERT_EQ(pool.get(entt::entity{3}), 3);
1384+
ASSERT_EQ(pool.get(entt::entity{42}), 9);
1385+
ASSERT_EQ(pool.index(entt::entity{3}), 1u);
1386+
ASSERT_EQ(pool.index(entt::entity{42}), 0u);
1387+
}
1388+
1389+
TEST(Storage, StableSwapElements) {
1390+
entt::storage<stable_type> pool;
1391+
1392+
pool.emplace(entt::entity{3}, 3);
1393+
pool.emplace(entt::entity{12}, 6);
1394+
pool.emplace(entt::entity{42}, 9);
1395+
1396+
pool.erase(entt::entity{12});
1397+
1398+
ASSERT_EQ(pool.get(entt::entity{3}).value, 3);
1399+
ASSERT_EQ(pool.get(entt::entity{42}).value, 9);
1400+
ASSERT_EQ(pool.index(entt::entity{3}), 0u);
1401+
ASSERT_EQ(pool.index(entt::entity{42}), 2u);
1402+
1403+
pool.swap_elements(entt::entity{3}, entt::entity{42});
1404+
1405+
ASSERT_EQ(pool.get(entt::entity{3}).value, 3);
1406+
ASSERT_EQ(pool.get(entt::entity{42}).value, 9);
1407+
ASSERT_EQ(pool.index(entt::entity{3}), 2u);
1408+
ASSERT_EQ(pool.index(entt::entity{42}), 0u);
1409+
}
1410+
13671411
TEST(Storage, SortOrdered) {
13681412
entt::storage<boxed_int> pool;
13691413
entt::entity entities[5u]{entt::entity{12}, entt::entity{42}, entt::entity{7}, entt::entity{3}, entt::entity{9}};

0 commit comments

Comments
 (0)