Ticket #1860: 0003-Iterate-over-a-list-instead-of-a-map-in-the-Componen.patch

File 0003-Iterate-over-a-list-instead-of-a-map-in-the-Componen.patch, 5.5 KB (added by sbte, 10 years ago)
  • source/simulation2/system/ComponentManager.cpp

    From 8b8d699af74bfc0d448427278c8d5f91b678758e 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 list instead of a map in the ComponentManager where
     possible
    
    ---
     source/simulation2/system/ComponentManager.cpp | 30 ++++++++++++++++----------
     source/simulation2/system/ComponentManager.h   |  1 +
     2 files changed, 20 insertions(+), 11 deletions(-)
    
    diff --git a/source/simulation2/system/ComponentManager.cpp b/source/simulation2/system/ComponentManager.cpp
    index 2105d37..6e36706 100644
    a b void CComponentManager::Script_RegisterComponentType(void* cbdata, int iid, std:  
    283283    {
    284284        // For every script component with this cid, we need to switch its
    285285        // 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::list<IComponent*>& comps = componentManager->m_ComponentsByTypeIdVec[cid];
     287        std::list<IComponent*>::const_iterator eit = comps.begin();
    288288        for (; eit != comps.end(); ++eit)
    289289        {
    290             jsval instance = eit->second->GetJSInstance();
     290            jsval instance = (*eit)->GetJSInstance();
    291291            if (!JSVAL_IS_NULL(instance))
    292292                componentManager->m_ScriptInterface.SetPrototype(instance, proto.get());
    293293        }
    void CComponentManager::ResetState()  
    466466        ifcit->clear();
    467467
    468468    m_ComponentsByTypeId.clear();
     469    m_ComponentsByTypeIdVec.clear();
    469470
    470471    m_DestructionQueue.clear();
    471472
    IComponent* CComponentManager::ConstructComponent(entity_id_t ent, ComponentType  
    603604    }
    604605
    605606    std::map<entity_id_t, IComponent*>& emap2 = m_ComponentsByTypeId[cid];
     607    std::list<IComponent*>& evec2 = m_ComponentsByTypeIdVec[cid];
    606608
    607609    // If this is a scripted component, construct the appropriate JS object first
    608610    jsval obj = JSVAL_NULL;
    IComponent* CComponentManager::ConstructComponent(entity_id_t ent, ComponentType  
    625627
    626628    // Store a reference to the new component
    627629    emap1.insert(std::make_pair(ent, component));
     630    evec2.push_back(component);
    628631    emap2.insert(std::make_pair(ent, component));
    629632    // TODO: We need to more careful about this - if an entity is constructed by a component
    630633    // while we're iterating over all components, this will invalidate the iterators and everything
    void CComponentManager::FlushDestroyedComponents()  
    719722                std::map<entity_id_t, IComponent*>::iterator eit = iit->second.find(ent);
    720723                if (eit != iit->second.end())
    721724                {
     725                    // Remove from m_ComponentsByTypeIdVec:
     726                    std::map<ComponentTypeId, std::list<IComponent*> >::iterator iit2 = m_ComponentsByTypeIdVec.begin();
     727                    for (; iit2 != m_ComponentsByTypeIdVec.end(); ++iit2)
     728                            iit2->second.remove(eit->second);
     729
    722730                    eit->second->Deinit();
    723731                    m_ComponentTypesById[iit->first].dealloc(eit->second);
    724732                    iit->second.erase(ent);
    void CComponentManager::BroadcastMessage(const CMessage& msg) const  
    822830        for (; ctit != it->second.end(); ++ctit)
    823831        {
    824832            // 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())
     833            std::map<ComponentTypeId, std::list<IComponent*> >::const_iterator emap = m_ComponentsByTypeIdVec.find(*ctit);
     834            if (emap == m_ComponentsByTypeIdVec.end())
    827835                continue;
    828836
    829837            // Send the message to all of them
    830             std::map<entity_id_t, IComponent*>::const_iterator eit = emap->second.begin();
     838            std::list<IComponent*>::const_iterator eit = emap->second.begin();
    831839            for (; eit != emap->second.end(); ++eit)
    832                 eit->second->HandleMessage(msg, false);
     840                (*eit)->HandleMessage(msg, false);
    833841        }
    834842    }
    835843
    void CComponentManager::SendGlobalMessage(entity_id_t ent, const CMessage& msg)  
    859867            }
    860868
    861869            // 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())
     870            std::map<ComponentTypeId, std::list<IComponent*> >::const_iterator emap = m_ComponentsByTypeIdVec.find(*ctit);
     871            if (emap == m_ComponentsByTypeIdVec.end())
    864872                continue;
    865873
    866874            // Send the message to all of them
    867             std::map<entity_id_t, IComponent*>::const_iterator eit = emap->second.begin();
     875            std::list<IComponent*>::const_iterator eit = emap->second.begin();
    868876            for (; eit != emap->second.end(); ++eit)
    869                 eit->second->HandleMessage(msg, true);
     877                (*eit)->HandleMessage(msg, true);
    870878        }
    871879    }
    872880}
  • source/simulation2/system/ComponentManager.h

    diff --git a/source/simulation2/system/ComponentManager.h b/source/simulation2/system/ComponentManager.h
    index 17cc165..02d97ab 100644
    a b private:  
    262262    std::map<ComponentTypeId, ComponentType> m_ComponentTypesById;
    263263    std::vector<boost::unordered_map<entity_id_t, IComponent*> > m_ComponentsByInterface; // indexed by InterfaceId
    264264    std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> > m_ComponentsByTypeId;
     265    std::map<ComponentTypeId, std::list<IComponent*> > m_ComponentsByTypeIdVec;
    265266    std::map<MessageTypeId, std::vector<ComponentTypeId> > m_LocalMessageSubscriptions;
    266267    std::map<MessageTypeId, std::vector<ComponentTypeId> > m_GlobalMessageSubscriptions;
    267268    std::map<std::string, ComponentTypeId> m_ComponentTypeIdsByName;