[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 | |
---|
[4488] | 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(); |
---|
| 34 | |
---|
| 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 | |
---|
| 47 | normaly you will use it like this: |
---|
| 48 | |
---|
| 49 | tIterator<char>* nameIterator = nameList->getIterator(); |
---|
| 50 | char name* = nameIterator->nextElement(); |
---|
| 51 | while( name != NULL) |
---|
| 52 | { |
---|
| 53 | PRINTF(3)("found name: %s in list\n", name); |
---|
| 54 | name = nameIterator->nextElement(); |
---|
| 55 | } |
---|
| 56 | delete nameIterator; |
---|
| 57 | */ |
---|
[3652] | 58 | template<class T> |
---|
[3653] | 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 | */ |
---|
[2822] | 98 | template<class T> class tList |
---|
| 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(); |
---|
[3652] | 112 | tIterator<T>* getIterator(); |
---|
[3585] | 113 | T* nextElement(T* toEntity); |
---|
[2822] | 114 | T* toArray(); |
---|
[3652] | 115 | |
---|
| 116 | private: |
---|
[4497] | 117 | unsigned int size; //!< the size (lenght) of the list |
---|
| 118 | listElement<T>* first; //!< pointer to the first element |
---|
| 119 | listElement<T>* last; //!< pointer to the last element |
---|
| 120 | listElement<T>* currentEl; //!< pointer to the current element |
---|
[2822] | 121 | }; |
---|
| 122 | |
---|
| 123 | |
---|
[4497] | 124 | /** |
---|
| 125 | \brief the constructor |
---|
| 126 | */ |
---|
[2822] | 127 | template<class T> |
---|
[3831] | 128 | inline tList<T>::tList () |
---|
[2822] | 129 | { |
---|
| 130 | this->first = NULL; |
---|
| 131 | this->last = NULL; |
---|
| 132 | this->size = 0; |
---|
| 133 | } |
---|
| 134 | |
---|
[4497] | 135 | |
---|
| 136 | /** |
---|
| 137 | \brief the deconstructor |
---|
| 138 | |
---|
| 139 | this will delete only the list. ATTENTION: the list is deleted, but the objects in the list will |
---|
| 140 | not be deleted |
---|
| 141 | */ |
---|
[2822] | 142 | template<class T> |
---|
[3831] | 143 | inline tList<T>::~tList () |
---|
[3553] | 144 | { |
---|
| 145 | this->currentEl = this->first; |
---|
| 146 | while(this->currentEl != NULL) |
---|
| 147 | { |
---|
[3652] | 148 | listElement<T>* le = this->currentEl->next; |
---|
[3553] | 149 | //delete this->currentEl->curr; |
---|
| 150 | delete this->currentEl; |
---|
| 151 | this->currentEl = le; |
---|
| 152 | } |
---|
| 153 | this->first = NULL; |
---|
| 154 | this->last = NULL; |
---|
| 155 | this->size = 0; |
---|
| 156 | } |
---|
[2822] | 157 | |
---|
[3553] | 158 | |
---|
[4497] | 159 | /** |
---|
| 160 | \brief add an entity to the list |
---|
| 161 | \param entity: the entity to add |
---|
| 162 | */ |
---|
[2822] | 163 | template<class T> |
---|
[3661] | 164 | inline void tList<T>::add(T* entity) |
---|
[2822] | 165 | { |
---|
[3860] | 166 | if( unlikely(entity == NULL)) return; |
---|
[3652] | 167 | listElement<T>* el = new listElement<T>; |
---|
[2822] | 168 | el->prev = this->last; |
---|
| 169 | el->curr = entity; |
---|
| 170 | el->next = NULL; |
---|
| 171 | |
---|
| 172 | this->last = el; |
---|
| 173 | |
---|
[3860] | 174 | if( unlikely(el->prev == NULL)) this->first = el; /* if first element */ |
---|
[2822] | 175 | else el->prev->next = el; |
---|
| 176 | this->size++; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | |
---|
[4497] | 180 | /** |
---|
| 181 | \brief remove an entity from the list |
---|
| 182 | \param entity: the entity to be removed |
---|
| 183 | */ |
---|
[2822] | 184 | template<class T> |
---|
[3661] | 185 | inline void tList<T>::remove(T* entity) |
---|
[2822] | 186 | { |
---|
| 187 | this->currentEl = this->first; |
---|
[3652] | 188 | listElement<T>* te; |
---|
[2822] | 189 | while( this->currentEl != NULL) |
---|
| 190 | { |
---|
[3860] | 191 | if( unlikely(this->currentEl->curr == entity)) |
---|
[3221] | 192 | { |
---|
[3860] | 193 | if( unlikely(this->currentEl->prev == NULL)) this->first = this->currentEl->next; |
---|
[2822] | 194 | else this->currentEl->prev->next = this->currentEl->next; |
---|
| 195 | |
---|
[3860] | 196 | if( unlikely(this->currentEl->next == NULL)) this->last = this->currentEl->prev; |
---|
[2822] | 197 | else this->currentEl->next->prev = this->currentEl->prev; |
---|
| 198 | |
---|
| 199 | delete this->currentEl; |
---|
[3534] | 200 | this->size--; |
---|
[2822] | 201 | return; |
---|
| 202 | } |
---|
| 203 | this->currentEl = this->currentEl->next; |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | |
---|
[4497] | 208 | /** |
---|
| 209 | \brief this will deletes the objects from the list |
---|
| 210 | */ |
---|
[2822] | 211 | template<class T> |
---|
[4497] | 212 | inline void tList<T>::flush() |
---|
[2822] | 213 | { |
---|
| 214 | this->currentEl = this->first; |
---|
| 215 | while(this->currentEl != NULL) |
---|
| 216 | { |
---|
[3652] | 217 | listElement<T>* le = this->currentEl->next; |
---|
[4497] | 218 | delete this->currentEl->curr; |
---|
[2822] | 219 | delete this->currentEl; |
---|
| 220 | this->currentEl = le; |
---|
| 221 | } |
---|
| 222 | this->first = NULL; |
---|
| 223 | this->last = NULL; |
---|
| 224 | this->size = 0; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | |
---|
[4497] | 228 | /** |
---|
| 229 | \brief returns the first element of the list |
---|
| 230 | \returns first element |
---|
| 231 | */ |
---|
[2822] | 232 | template<class T> |
---|
[3831] | 233 | inline T* tList<T>::firstElement() |
---|
[2822] | 234 | { |
---|
| 235 | return this->first->curr; |
---|
| 236 | } |
---|
| 237 | |
---|
[3832] | 238 | |
---|
[4497] | 239 | /** |
---|
| 240 | \brief function returns the last element of the list |
---|
| 241 | \returns the last element |
---|
| 242 | */ |
---|
[3790] | 243 | template<class T> |
---|
[3831] | 244 | inline T* tList<T>::lastElement() |
---|
[3790] | 245 | { |
---|
| 246 | return this->last->curr; |
---|
| 247 | } |
---|
[2822] | 248 | |
---|
[3790] | 249 | |
---|
[4497] | 250 | /** |
---|
| 251 | \brief returns true if the list is empty |
---|
| 252 | \returns true if the list is empty |
---|
| 253 | */ |
---|
[2822] | 254 | template<class T> |
---|
[3831] | 255 | inline bool tList<T>::isEmpty() |
---|
[2822] | 256 | { |
---|
| 257 | return (this->size==0)?true:false; |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | |
---|
[4497] | 261 | /** |
---|
| 262 | \brief this returns the number of elements in the list |
---|
| 263 | \returns number of elements |
---|
| 264 | */ |
---|
[2822] | 265 | template<class T> |
---|
[3831] | 266 | inline int tList<T>::getSize() |
---|
[2822] | 267 | { |
---|
| 268 | return this->size; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | |
---|
[4497] | 272 | /** |
---|
| 273 | \brief creates an itereator object and returns it |
---|
| 274 | \returns the iterator object to this list |
---|
| 275 | |
---|
| 276 | You will use this, if you want to iterate through the list |
---|
[3832] | 277 | */ |
---|
[2822] | 278 | template<class T> |
---|
[3653] | 279 | inline tIterator<T>* tList<T>::getIterator() |
---|
[3652] | 280 | { |
---|
| 281 | tIterator<T>* iterator = new tIterator<T>(this->first); |
---|
| 282 | return iterator; |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | |
---|
[2822] | 286 | |
---|
[3585] | 287 | /** |
---|
| 288 | \brief this returns the next element after toEntity or the first if toEntity is last |
---|
[4497] | 289 | \param toEntity: the entity after which is an entity, that has to be returned, sorry, terrible phrase |
---|
| 290 | \returns the element after toEntity |
---|
[3585] | 291 | */ |
---|
[2822] | 292 | template<class T> |
---|
[3831] | 293 | inline T* tList<T>::nextElement(T* toEntity) |
---|
[3585] | 294 | { |
---|
[3586] | 295 | //if( this->last == this->first == NULL) return NULL; |
---|
| 296 | if(this->size == 0) return NULL; |
---|
[3585] | 297 | if( toEntity == NULL) return this->first->curr; |
---|
| 298 | if( toEntity == this->last->curr ) return this->first->curr; |
---|
| 299 | this->currentEl = this->first; |
---|
| 300 | while(this->currentEl->curr != toEntity && this->currentEl->next != NULL) |
---|
| 301 | { |
---|
| 302 | this->currentEl = this->currentEl->next; |
---|
| 303 | } |
---|
| 304 | if(this->currentEl == NULL) return NULL; |
---|
| 305 | return this->currentEl->next->curr; |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | |
---|
[4497] | 309 | /** |
---|
| 310 | \brief creates an array out of the list (ATTENTION: not implemented) |
---|
| 311 | \returns pointer to the array beginning |
---|
| 312 | |
---|
| 313 | ATTENTION: function is not implemented and wont do anything |
---|
| 314 | */ |
---|
[3585] | 315 | template<class T> |
---|
[2822] | 316 | T* tList<T>::toArray() |
---|
[4497] | 317 | { |
---|
| 318 | return NULL; |
---|
| 319 | } |
---|
[2822] | 320 | |
---|
[3224] | 321 | #endif /* _LIST_H */ |
---|