Changeset 5229 in orxonox.OLD for trunk/src/lib
- Timestamp:
- Sep 23, 2005, 11:47:18 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/util/list.h
r5211 r5229 26 26 27 27 you will use this as a generic list for all type of objects 28 */28 */ 29 29 template<class T> class tList 30 30 { 31 31 friend class tIterator<T>; 32 32 33 public:34 tList ();35 ~tList ();36 37 void add(T* entity);38 void addAtBeginning(T* entity); //!< @todo This should be made with an ENUM39 void remove(const T* entity);40 void removeLast();41 void flush();42 T* firstElement() const;43 T* lastElement() const;44 bool isEmpty() const;45 unsigned int getSize() const;46 bool inList(T* entity);47 tIterator<T>* getIterator() const;48 T* nextElement(T* toEntity);49 T* toArray();50 51 private:52 unsigned int size; //!< the size (lenght) of the list53 listElement<T>* first; //!< pointer to the first element54 listElement<T>* last; //!< pointer to the last element55 listElement<T>* currentEl; //!< pointer to the current element33 public: 34 tList (); 35 ~tList (); 36 37 void add(T* entity); 38 void addAtBeginning(T* entity); //!< @todo This should be made with an ENUM 39 void remove(const T* entity); 40 void removeLast(); 41 void flush(); 42 T* firstElement() const; 43 T* lastElement() const; 44 bool isEmpty() const; 45 unsigned int getSize() const; 46 bool inList(T* entity) const; 47 tIterator<T>* getIterator() const; 48 T* nextElement(T* toEntity); 49 T* toArray(); 50 51 private: 52 unsigned int size; //!< the size (lenght) of the list 53 listElement<T>* first; //!< pointer to the first element 54 listElement<T>* last; //!< pointer to the last element 55 listElement<T>* currentEl; //!< pointer to the current element 56 56 }; 57 57 … … 59 59 /** 60 60 * the constructor 61 */ 62 template<class T> 63 inline tList<T>::tList () 64 { 61 */ 62 template<class T> 63 inline tList<T>::tList () 64 { 65 this->currentEl = NULL; 65 66 this->first = NULL; 66 67 this->last = NULL; … … 72 73 * the deconstructor 73 74 74 this will delete only the list. ATTENTION: the list is deleted, but the objects in the list will 75 not be deleted 76 */ 77 template<class T> 78 inline tList<T>::~tList () 75 this will delete only the list. ATTENTION: the list is deleted, but the objects in the list will remain 76 */ 77 template<class T> 78 inline tList<T>::~tList () 79 79 { 80 80 this->currentEl = this->first; 81 listElement<T>* le; 81 82 while(this->currentEl != NULL) 82 { 83 listElement<T>* le = this->currentEl->next; 84 //delete this->currentEl->curr; //! THIS IS EXTREMELY UNSAFE (the list only stores pointers not instances) // 85 delete this->currentEl; 86 this->currentEl = le; 87 } 83 { 84 le = this->currentEl->next; 85 delete this->currentEl; 86 this->currentEl = le; 87 } 88 88 this->first = NULL; 89 89 this->last = NULL; 90 this->currentEl = NULL; 90 91 this->size = 0; 91 92 } … … 95 96 * add an entity to the list 96 97 * @param entity: the entity to add 97 */98 template<class T> 99 inline void tList<T>::add(T* entity)98 */ 99 template<class T> 100 inline void tList<T>::add(T* entity) 100 101 { 101 102 if( unlikely(entity == NULL)) return; … … 127 128 this->first = el; 128 129 129 if( unlikely(el->next == NULL)) this-> first = el; /* if first element */130 if( unlikely(el->next == NULL)) this->last = el; /* if first element */ 130 131 else el->next->prev = el; 131 132 this->size++; … … 136 137 * remove an entity from the list 137 138 * @param entity: the entity to be removed 138 */139 template<class T> 140 inline void tList<T>::remove(const T* entity)139 */ 140 template<class T> 141 inline void tList<T>::remove(const T* entity) 141 142 { 142 143 this->currentEl = this->first; 143 144 while( this->currentEl != NULL) 145 { 146 if( unlikely(this->currentEl->curr == entity)) 144 147 { 145 if( unlikely(this->currentEl->curr == entity)) 146 { 147 if( unlikely(this->currentEl->prev == NULL)) this->first = this->currentEl->next; 148 else this->currentEl->prev->next = this->currentEl->next; 149 150 if( unlikely(this->currentEl->next == NULL)) this->last = this->currentEl->prev; 151 else this->currentEl->next->prev = this->currentEl->prev; 152 153 delete this->currentEl; 154 this->size--; 155 return; 156 } 157 this->currentEl = this->currentEl->next; 148 // erstes element? 149 if( unlikely(this->currentEl->prev == NULL)) this->first = this->currentEl->next; 150 else this->currentEl->prev->next = this->currentEl->next; 151 152 // letztes element? 153 if( unlikely(this->currentEl->next == NULL)) this->last = this->currentEl->prev; 154 else this->currentEl->next->prev = this->currentEl->prev; 155 156 delete this->currentEl; 157 this->size--; 158 return; 158 159 } 159 } 160 this->currentEl = this->currentEl->next; 161 } 162 } 163 160 164 161 165 /** … … 165 169 inline void tList<T>::removeLast() 166 170 { 167 if (this->last == NULL)171 if( unlikely(this->last == NULL)) 168 172 return; 169 else if (this->last == this->first) 173 174 // only one element in the list (and its not NULL :D ) 175 if( unlikely(this->last == this->first)) 170 176 { 171 177 delete this->first; … … 183 189 } 184 190 185 /** 186 * this will deletes the objects from the list 187 */ 188 template<class T> 189 inline void tList<T>::flush() 191 192 /** 193 * this will delete all objects from the list, this can be very dangerous if there are ghost objects in the list. 194 */ 195 template<class T> 196 inline void tList<T>::flush() 190 197 { 191 198 this->currentEl = this->first; 192 while(this->currentEl != NULL) 193 { 194 listElement<T>* le = this->currentEl->next; 195 delete this->currentEl->curr; 196 delete this->currentEl; 197 this->currentEl = le; 198 } 199 200 listElement<T>* le; 201 while( this->currentEl != NULL) 202 { 203 le = this->currentEl->next; 204 delete this->currentEl->curr; 205 delete this->currentEl; 206 this->currentEl = le; 207 } 199 208 this->first = NULL; 200 209 this->last = NULL; … … 206 215 * returns the first element of the list 207 216 * @returns first element 208 */209 template<class T> 210 inline T* tList<T>::firstElement() const217 */ 218 template<class T> 219 inline T* tList<T>::firstElement() const 211 220 { 212 221 return this->first->curr; … … 217 226 * function returns the last element of the list 218 227 * @returns the last element 219 */220 template<class T> 221 inline T* tList<T>::lastElement() const228 */ 229 template<class T> 230 inline T* tList<T>::lastElement() const 222 231 { 223 232 return this->last->curr; … … 228 237 * returns true if the list is empty 229 238 * @returns true if the list is empty 230 */ 231 template<class T> 232 inline bool tList<T>::isEmpty() const 233 { 234 return (this->size==0)?true:false; 235 } 236 237 /** 238 * checks if an entity is in the List 239 */ 240 template<class T> 241 inline bool tList<T>::isEmpty() const 242 { 243 return (this->size == 0)?true:false; 244 } 245 246 247 /** 248 * checks if an entity is in the List. 239 249 * @param entity The entity to check for in the entire List. 240 250 * @returns true if it is, false otherwise 241 */242 template<class T> 243 inline bool tList<T>::inList(T* entity) 251 */ 252 template<class T> 253 inline bool tList<T>::inList(T* entity) const 244 254 { 245 255 // pre checks 246 if(this->size == 0) return false; 247 if( entity == NULL) return false; 256 if( unlikely(entity == NULL)) return false; 248 257 249 258 // search in the List 250 this->currentEl = this->first;251 while( this->currentEl->curr != entity &&this->currentEl != NULL)252 this->currentEl = this->currentEl->next;253 254 // post checks255 if(this->currentEl == NULL) 256 return false;257 else258 return true; 259 } 259 listElement<T>* el = this->first; 260 while( this->currentEl != NULL) 261 { 262 if( this->currentEl->curr == entity) 263 return true; 264 265 el = this->currentEl->next; 266 } 267 } 268 260 269 261 270 /** 262 271 * this returns the number of elements in the list 263 272 * @returns number of elements 264 */265 template<class T> 266 inline unsigned int tList<T>::getSize() const273 */ 274 template<class T> 275 inline unsigned int tList<T>::getSize() const 267 276 { 268 277 return this->size; … … 275 284 276 285 You will use this, if you want to iterate through the list 277 */278 template<class T> 279 inline tIterator<T>* tList<T>::getIterator() const286 */ 287 template<class T> 288 inline tIterator<T>* tList<T>::getIterator() const 280 289 { 281 290 return new tIterator<T>(this); … … 288 297 * @param toEntity: the entity after which is an entity, that has to be returned, sorry, terrible phrase 289 298 * @returns the element after toEntity 290 */291 template<class T> 292 inline T* tList<T>::nextElement(T* toEntity)299 */ 300 template<class T> 301 inline T* tList<T>::nextElement(T* toEntity) 293 302 { 294 303 //if( this->last == this->first == NULL) return NULL; … … 298 307 this->currentEl = this->first; 299 308 while(this->currentEl->curr != toEntity && this->currentEl->next != NULL) 300 301 302 309 { 310 this->currentEl = this->currentEl->next; 311 } 303 312 if(this->currentEl == NULL) return NULL; 304 313 return this->currentEl->next->curr; … … 311 320 312 321 ATTENTION: function is not implemented and wont do anything 313 */314 template<class T> 315 T* tList<T>::toArray()322 */ 323 template<class T> 324 T* tList<T>::toArray() 316 325 { 317 326 return NULL; … … 322 331 323 332 /** 324 * an iterator class333 * an iterator class 325 334 326 335 this enables the user to iterate through a list very easely … … 336 345 T* nextElement(); 337 346 T* prevElement(); 338 T* seekElement( T* element);347 T* seekElement(const T* element); 339 348 T* iteratorElement(const tIterator<T>* iterator); 340 349 … … 376 385 template<class T> 377 386 inline tIterator<T>::~tIterator () 378 { 379 } 380 387 {} 388 389 /** 390 * this returns the first element of the iterator list 391 * @returns first element 392 */ 381 393 template<class T> 382 394 inline T* tIterator<T>::firstElement () 383 395 { 384 396 this->tmpEl = this->list->first; 385 if (this->tmpEl != NULL)397 if( likely(this->tmpEl != NULL)) 386 398 { 387 399 this->currentEl = this->tmpEl->next; … … 392 404 } 393 405 406 /** 407 * this returns the last element of the iterator list 408 * @returns last element 409 */ 394 410 template<class T> 395 411 inline T* tIterator<T>::lastElement () 396 412 { 397 413 this->tmpEl = this->list->last; 398 if (this->tmpEl != NULL)414 if( likely(this->tmpEl != NULL)) 399 415 { 400 416 this->currentEl = tmpEl->prev; … … 413 429 inline T* tIterator<T>::nextElement () 414 430 { 415 if( this->currentEl == NULL)431 if( unlikely(this->currentEl == NULL)) 416 432 return NULL; 417 433 … … 422 438 } 423 439 440 424 441 /** 425 442 * use it to iterate backwards through the list … … 429 446 inline T* tIterator<T>::prevElement () 430 447 { 431 if( this->currentEl == NULL)448 if( unlikely(this->currentEl == NULL)) 432 449 return NULL; 433 450 … … 445 462 */ 446 463 template<class T> 447 inline T* tIterator<T>::seekElement ( T* element)448 { 449 if (element == NULL)464 inline T* tIterator<T>::seekElement (const T* element) 465 { 466 if( unlikely(element == NULL)) 450 467 { 451 468 this->currentEl = NULL; … … 476 493 T* tIterator<T>::iteratorElement(const tIterator<T>* iterator) 477 494 { 478 if (iterator != NULL && iterator->list == this->list)495 if( unlikely(iterator != NULL && iterator->list == this->list)) 479 496 { 480 497 this->currentEl = iterator->currentEl;
Note: See TracChangeset
for help on using the changeset viewer.