diff options
author | Alec Thomas <alec@swapoff.org> | 2014-10-23 14:33:17 +1100 |
---|---|---|
committer | Alec Thomas <alec@swapoff.org> | 2014-10-23 14:33:17 +1100 |
commit | 0dcfeffc8e3373321e71be943164e005933914f2 (patch) | |
tree | 57865bab31ff916c0a1fd04d5a034786ccea1440 | |
parent | 04fdec9b2284517233471691bc887a51c77149b7 (diff) |
Sort free list before iterating over all entities.
This should make iteration over all entities much faster.
-rw-r--r-- | entityx/Entity.h | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/entityx/Entity.h b/entityx/Entity.h index b749c4c..c3fdbee 100644 --- a/entityx/Entity.h +++ b/entityx/Entity.h @@ -340,9 +340,19 @@ class EntityManager : entityx::help::NonCopyable { protected: ViewIterator(EntityManager *manager, uint32_t index) - : manager_(manager), i_(index), capacity_(manager_->capacity()) {} + : manager_(manager), i_(index), capacity_(manager_->capacity()) { + if (Debug) { + manager_->free_list_.sort(); + free_cursor_ = manager_->free_list_.begin(); + } + } ViewIterator(EntityManager *manager, const ComponentMask mask, uint32_t index) - : manager_(manager), mask_(mask), i_(index), capacity_(manager_->capacity()) {} + : manager_(manager), mask_(mask), i_(index), capacity_(manager_->capacity()) { + if (Debug) { + manager_->free_list_.sort(); + free_cursor_ = manager_->free_list_.begin(); + } + } void next() { while (i_ < capacity_ && !predicate()) { @@ -355,13 +365,14 @@ class EntityManager : entityx::help::NonCopyable { } } - inline bool predicate() const { + inline bool predicate() { return (Debug && valid_entity()) || (manager_->entity_component_mask_[i_] & mask_) == mask_; } - inline bool valid_entity() const { - for (uint32_t i : manager_->free_list_) { - if (i_ == i) return false; + inline bool valid_entity() { + if (free_cursor_ != manager_->free_list_.end() && *free_cursor_ == i_) { + ++free_cursor_; + return false; } return true; } @@ -370,6 +381,7 @@ class EntityManager : entityx::help::NonCopyable { ComponentMask mask_; uint32_t i_; size_t capacity_; + std::list<uint32_t>::iterator free_cursor_; }; template <bool Debug> |