From 697413d5cd05e6cd3bb58f332e35a3d2bcb8549a Mon Sep 17 00:00:00 2001
From: "Sven (Sbte)" <svenb.linux@gmail.com>
Date: Sun, 10 Mar 2013 17:41:26 +0100
Subject: Iterate over a vector instead of a map in the ComponentManager where
possible
---
source/simulation2/system/ComponentManager.cpp | 29 ++++++++++++++++----------
source/simulation2/system/ComponentManager.h | 1 +
2 files changed, 19 insertions(+), 11 deletions(-)
diff --git a/source/simulation2/system/ComponentManager.cpp b/source/simulation2/system/ComponentManager.cpp
index 2105d37..7bbd11b 100644
a
|
b
|
void CComponentManager::Script_RegisterComponentType(void* cbdata, int iid, std:
|
283 | 283 | { |
284 | 284 | // For every script component with this cid, we need to switch its |
285 | 285 | // prototype from the old constructor's prototype property to the new one's |
286 | | const std::map<entity_id_t, IComponent*>& comps = componentManager->m_ComponentsByTypeId[cid]; |
287 | | std::map<entity_id_t, IComponent*>::const_iterator eit = comps.begin(); |
| 286 | const std::vector<IComponent*>& comps = componentManager->m_ComponentsByTypeIdVec[cid]; |
| 287 | std::vector<IComponent*>::const_iterator eit = comps.begin(); |
288 | 288 | for (; eit != comps.end(); ++eit) |
289 | 289 | { |
290 | | jsval instance = eit->second->GetJSInstance(); |
| 290 | jsval instance = (*eit)->GetJSInstance(); |
291 | 291 | if (!JSVAL_IS_NULL(instance)) |
292 | 292 | componentManager->m_ScriptInterface.SetPrototype(instance, proto.get()); |
293 | 293 | } |
… |
… |
void CComponentManager::ResetState()
|
466 | 466 | ifcit->clear(); |
467 | 467 | |
468 | 468 | m_ComponentsByTypeId.clear(); |
| 469 | m_ComponentsByTypeIdVec.clear(); |
469 | 470 | |
470 | 471 | m_DestructionQueue.clear(); |
471 | 472 | |
… |
… |
IComponent* CComponentManager::ConstructComponent(entity_id_t ent, ComponentType
|
603 | 604 | } |
604 | 605 | |
605 | 606 | std::map<entity_id_t, IComponent*>& emap2 = m_ComponentsByTypeId[cid]; |
| 607 | std::vector<IComponent*>& evec2 = m_ComponentsByTypeIdVec[cid]; |
606 | 608 | |
607 | 609 | // If this is a scripted component, construct the appropriate JS object first |
608 | 610 | jsval obj = JSVAL_NULL; |
… |
… |
IComponent* CComponentManager::ConstructComponent(entity_id_t ent, ComponentType
|
625 | 627 | |
626 | 628 | // Store a reference to the new component |
627 | 629 | emap1.insert(std::make_pair(ent, component)); |
| 630 | evec2.push_back(component); |
628 | 631 | emap2.insert(std::make_pair(ent, component)); |
629 | 632 | // TODO: We need to more careful about this - if an entity is constructed by a component |
630 | 633 | // while we're iterating over all components, this will invalidate the iterators and everything |
… |
… |
void CComponentManager::FlushDestroyedComponents()
|
719 | 722 | std::map<entity_id_t, IComponent*>::iterator eit = iit->second.find(ent); |
720 | 723 | if (eit != iit->second.end()) |
721 | 724 | { |
| 725 | // Remove from m_ComponentsByTypeIdVec: |
| 726 | std::vector<IComponent*>::iterator eit2 = std::find(m_ComponentsByTypeIdVec[iit->first].begin(), m_ComponentsByTypeIdVec[iit->first].end(), eit->second); |
| 727 | m_ComponentsByTypeIdVec[iit->first].erase(eit2); |
| 728 | |
722 | 729 | eit->second->Deinit(); |
723 | 730 | m_ComponentTypesById[iit->first].dealloc(eit->second); |
724 | 731 | iit->second.erase(ent); |
… |
… |
void CComponentManager::BroadcastMessage(const CMessage& msg) const
|
822 | 829 | for (; ctit != it->second.end(); ++ctit) |
823 | 830 | { |
824 | 831 | // Find the component instances of this type (if any) |
825 | | std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> >::const_iterator emap = m_ComponentsByTypeId.find(*ctit); |
826 | | if (emap == m_ComponentsByTypeId.end()) |
| 832 | std::map<ComponentTypeId, std::vector<IComponent*> >::const_iterator emap = m_ComponentsByTypeIdVec.find(*ctit); |
| 833 | if (emap == m_ComponentsByTypeIdVec.end()) |
827 | 834 | continue; |
828 | 835 | |
829 | 836 | // Send the message to all of them |
830 | | std::map<entity_id_t, IComponent*>::const_iterator eit = emap->second.begin(); |
| 837 | std::vector<IComponent*>::const_iterator eit = emap->second.begin(); |
831 | 838 | for (; eit != emap->second.end(); ++eit) |
832 | | eit->second->HandleMessage(msg, false); |
| 839 | (*eit)->HandleMessage(msg, false); |
833 | 840 | } |
834 | 841 | } |
835 | 842 | |
… |
… |
void CComponentManager::SendGlobalMessage(entity_id_t ent, const CMessage& msg)
|
859 | 866 | } |
860 | 867 | |
861 | 868 | // Find the component instances of this type (if any) |
862 | | std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> >::const_iterator emap = m_ComponentsByTypeId.find(*ctit); |
863 | | if (emap == m_ComponentsByTypeId.end()) |
| 869 | std::map<ComponentTypeId, std::vector<IComponent*> >::const_iterator emap = m_ComponentsByTypeIdVec.find(*ctit); |
| 870 | if (emap == m_ComponentsByTypeIdVec.end()) |
864 | 871 | continue; |
865 | 872 | |
866 | 873 | // Send the message to all of them |
867 | | std::map<entity_id_t, IComponent*>::const_iterator eit = emap->second.begin(); |
| 874 | std::vector<IComponent*>::const_iterator eit = emap->second.begin(); |
868 | 875 | for (; eit != emap->second.end(); ++eit) |
869 | | eit->second->HandleMessage(msg, true); |
| 876 | (*eit)->HandleMessage(msg, true); |
870 | 877 | } |
871 | 878 | } |
872 | 879 | } |
diff --git a/source/simulation2/system/ComponentManager.h b/source/simulation2/system/ComponentManager.h
index 17cc165..9540ac7 100644
a
|
b
|
private:
|
262 | 262 | std::map<ComponentTypeId, ComponentType> m_ComponentTypesById; |
263 | 263 | std::vector<boost::unordered_map<entity_id_t, IComponent*> > m_ComponentsByInterface; // indexed by InterfaceId |
264 | 264 | std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> > m_ComponentsByTypeId; |
| 265 | std::map<ComponentTypeId, std::vector<IComponent*> > m_ComponentsByTypeIdVec; |
265 | 266 | std::map<MessageTypeId, std::vector<ComponentTypeId> > m_LocalMessageSubscriptions; |
266 | 267 | std::map<MessageTypeId, std::vector<ComponentTypeId> > m_GlobalMessageSubscriptions; |
267 | 268 | std::map<std::string, ComponentTypeId> m_ComponentTypeIdsByName; |