Ticket #1860: 0001-Iterate-over-a-vector-instead-of-a-map-in-the-Compon.patch

File 0001-Iterate-over-a-vector-instead-of-a-map-in-the-Compon.patch, 5.6 KB (added by sbte, 11 years ago)
  • source/simulation2/system/ComponentManager.cpp

    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:  
    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::vector<IComponent*>& comps = componentManager->m_ComponentsByTypeIdVec[cid];
     287        std::vector<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::vector<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::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
    722729                    eit->second->Deinit();
    723730                    m_ComponentTypesById[iit->first].dealloc(eit->second);
    724731                    iit->second.erase(ent);
    void CComponentManager::BroadcastMessage(const CMessage& msg) const  
    822829        for (; ctit != it->second.end(); ++ctit)
    823830        {
    824831            // 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())
    827834                continue;
    828835
    829836            // 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();
    831838            for (; eit != emap->second.end(); ++eit)
    832                 eit->second->HandleMessage(msg, false);
     839                (*eit)->HandleMessage(msg, false);
    833840        }
    834841    }
    835842
    void CComponentManager::SendGlobalMessage(entity_id_t ent, const CMessage& msg)  
    859866            }
    860867
    861868            // 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())
    864871                continue;
    865872
    866873            // 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();
    868875            for (; eit != emap->second.end(); ++eit)
    869                 eit->second->HandleMessage(msg, true);
     876                (*eit)->HandleMessage(msg, true);
    870877        }
    871878    }
    872879}
  • source/simulation2/system/ComponentManager.h

    diff --git a/source/simulation2/system/ComponentManager.h b/source/simulation2/system/ComponentManager.h
    index 17cc165..9540ac7 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::vector<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;