Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 221 for code


Ignore:
Timestamp:
Nov 20, 2007, 2:57:59 AM (16 years ago)
Author:
landauf
Message:
  • made ObjectList double-linked to allow forward- and backward-iterating. its now a LI(F/L)O list.
  • added an iterator to iterate through object-lists. you can iterate forwards and backwards.

iterating forwards is easy: you get "0 1 2 … last"
iterating backwards is a bit tricky: you still get "0" first, but then "last … 2 1".
thats caused by the structure of the for-loop: you get the first element before the iterator knows if you'll increase or decrease it

Location:
code/branches/objecthierarchie/src
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • code/branches/objecthierarchie/src/BaseObject.h

    r220 r221  
    6464                { this->getIdentifier()->isDirectParentOf(object->getIdentifier()); }
    6565
     66            std::string name_; // test
     67
    6668    };
    6769}
  • code/branches/objecthierarchie/src/CMakeLists.txt

    r219 r221  
    33# create a few variables to simplify life
    44SET(SRC_FILES orxonox.cc IdentifierList.cc ObjectList.cc Identifier.cc Factory.cc OrxonoxClass.cc BaseObject.cc test1.cc test2.cc test3.cc)
    5 SET(INC_FILES IdentifierIncludes.h Identifier.h Factory.h IdentifierList.h ObjectList.h OrxonoxClass.h BaseObject.h Test.h test1.h test2.h test3.h)
     5SET(INC_FILES IdentifierIncludes.h Identifier.h Factory.h IdentifierList.h ObjectList.h Iterator.h OrxonoxClass.h BaseObject.h Test.h test1.h test2.h test3.h)
    66
    77#Creates an executable
  • code/branches/objecthierarchie/src/Identifier.cc

    r220 r221  
    8484    void Identifier::removeObject(OrxonoxClass* object)
    8585    {
    86         std::cout << "*** Removed object from " << this->name_ << "-list.\n";
    87         this->objects_.remove(object);
     86        bool bIterateForwards = !Identifier::isCreatingHierarchy();
     87
     88        if (bIterateForwards)
     89            std::cout << "*** Removed object from " << this->name_ << "-list, iterating forwards.\n";
     90        else
     91            std::cout << "*** Removed object from " << this->name_ << "-list, iterating backwards.\n";
     92
     93        this->objects_.remove(object, bIterateForwards);
    8894
    8995        IdentifierListElement* temp = this->directParents_.first_;
  • code/branches/objecthierarchie/src/Identifier.h

    r220 r221  
    3535        friend class BaseIdentifier;
    3636
     37        template <class T>
     38        friend class Iterator;
     39
    3740        public:
    3841            void addObject(OrxonoxClass* object);
     
    172175            Identifier::startCreatingHierarchy();
    173176            T* temp = new T();
     177            delete temp;
    174178            Identifier::stopCreatingHierarchy();
    175             delete temp;
    176179        }
    177180
     
    186189            BaseIdentifier();
    187190
    188             BaseIdentifier<B>& operator= (Identifier* identifier)
     191            BaseIdentifier<B>& operator=(Identifier* identifier)
    189192            {
    190193                if (!identifier->isA(ClassIdentifier<B>::getIdentifier()))
     
    199202            }
    200203
    201             Identifier* operator* ()
     204            Identifier* operator*()
    202205            {
    203206                return this->identifier_;
    204207            }
    205208
    206             Identifier* operator-> () const
     209            Identifier* operator->() const
    207210            {
    208211                return this->identifier_;
  • code/branches/objecthierarchie/src/IdentifierIncludes.h

    r220 r221  
    33#include "IdentifierList.h"
    44#include "ObjectList.h"
     5#include "Iterator.h"
    56#include "OrxonoxClass.h"
    67
  • code/branches/objecthierarchie/src/ObjectList.cc

    r197 r221  
    99    {
    1010        this->first_ = 0;
     11        this->last_ = 0;
    1112    }
    1213
     
    2425    void ObjectList::add(OrxonoxClass* object)
    2526    {
    26         ObjectListElement* temp = this->first_;
    27         this->first_ = new ObjectListElement(object);
    28         this->first_->next_ = temp;
     27        if (!this->last_)
     28        {
     29            this->last_ = new ObjectListElement(object);
     30            this->first_ = this->last_;
     31        }
     32        else
     33        {
     34            ObjectListElement* temp = this->last_;
     35            this->last_ = new ObjectListElement(object);
     36            this->last_->prev_ = temp;
     37            temp->next_ = this->last_;
     38        }
    2939    }
    3040
    31     void ObjectList::remove(OrxonoxClass* object)
     41    void ObjectList::remove(OrxonoxClass* object, bool bIterateForwards)
    3242    {
    33         if (!object)
     43        if (!object || !this->first_ || !this->last_)
    3444            return;
    3545
    36         if (this->first_->object_ == object)
     46        if (this->first_ == this->last_)
    3747        {
    38             ObjectListElement* temp = this->first_->next_;
    39             delete this->first_;
    40             this->first_ = temp;
     48            if (this->first_->object_ == object)
     49            {
     50                delete this->first_;
     51                this->first_ = 0;
     52                this->last_ = 0;
     53            }
    4154
    4255            return;
    4356        }
    4457
    45         ObjectListElement* temp = this->first_;
    46         while (temp->next_)
     58        if (bIterateForwards)
    4759        {
    48             if (temp->next_->object_ == object)
     60            if (this->first_->object_ == object)
    4961            {
    50                 ObjectListElement* temp2 = temp->next_->next_;
    51                 delete temp->next_;
    52                 temp->next_ = temp2;
     62                ObjectListElement* temp = this->first_->next_;
     63                delete this->first_;
     64                this->first_ = temp;
    5365
    5466                return;
    5567            }
    5668
    57             temp = temp->next_;
     69            ObjectListElement* temp = this->first_;
     70            while (temp->next_)
     71            {
     72                if (temp->next_->object_ == object)
     73                {
     74                    ObjectListElement* temp2 = temp->next_->next_;
     75                    delete temp->next_;
     76                    temp->next_ = temp2;
     77                    temp2->prev_ = temp;
     78
     79                    return;
     80                }
     81
     82                temp = temp->next_;
     83            }
     84        }
     85        else
     86        {
     87            if (this->last_->object_ == object)
     88            {
     89                ObjectListElement* temp = this->last_->prev_;
     90                delete this->last_;
     91                this->last_ = temp;
     92
     93                return;
     94            }
     95
     96            ObjectListElement* temp = this->last_;
     97            while (temp->prev_)
     98            {
     99                if (temp->prev_->object_ == object)
     100                {
     101                    ObjectListElement* temp2 = temp->prev_->prev_;
     102                    delete temp->prev_;
     103                    temp->prev_ = temp2;
     104                    temp2->next_ = temp;
     105
     106                    return;
     107                }
     108
     109                temp = temp->prev_;
     110            }
    58111        }
    59112    }
     
    67120        this->object_ = object;
    68121        this->next_ = 0;
     122        this->prev_ = 0;
    69123    }
    70124
  • code/branches/objecthierarchie/src/ObjectList.h

    r197 r221  
    1414            OrxonoxClass* object_;
    1515            ObjectListElement* next_;
     16            ObjectListElement* prev_;
    1617    };
    1718
     
    2223            ~ObjectList();
    2324            void add(OrxonoxClass* object);
    24             void remove(OrxonoxClass* object);
     25            void remove(OrxonoxClass* object, bool bIterateForwards = true);
    2526
    2627            ObjectListElement* first_;
     28            ObjectListElement* last_;
    2729    };
    2830}
  • code/branches/objecthierarchie/src/orxonox.cc

    r219 r221  
    500500        delete test8_03;
    501501*/
    502 
     502/*
    503503        std::cout << "Test 9\n";
    504504        std::cout << "1\n";
     
    526526        delete test9_05;
    527527        delete test9_06;
     528*/
     529        std::cout << "Test 10\n";
     530        Identifier* test9_01 = Class(A1B2);
     531        Identifier* test9_02 = Class(A2);
     532        Identifier* test9_03 = Class(A3B1C1);
     533
     534
     535        BaseObject* test9_04 = test9_01->fabricate();
     536        BaseObject* test9_05 = test9_02->fabricate();
     537        BaseObject* test9_06 = test9_03->fabricate();
     538
     539        BaseObject* test9_07;
     540        for (int i = 0; i < 10; i++)
     541            test9_07 = Factory("A1B1C1");
     542
     543        std::cout << "1\n";
     544        int count = 0;
     545        for (Iterator<BaseObject> it; it != 0; it++)
     546            count++;
     547        std::cout << "Anzahl BaseObjects: " << count << "\n";
     548
     549        count = 0;
     550        for (Iterator<A1> it; it != 0; it++)
     551            count++;
     552        std::cout << "Anzahl A1: " << count << "\n";
     553
     554        count = 0;
     555        for (Iterator<A1B1> it; it; it++)
     556            count++;
     557        std::cout << "Anzahl A1B1: " << count << "\n";
     558
     559        count = 0;
     560        for (Iterator<A1B1C1> it; it; it++)
     561            count++;
     562        std::cout << "Anzahl A1B1C1: " << count << "\n";
     563
     564        count = 0;
     565        for (Iterator<A2> it; it; it++)
     566            count++;
     567        std::cout << "Anzahl A2: " << count << "\n";
     568
     569
     570        std::cout << "2\n";
     571        BaseObject* test9_08;
     572        for (int i = 0; i < 10; i++)
     573        {
     574            test9_08 = Factory("A2B1C1");
     575            test9_08->name_ = "A2B1C1#";
     576            test9_08->name_ += ('0' + i);
     577        }
     578
     579        for (Iterator<A2B1C1> it; it; it++)
     580            std::cout << "Name: " << it->name_ << "\n";
     581
     582        std::cout << "3\n";
     583        for (Iterator<A2B1C1> it; it; it--)
     584            std::cout << "Name: " << it->name_ << "\n";
     585
     586        std::cout << "4\n";
    528587
    529588      }
Note: See TracChangeset for help on using the changeset viewer.