From 7382c090c2c8e4784313d9e4b5b50ea756ddcbcb Mon Sep 17 00:00:00 2001 From: guillaume abel Date: Tue, 3 Oct 2023 16:11:49 +0200 Subject: [PATCH 1/2] ECS: Fix remove entity function PATCH --- src/ECS/Registry.hpp | 2 +- src/ECS/SparseArray.hpp | 30 +++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/ECS/Registry.hpp b/src/ECS/Registry.hpp index 22d54e03..53a8830b 100644 --- a/src/ECS/Registry.hpp +++ b/src/ECS/Registry.hpp @@ -26,7 +26,7 @@ enum BackLayers { BACK = 0, BACKMAX }; /* * FRONT is the frontest layer, so when adding a new one increment the FRONT * value and add the new one above - */ +*/ enum FrontLayers { FRONT = 0, FRONTMAX }; diff --git a/src/ECS/SparseArray.hpp b/src/ECS/SparseArray.hpp index 5c468755..48dc211e 100644 --- a/src/ECS/SparseArray.hpp +++ b/src/ECS/SparseArray.hpp @@ -47,13 +47,9 @@ class SparseArray { throw std::runtime_error( "SparseArrays::erase: ID out of bounds!"); } - if (_sparse[id] != -1) { - auto it = _dense.begin(); - std::advance(it, _sparse[id]); - _dense.erase(it); - auto revIt = _revSparse.begin(); - std::advance(revIt, _sparse[id]); - _revSparse.erase(revIt); + std::size_t sparseValue = _sparse[id]; + if (sparseValue != -1) { + removeDenses(id, sparseValue); } auto it = _sparse.begin(); std::advance(it, id); @@ -101,6 +97,26 @@ class SparseArray { } private: + void removeDenses(std::size_t id, std::size_t sparseValue) + { + auto it = _dense.begin(); + std::advance(it, sparseValue); + _dense.erase(it); + auto revIt = _revSparse.begin(); + std::advance(revIt, sparseValue); + _revSparse.erase(revIt); + for (auto revIt2 = _revSparse.begin(); revIt2 != _revSparse.end(); revIt2++) { + if (*revIt2 > id) { + (*revIt2)--; + } + } + for (auto it2 = _sparse.begin(); it2 != _sparse.end(); it2++) { + if (*it2 > sparseValue) { + (*it2)--; + } + } + } + void throwIfDontExist(std::size_t id) { if (!exist(id)) { From 63b4aa71b0985c5141f51ca9ccb06750772ed009 Mon Sep 17 00:00:00 2001 From: Github Actions Date: Tue, 3 Oct 2023 14:12:47 +0000 Subject: [PATCH 2/2] FORMAT-AUTO: automatic format on pull request #41 --- src/ECS/Registry.hpp | 2 +- src/ECS/SparseArray.hpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ECS/Registry.hpp b/src/ECS/Registry.hpp index 53a8830b..22d54e03 100644 --- a/src/ECS/Registry.hpp +++ b/src/ECS/Registry.hpp @@ -26,7 +26,7 @@ enum BackLayers { BACK = 0, BACKMAX }; /* * FRONT is the frontest layer, so when adding a new one increment the FRONT * value and add the new one above -*/ + */ enum FrontLayers { FRONT = 0, FRONTMAX }; diff --git a/src/ECS/SparseArray.hpp b/src/ECS/SparseArray.hpp index 48dc211e..1a2fb90d 100644 --- a/src/ECS/SparseArray.hpp +++ b/src/ECS/SparseArray.hpp @@ -105,7 +105,8 @@ class SparseArray { auto revIt = _revSparse.begin(); std::advance(revIt, sparseValue); _revSparse.erase(revIt); - for (auto revIt2 = _revSparse.begin(); revIt2 != _revSparse.end(); revIt2++) { + for (auto revIt2 = _revSparse.begin(); revIt2 != _revSparse.end(); + revIt2++) { if (*revIt2 > id) { (*revIt2)--; }