[4486] | 1 | /*! |
---|
| 2 | \file list.h |
---|
| 3 | \brief a File that includes a List-template |
---|
| 4 | */ |
---|
[2636] | 5 | |
---|
[3224] | 6 | #ifndef _LIST_H |
---|
| 7 | #define _LIST_H |
---|
[2036] | 8 | |
---|
[3860] | 9 | #include "compiler.h" |
---|
| 10 | #ifndef NULL |
---|
[4499] | 11 | #define NULL 0 //!< this will define NULL |
---|
[3860] | 12 | #endif |
---|
[2036] | 13 | |
---|
| 14 | |
---|
| 15 | |
---|
[4574] | 16 | //! a list element of the tList, |
---|
[3652] | 17 | template<class T> struct listElement |
---|
[2077] | 18 | { |
---|
[4488] | 19 | listElement* prev; //!< pointer to the previous listElement in the list |
---|
| 20 | T* curr; //!< pointer to the list payload/container |
---|
| 21 | listElement* next; //!< pointer to the next listElement |
---|
[3652] | 22 | }; |
---|
[2077] | 23 | |
---|
[4488] | 24 | /** |
---|
| 25 | \brief an iterator class |
---|
| 26 | |
---|
| 27 | this enables the user to iterate through a list very easely |
---|
| 28 | */ |
---|
[3652] | 29 | template<class T> class tIterator |
---|
| 30 | { |
---|
[2816] | 31 | public: |
---|
[3652] | 32 | tIterator(listElement<T>* startElement); |
---|
| 33 | ~tIterator(); |
---|
[4574] | 34 | |
---|
[3652] | 35 | T* nextElement(); |
---|
[2077] | 36 | |
---|
[2816] | 37 | private: |
---|
[4488] | 38 | listElement<T>* currentEl; //!< pointer to the current list element in the iterator |
---|
| 39 | listElement<T>* tmpEl; //!< temp listElemnt pointer |
---|
[2816] | 40 | }; |
---|
| 41 | |
---|
[2822] | 42 | |
---|
[4488] | 43 | /** |
---|
| 44 | \brief iterator constructor |
---|
| 45 | \param startElement: the first list element from the tList |
---|
| 46 | |
---|
[4574] | 47 | normaly you will use it like this: |
---|
[4488] | 48 | |
---|
| 49 | tIterator<char>* nameIterator = nameList->getIterator(); |
---|
[4574] | 50 | char name* = nameIterator->nextElement(); |
---|
[4488] | 51 | while( name != NULL) |
---|
| 52 | { |
---|
| 53 | PRINTF(3)("found name: %s in list\n", name); |
---|
[4574] | 54 | name = nameIterator->nextElement(); |
---|
[4488] | 55 | } |
---|
[4574] | 56 | delete nameIterator; |
---|
[4488] | 57 | */ |
---|
[3652] | 58 | template<class T> |
---|
[4574] | 59 | inline tIterator<T>::tIterator (listElement<T>* startElement) |
---|
[3652] | 60 | { |
---|
| 61 | this->currentEl = startElement; |
---|
[3669] | 62 | this->tmpEl = NULL; |
---|
[3652] | 63 | } |
---|
| 64 | |
---|
| 65 | |
---|
[4488] | 66 | /** |
---|
| 67 | \brief the destructor |
---|
| 68 | */ |
---|
[3652] | 69 | template<class T> |
---|
[3831] | 70 | inline tIterator<T>::~tIterator () |
---|
[3652] | 71 | { |
---|
| 72 | this->currentEl = NULL; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
[4488] | 76 | /** |
---|
| 77 | \brief use it to iterate through the list |
---|
| 78 | \returns next list element |
---|
| 79 | */ |
---|
[3652] | 80 | template<class T> |
---|
| 81 | inline T* tIterator<T>::nextElement () |
---|
| 82 | { |
---|
[3668] | 83 | if( this->currentEl == NULL) |
---|
[3661] | 84 | return NULL; |
---|
| 85 | |
---|
[3669] | 86 | this->tmpEl = this->currentEl; |
---|
[3653] | 87 | this->currentEl = this->currentEl->next; |
---|
[3669] | 88 | return this->tmpEl->curr; |
---|
[3652] | 89 | } |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | |
---|
[4497] | 93 | /** |
---|
| 94 | \brief the list template class |
---|
| 95 | |
---|
| 96 | you will use this as a generic list for all type of objects |
---|
| 97 | */ |
---|
[4574] | 98 | template<class T> class tList |
---|
[2822] | 99 | { |
---|
| 100 | public: |
---|
| 101 | tList (); |
---|
| 102 | ~tList (); |
---|
| 103 | |
---|
[3365] | 104 | void add(T* entity); |
---|
| 105 | void remove(T* entity); |
---|
[4497] | 106 | void flush(); |
---|
[2822] | 107 | T* firstElement(); |
---|
[3790] | 108 | T* lastElement(); |
---|
[2822] | 109 | bool isEmpty(); |
---|
| 110 | int getSize(); |
---|
[3832] | 111 | //T* enumerate(); |
---|
[4508] | 112 | bool inList(T* entity); |
---|
[3652] | 113 | tIterator<T>* getIterator(); |
---|
[3585] | 114 | T* nextElement(T* toEntity); |
---|
[2822] | 115 | T* toArray(); |
---|
[3652] | 116 | |
---|
| 117 | private: |
---|
[4497] | 118 | unsigned int size; //!< the size (lenght) of the list |
---|
| 119 | listElement<T>* first; //!< pointer to the first element |
---|
| 120 | listElement<T>* last; //!< pointer to the last element |
---|
| 121 | listElement<T>* currentEl; //!< pointer to the current element |
---|
[2822] | 122 | }; |
---|
| 123 | |
---|
| 124 | |
---|
[4497] | 125 | /** |
---|
| 126 | \brief the constructor |
---|
| 127 | */ |
---|
[2822] | 128 | template<class T> |
---|
[4574] | 129 | inline tList<T>::tList () |
---|
[2822] | 130 | { |
---|
| 131 | this->first = NULL; |
---|
| 132 | this->last = NULL; |
---|
| 133 | this->size = 0; |
---|
| 134 | } |
---|
| 135 | |
---|
[4497] | 136 | |
---|
| 137 | /** |
---|
| 138 | \brief the deconstructor |
---|
| 139 | |
---|
[4574] | 140 | this will delete only the list. ATTENTION: the list is deleted, but the objects in the list will |
---|
[4497] | 141 | not be deleted |
---|
| 142 | */ |
---|
[2822] | 143 | template<class T> |
---|
[4574] | 144 | inline tList<T>::~tList () |
---|
[3553] | 145 | { |
---|
| 146 | this->currentEl = this->first; |
---|
| 147 | while(this->currentEl != NULL) |
---|
| 148 | { |
---|
[3652] | 149 | listElement<T>* le = this->currentEl->next; |
---|
[3553] | 150 | //delete this->currentEl->curr; |
---|
| 151 | delete this->currentEl; |
---|
| 152 | this->currentEl = le; |
---|
| 153 | } |
---|
| 154 | this->first = NULL; |
---|
| 155 | this->last = NULL; |
---|
| 156 | this->size = 0; |
---|
| 157 | } |
---|
[2822] | 158 | |
---|
[3553] | 159 | |
---|
[4497] | 160 | /** |
---|
| 161 | \brief add an entity to the list |
---|
| 162 | \param entity: the entity to add |
---|
| 163 | */ |
---|
[2822] | 164 | template<class T> |
---|
[3661] | 165 | inline void tList<T>::add(T* entity) |
---|
[2822] | 166 | { |
---|
[3860] | 167 | if( unlikely(entity == NULL)) return; |
---|
[3652] | 168 | listElement<T>* el = new listElement<T>; |
---|
[2822] | 169 | el->prev = this->last; |
---|
| 170 | el->curr = entity; |
---|
| 171 | el->next = NULL; |
---|
| 172 | |
---|
| 173 | this->last = el; |
---|
| 174 | |
---|
[3860] | 175 | if( unlikely(el->prev == NULL)) this->first = el; /* if first element */ |
---|
[2822] | 176 | else el->prev->next = el; |
---|
| 177 | this->size++; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | |
---|
[4497] | 181 | /** |
---|
| 182 | \brief remove an entity from the list |
---|
| 183 | \param entity: the entity to be removed |
---|
| 184 | */ |
---|
[2822] | 185 | template<class T> |
---|
[3661] | 186 | inline void tList<T>::remove(T* entity) |
---|
[2822] | 187 | { |
---|
| 188 | this->currentEl = this->first; |
---|
[3652] | 189 | listElement<T>* te; |
---|
[2822] | 190 | while( this->currentEl != NULL) |
---|
| 191 | { |
---|
[3860] | 192 | if( unlikely(this->currentEl->curr == entity)) |
---|
[4574] | 193 | { |
---|
| 194 | if( unlikely(this->currentEl->prev == NULL)) this->first = this->currentEl->next; |
---|
| 195 | else this->currentEl->prev->next = this->currentEl->next; |
---|
[2822] | 196 | |
---|
[4574] | 197 | if( unlikely(this->currentEl->next == NULL)) this->last = this->currentEl->prev; |
---|
| 198 | else this->currentEl->next->prev = this->currentEl->prev; |
---|
[2822] | 199 | |
---|
[4574] | 200 | delete this->currentEl; |
---|
| 201 | this->size--; |
---|
| 202 | return; |
---|
| 203 | } |
---|
[2822] | 204 | this->currentEl = this->currentEl->next; |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | |
---|
[4497] | 209 | /** |
---|
| 210 | \brief this will deletes the objects from the list |
---|
| 211 | */ |
---|
[2822] | 212 | template<class T> |
---|
[4497] | 213 | inline void tList<T>::flush() |
---|
[2822] | 214 | { |
---|
| 215 | this->currentEl = this->first; |
---|
| 216 | while(this->currentEl != NULL) |
---|
| 217 | { |
---|
[3652] | 218 | listElement<T>* le = this->currentEl->next; |
---|
[4497] | 219 | delete this->currentEl->curr; |
---|
[2822] | 220 | delete this->currentEl; |
---|
| 221 | this->currentEl = le; |
---|
| 222 | } |
---|
| 223 | this->first = NULL; |
---|
| 224 | this->last = NULL; |
---|
| 225 | this->size = 0; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | |
---|
[4497] | 229 | /** |
---|
| 230 | \brief returns the first element of the list |
---|
| 231 | \returns first element |
---|
| 232 | */ |
---|
[2822] | 233 | template<class T> |
---|
[3831] | 234 | inline T* tList<T>::firstElement() |
---|
[2822] | 235 | { |
---|
| 236 | return this->first->curr; |
---|
| 237 | } |
---|
| 238 | |
---|
[3832] | 239 | |
---|
[4497] | 240 | /** |
---|
| 241 | \brief function returns the last element of the list |
---|
| 242 | \returns the last element |
---|
| 243 | */ |
---|
[3790] | 244 | template<class T> |
---|
[3831] | 245 | inline T* tList<T>::lastElement() |
---|
[3790] | 246 | { |
---|
| 247 | return this->last->curr; |
---|
| 248 | } |
---|
[2822] | 249 | |
---|
[3790] | 250 | |
---|
[4497] | 251 | /** |
---|
| 252 | \brief returns true if the list is empty |
---|
| 253 | \returns true if the list is empty |
---|
| 254 | */ |
---|
[2822] | 255 | template<class T> |
---|
[3831] | 256 | inline bool tList<T>::isEmpty() |
---|
[2822] | 257 | { |
---|
| 258 | return (this->size==0)?true:false; |
---|
| 259 | } |
---|
| 260 | |
---|
[4508] | 261 | /** |
---|
| 262 | \brief checks if an entity is in the List |
---|
| 263 | \param entity The entity to check for in the entire List. |
---|
| 264 | \returns true if it is, false otherwise |
---|
| 265 | */ |
---|
| 266 | template<class T> |
---|
| 267 | inline bool tList<T>::inList(T* entity) |
---|
[4574] | 268 | { |
---|
[4508] | 269 | // pre checks |
---|
| 270 | if(this->size == 0) return false; |
---|
| 271 | if( entity == NULL) return false; |
---|
[2822] | 272 | |
---|
[4508] | 273 | // search in the List |
---|
| 274 | this->currentEl = this->first; |
---|
| 275 | while(this->currentEl->curr != entity && this->currentEl != NULL) |
---|
| 276 | this->currentEl = this->currentEl->next; |
---|
| 277 | |
---|
| 278 | // post checks |
---|
[4574] | 279 | if(this->currentEl == NULL) |
---|
[4508] | 280 | return false; |
---|
| 281 | else |
---|
| 282 | return true; |
---|
| 283 | } |
---|
| 284 | |
---|
[4497] | 285 | /** |
---|
| 286 | \brief this returns the number of elements in the list |
---|
| 287 | \returns number of elements |
---|
| 288 | */ |
---|
[2822] | 289 | template<class T> |
---|
[3831] | 290 | inline int tList<T>::getSize() |
---|
[2822] | 291 | { |
---|
| 292 | return this->size; |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | |
---|
[4497] | 296 | /** |
---|
| 297 | \brief creates an itereator object and returns it |
---|
| 298 | \returns the iterator object to this list |
---|
| 299 | |
---|
| 300 | You will use this, if you want to iterate through the list |
---|
[3832] | 301 | */ |
---|
[2822] | 302 | template<class T> |
---|
[3653] | 303 | inline tIterator<T>* tList<T>::getIterator() |
---|
[3652] | 304 | { |
---|
| 305 | tIterator<T>* iterator = new tIterator<T>(this->first); |
---|
| 306 | return iterator; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | |
---|
[2822] | 310 | |
---|
[3585] | 311 | /** |
---|
| 312 | \brief this returns the next element after toEntity or the first if toEntity is last |
---|
[4497] | 313 | \param toEntity: the entity after which is an entity, that has to be returned, sorry, terrible phrase |
---|
| 314 | \returns the element after toEntity |
---|
[3585] | 315 | */ |
---|
[2822] | 316 | template<class T> |
---|
[3831] | 317 | inline T* tList<T>::nextElement(T* toEntity) |
---|
[3585] | 318 | { |
---|
[3586] | 319 | //if( this->last == this->first == NULL) return NULL; |
---|
| 320 | if(this->size == 0) return NULL; |
---|
[3585] | 321 | if( toEntity == NULL) return this->first->curr; |
---|
| 322 | if( toEntity == this->last->curr ) return this->first->curr; |
---|
| 323 | this->currentEl = this->first; |
---|
| 324 | while(this->currentEl->curr != toEntity && this->currentEl->next != NULL) |
---|
| 325 | { |
---|
| 326 | this->currentEl = this->currentEl->next; |
---|
| 327 | } |
---|
| 328 | if(this->currentEl == NULL) return NULL; |
---|
| 329 | return this->currentEl->next->curr; |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | |
---|
[4497] | 333 | /** |
---|
| 334 | \brief creates an array out of the list (ATTENTION: not implemented) |
---|
| 335 | \returns pointer to the array beginning |
---|
| 336 | |
---|
| 337 | ATTENTION: function is not implemented and wont do anything |
---|
| 338 | */ |
---|
[3585] | 339 | template<class T> |
---|
[2822] | 340 | T* tList<T>::toArray() |
---|
[4497] | 341 | { |
---|
| 342 | return NULL; |
---|
| 343 | } |
---|
[2822] | 344 | |
---|
[3224] | 345 | #endif /* _LIST_H */ |
---|