Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchie/src/ObjectList.cc @ 221

Last change on this file since 221 was 221, checked in by landauf, 16 years ago
  • 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

File size: 3.2 KB
Line 
1#include "ObjectList.h"
2
3namespace orxonox
4{
5    // ###############################
6    // ###       ObjectList        ###
7    // ###############################
8    ObjectList::ObjectList()
9    {
10        this->first_ = 0;
11        this->last_ = 0;
12    }
13
14    ObjectList::~ObjectList()
15    {
16        ObjectListElement* temp;
17        while (this->first_)
18        {
19            temp = this->first_->next_;
20            delete this->first_;
21            this->first_ = temp;
22        }
23    }
24
25    void ObjectList::add(OrxonoxClass* object)
26    {
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        }
39    }
40
41    void ObjectList::remove(OrxonoxClass* object, bool bIterateForwards)
42    {
43        if (!object || !this->first_ || !this->last_)
44            return;
45
46        if (this->first_ == this->last_)
47        {
48            if (this->first_->object_ == object)
49            {
50                delete this->first_;
51                this->first_ = 0;
52                this->last_ = 0;
53            }
54
55            return;
56        }
57
58        if (bIterateForwards)
59        {
60            if (this->first_->object_ == object)
61            {
62                ObjectListElement* temp = this->first_->next_;
63                delete this->first_;
64                this->first_ = temp;
65
66                return;
67            }
68
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            }
111        }
112    }
113
114
115    // ###############################
116    // ###    ObjectListElement    ###
117    // ###############################
118    ObjectListElement::ObjectListElement(OrxonoxClass* object)
119    {
120        this->object_ = object;
121        this->next_ = 0;
122        this->prev_ = 0;
123    }
124
125    ObjectListElement::~ObjectListElement()
126    {
127    }
128}
Note: See TracBrowser for help on using the repository browser.