Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECS: Fix remove entity function #41

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/ECS/SparseArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -101,6 +97,27 @@ class SparseArray {
}

private:
void removeDenses(std::size_t id, std::size_t sparseValue)
guillaumeAbel marked this conversation as resolved.
Show resolved Hide resolved
{
auto it = _dense.begin();
std::advance(it, sparseValue);
_dense.erase(it);
auto revIt = _revSparse.begin();
TTENSHII marked this conversation as resolved.
Show resolved Hide resolved
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)) {
Expand Down