From 3caea41b6e1fcd4af31214cb7e1bfc8958f51371 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Thu, 5 Sep 2013 17:16:11 -0400 Subject: Fix stupid iteration bug. Iteration would terminate at ID size() rather than capacity(), where size() is the number of allocated entities. Depending on where deleted entities were, this would likely miss entities at the end of the allocated vectors. Fixes #10. --- entityx/Entity.h | 5 +++-- entityx/Entity_test.cc | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/entityx/Entity.h b/entityx/Entity.h index abb3552..28c1b5d 100644 --- a/entityx/Entity.h +++ b/entityx/Entity.h @@ -302,6 +302,7 @@ class EntityManager : boost::noncopyable, public enable_shared_from_thiscreate_id(i_); for (auto &unpacker : unpackers_) { @@ -333,9 +334,9 @@ class EntityManager : boost::noncopyable, public enable_shared_from_thissize()); } + Iterator end() { return Iterator(manager_, predicates_, unpackers_, manager_->capacity()); } const Iterator begin() const { return Iterator(manager_, predicates_, unpackers_, 0); } - const Iterator end() const { return Iterator(manager_, predicates_, unpackers_, manager_->size()); } + const Iterator end() const { return Iterator(manager_, predicates_, unpackers_, manager_->capacity()); } template View &unpack_to(ptr &a) { diff --git a/entityx/Entity_test.cc b/entityx/Entity_test.cc index 701c15b..bdc217b 100644 --- a/entityx/Entity_test.cc +++ b/entityx/Entity_test.cc @@ -8,6 +8,7 @@ * Author: Alec Thomas */ +#include #include #include #include @@ -369,3 +370,25 @@ TEST_F(EntityManagerTest, TestEntityDestroyAll) { ASSERT_FALSE(a.valid()); ASSERT_FALSE(b.valid()); } + + +TEST_F(EntityManagerTest, TestEntityDestroyHole) { + std::vector entities; + + auto count = [this]() { + auto e = em->entities_with_components(); + return std::count_if(e.begin(), e.end(), [] (const Entity &) { return true; }); + }; + + for (int i = 0; i < 5000; i++) { + auto e = em->create(); + e.assign(); + entities.push_back(e); + } + + ASSERT_EQ(count(), 5000); + + entities[2500].destroy(); + + ASSERT_EQ(count(), 4999); +} -- cgit v1.2.3