[2636] | 1 | |
---|
[3224] | 2 | #ifndef _LIST_H |
---|
| 3 | #define _LIST_H |
---|
[2036] | 4 | |
---|
[2816] | 5 | #include "stdincl.h" |
---|
[2036] | 6 | |
---|
[2077] | 7 | //! An enum to list all the modes available when adding an object to a List |
---|
[2816] | 8 | //enum ADDMODE {LIST_ADD_FIRST, LIST_ADD_LAST}; |
---|
[2077] | 9 | //! An enum to list the two searching directions available when removing an object from a List |
---|
[2816] | 10 | //enum FINDMODE {LIST_FIND_BW, LIST_FIND_FW}; |
---|
[2036] | 11 | |
---|
| 12 | |
---|
[2077] | 13 | |
---|
[2816] | 14 | class WorldEntity; |
---|
[2036] | 15 | |
---|
[2816] | 16 | class List { |
---|
[2036] | 17 | |
---|
[2816] | 18 | public: |
---|
| 19 | List (); |
---|
| 20 | ~List (); |
---|
[2036] | 21 | |
---|
[2816] | 22 | void add(WorldEntity* entity); |
---|
| 23 | void remove(WorldEntity* entity); |
---|
[3194] | 24 | void destroy(); |
---|
[2816] | 25 | WorldEntity* firstElement(); |
---|
| 26 | bool isEmpty(); |
---|
| 27 | int getSize(); |
---|
| 28 | WorldEntity* enumerate(); |
---|
| 29 | WorldEntity* nextElement(); |
---|
| 30 | WorldEntity* toArray(); |
---|
| 31 | void debug(); |
---|
[2077] | 32 | |
---|
[2816] | 33 | private: |
---|
| 34 | struct listElement |
---|
[2077] | 35 | { |
---|
[2816] | 36 | listElement* prev; |
---|
| 37 | WorldEntity* curr; |
---|
| 38 | listElement* next; |
---|
| 39 | }; |
---|
| 40 | Uint32 size; |
---|
| 41 | listElement* first; |
---|
| 42 | listElement* last; |
---|
| 43 | listElement* currentEl; |
---|
[2077] | 44 | |
---|
| 45 | |
---|
[2816] | 46 | }; |
---|
[2077] | 47 | |
---|
[2816] | 48 | class Iterator |
---|
[2077] | 49 | { |
---|
| 50 | |
---|
[2816] | 51 | public: |
---|
| 52 | bool hasNext(); |
---|
| 53 | WorldEntity* next(); |
---|
[2077] | 54 | |
---|
[2816] | 55 | private: |
---|
[2077] | 56 | |
---|
[2816] | 57 | }; |
---|
| 58 | |
---|
[2822] | 59 | |
---|
| 60 | template<class T> class tList |
---|
| 61 | { |
---|
| 62 | private: |
---|
| 63 | struct listElement |
---|
| 64 | { |
---|
| 65 | listElement* prev; |
---|
| 66 | T* curr; |
---|
| 67 | listElement* next; |
---|
| 68 | }; |
---|
| 69 | |
---|
| 70 | Uint32 size; |
---|
| 71 | listElement* first; |
---|
| 72 | listElement* last; |
---|
| 73 | listElement* currentEl; |
---|
| 74 | |
---|
| 75 | public: |
---|
| 76 | tList (); |
---|
| 77 | ~tList (); |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | void add(WorldEntity* entity); |
---|
| 81 | void remove(WorldEntity* entity); |
---|
[3194] | 82 | void destroy(); |
---|
[2822] | 83 | T* firstElement(); |
---|
| 84 | bool isEmpty(); |
---|
| 85 | int getSize(); |
---|
| 86 | T* enumerate(); |
---|
| 87 | T* nextElement(); |
---|
| 88 | T* toArray(); |
---|
| 89 | void debug(); |
---|
| 90 | }; |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | template<class T> |
---|
| 94 | tList<T>::tList () |
---|
| 95 | { |
---|
| 96 | this->first = NULL; |
---|
| 97 | this->last = NULL; |
---|
| 98 | this->size = 0; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | template<class T> |
---|
| 102 | tList<T>::~tList () |
---|
| 103 | {} |
---|
| 104 | |
---|
| 105 | template<class T> |
---|
| 106 | void tList<T>::add(WorldEntity* entity) |
---|
| 107 | { |
---|
| 108 | listElement* el = new listElement; |
---|
| 109 | el->prev = this->last; |
---|
| 110 | el->curr = entity; |
---|
| 111 | el->next = NULL; |
---|
| 112 | |
---|
| 113 | this->last = el; |
---|
| 114 | |
---|
| 115 | if(this->size == 0) this->first = el; |
---|
| 116 | else el->prev->next = el; |
---|
| 117 | this->size++; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | template<class T> |
---|
| 122 | void tList<T>::remove(WorldEntity* entity) |
---|
| 123 | { |
---|
| 124 | this->currentEl = this->first; |
---|
| 125 | listElement* te; |
---|
| 126 | while( this->currentEl != NULL) |
---|
| 127 | { |
---|
| 128 | if( this->currentEl->curr == entity) |
---|
[3221] | 129 | { |
---|
[2822] | 130 | if( this->currentEl->prev == NULL ) this->first = this->currentEl->next; |
---|
| 131 | else this->currentEl->prev->next = this->currentEl->next; |
---|
| 132 | |
---|
| 133 | if( this->currentEl->next == NULL) this->last = this->currentEl->prev; |
---|
| 134 | else this->currentEl->next->prev = this->currentEl->prev; |
---|
| 135 | |
---|
| 136 | te = this->currentEl->next; |
---|
| 137 | delete this->currentEl; |
---|
| 138 | this->currentEl = te; |
---|
| 139 | return; |
---|
| 140 | } |
---|
| 141 | this->currentEl = this->currentEl->next; |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | template<class T> |
---|
[3194] | 147 | void tList<T>::destroy() |
---|
[2822] | 148 | { |
---|
| 149 | this->currentEl = this->first; |
---|
| 150 | while(this->currentEl != NULL) |
---|
| 151 | { |
---|
| 152 | listElement* le = this->currentEl->next; |
---|
| 153 | delete this->currentEl->curr; |
---|
| 154 | delete this->currentEl; |
---|
| 155 | this->currentEl = le; |
---|
| 156 | } |
---|
| 157 | this->first = NULL; |
---|
| 158 | this->last = NULL; |
---|
| 159 | this->size = 0; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | template<class T> |
---|
| 164 | T* tList<T>::firstElement() |
---|
| 165 | { |
---|
| 166 | return this->first->curr; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | template<class T> |
---|
| 171 | bool tList<T>::isEmpty() |
---|
| 172 | { |
---|
| 173 | return (this->size==0)?true:false; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | template<class T> |
---|
| 178 | int tList<T>::getSize() |
---|
| 179 | { |
---|
| 180 | return this->size; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | template<class T> |
---|
| 185 | T* tList<T>::enumerate() |
---|
| 186 | { |
---|
| 187 | if(this->size == 0) return NULL; |
---|
| 188 | this->currentEl = this->first; |
---|
| 189 | return this->currentEl->curr; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | template<class T> |
---|
| 194 | T* tList<T>::nextElement() |
---|
| 195 | { |
---|
| 196 | if(this->size == 0) return NULL; |
---|
| 197 | this->currentEl = this->currentEl->next; |
---|
| 198 | if(this->currentEl == NULL) return NULL; |
---|
| 199 | return this->currentEl->curr; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | |
---|
| 203 | template<class T> |
---|
| 204 | T* tList<T>::toArray() |
---|
| 205 | {} |
---|
| 206 | |
---|
[3224] | 207 | #endif /* _LIST_H */ |
---|