Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 10, 2008, 12:27:30 AM (16 years ago)
Author:
landauf
Message:

std::set instead of std::list for Identifier-lists (parents, children, …) for faster accessing by key ( std::set::find(xyz) )

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core2/src/orxonox/core/Identifier.cc

    r871 r876  
    5151        this->factory_ = 0;
    5252
    53         this->children_ = new std::list<const Identifier*>();
    54         this->directChildren_ = new std::list<const Identifier*>();
     53        this->children_ = new std::set<const Identifier*>();
     54        this->directChildren_ = new std::set<const Identifier*>();
    5555
    5656        // Use a static variable because the classID gets created before main() and that's why we should avoid static member variables
     
    7272        @param parents A list containing all parents
    7373    */
    74     void Identifier::initialize(std::list<const Identifier*>* parents)
     74    void Identifier::initialize(std::set<const Identifier*>* parents)
    7575    {
    7676        COUT(4) << "*** Identifier: Initialize " << this->name_ << "-Singleton." << std::endl;
     
    8383
    8484            // Iterate through all parents
    85             for (std::list<const Identifier*>::iterator it = parents->begin(); it != parents->end(); ++it)
     85            for (std::set<const Identifier*>::iterator it = parents->begin(); it != parents->end(); ++it)
    8686            {
    8787                // Tell the parent we're one of it's children
     
    8989
    9090                // Erase all parents of our parent from our direct-parent-list
    91                 for (std::list<const Identifier*>::const_iterator it1 = (*it)->getParents().begin(); it1 != (*it)->getParents().end(); ++it1)
     91                for (std::set<const Identifier*>::const_iterator it1 = (*it)->getParents().begin(); it1 != (*it)->getParents().end(); ++it1)
    9292                {
    9393                    // Search for the parent's parent in our direct-parent-list
    94                     for (std::list<const Identifier*>::iterator it2 = this->directParents_.begin(); it2 != this->directParents_.end(); ++it2)
     94                    for (std::set<const Identifier*>::iterator it2 = this->directParents_.begin(); it2 != this->directParents_.end(); ++it2)
    9595                    {
    9696                        if ((*it1) == (*it2))
     
    105105
    106106            // Now iterate through all direct parents
    107             for (std::list<const Identifier*>::iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it)
     107            for (std::set<const Identifier*>::iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it)
    108108            {
    109109                // Tell the parent we're one of it's direct children
     
    149149    bool Identifier::isA(const Identifier* identifier) const
    150150    {
    151         return (identifier == this || this->identifierIsInList(identifier, this->parents_));
     151        return (identifier == this || (this->parents_.find(identifier) != this->children_->end()));
    152152    }
    153153
     
    167167    bool Identifier::isChildOf(const Identifier* identifier) const
    168168    {
    169         return this->identifierIsInList(identifier, this->parents_);
     169        return (this->parents_.find(identifier) != this->children_->end());
    170170    }
    171171
     
    176176    bool Identifier::isDirectChildOf(const Identifier* identifier) const
    177177    {
    178         return this->identifierIsInList(identifier, this->directParents_);
     178        return (this->directParents_.find(identifier) != this->children_->end());
    179179    }
    180180
     
    185185    bool Identifier::isParentOf(const Identifier* identifier) const
    186186    {
    187         return this->identifierIsInList(identifier, *this->children_);
     187        return (this->children_->find(identifier) != this->children_->end());
    188188    }
    189189
     
    194194    bool Identifier::isDirectParentOf(const Identifier* identifier) const
    195195    {
    196         return this->identifierIsInList(identifier, *this->directChildren_);
     196        return (this->directChildren_->find(identifier) != this->children_->end());
    197197    }
    198198
     
    221221    }
    222222
    223     /**
    224         @brief Searches for a given identifier in a list and returns whether the identifier is in the list or not.
    225         @param identifier The identifier to look for
    226         @param list The list
    227         @return True = the identifier is in the list
    228     */
    229     bool Identifier::identifierIsInList(const Identifier* identifier, const std::list<const Identifier*>& list)
    230     {
    231         for (std::list<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it)
    232             if (identifier == (*it))
    233                 return true;
    234 
    235         return false;
    236     }
    237 
    238     std::ostream& operator<<(std::ostream& out, const std::list<const Identifier*>& list)
    239     {
    240         for (std::list<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it)
     223    std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list)
     224    {
     225        for (std::set<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it)
    241226            out << (*it)->getName() << " ";
    242227
Note: See TracChangeset for help on using the changeset viewer.