#ifndef _LIST_H #define _LIST_H #include "stdincl.h" //! An enum to list all the modes available when adding an object to a List //enum ADDMODE {LIST_ADD_FIRST, LIST_ADD_LAST}; //! An enum to list the two searching directions available when removing an object from a List //enum FINDMODE {LIST_FIND_BW, LIST_FIND_FW}; class WorldEntity; class List { public: List (); ~List (); void add(WorldEntity* entity); void remove(WorldEntity* entity); void destroy(); WorldEntity* firstElement(); bool isEmpty(); int getSize(); WorldEntity* enumerate(); WorldEntity* nextElement(); WorldEntity* toArray(); void debug(); private: struct listElement { listElement* prev; WorldEntity* curr; listElement* next; }; Uint32 size; listElement* first; listElement* last; listElement* currentEl; }; template struct listElement { listElement* prev; T* curr; listElement* next; }; template class tIterator { public: tIterator(listElement* startElement); ~tIterator(); T* nextElement(); private: listElement* currentEl; listElement* firstEl; }; template tIterator::tIterator (listElement* startElement) { this->currentEl = startElement; this->firstEl = startElement; } template tIterator::~tIterator () { this->currentEl = NULL; } template inline T* tIterator::nextElement () { if( this->currentEl == NULL) { PRINTF(1)("Iterator has not been initialized - there is no currentEl\n"); return NULL; } if( this->currentEl->next == NULL) return NULL; return this->currentEl->next->curr; } template class tList { public: tList (); ~tList (); void add(T* entity); void remove(T* entity); void destroy(); T* firstElement(); bool isEmpty(); int getSize(); T* enumerate(); tIterator* getIterator(); T* nextElement(); T* nextElement(T* toEntity); T* toArray(); void debug(); private: Uint32 size; listElement* first; listElement* last; listElement* currentEl; }; template tList::tList () { this->first = NULL; this->last = NULL; this->size = 0; } template tList::~tList () { this->currentEl = this->first; while(this->currentEl != NULL) { listElement* le = this->currentEl->next; //delete this->currentEl->curr; delete this->currentEl; this->currentEl = le; } this->first = NULL; this->last = NULL; this->size = 0; } template void tList::add(T* entity) { if( entity == NULL) return; listElement* el = new listElement; el->prev = this->last; el->curr = entity; el->next = NULL; this->last = el; if(el->prev == NULL) this->first = el; /* if first element */ else el->prev->next = el; this->size++; } template void tList::remove(T* entity) { if( entity == NULL) return; this->currentEl = this->first; listElement* te; while( this->currentEl != NULL) { if( this->currentEl->curr == entity) { if( this->currentEl->prev == NULL ) this->first = this->currentEl->next; else this->currentEl->prev->next = this->currentEl->next; if( this->currentEl->next == NULL) this->last = this->currentEl->prev; else this->currentEl->next->prev = this->currentEl->prev; te = this->currentEl->next; // for what am i doing this? delete this->currentEl; this->currentEl = te; this->size--; return; } this->currentEl = this->currentEl->next; } } template void tList::destroy() { this->currentEl = this->first; while(this->currentEl != NULL) { listElement* le = this->currentEl->next; //delete this->currentEl->curr; delete this->currentEl; this->currentEl = le; } this->first = NULL; this->last = NULL; this->size = 0; } template T* tList::firstElement() { return this->first->curr; } template bool tList::isEmpty() { return (this->size==0)?true:false; } template int tList::getSize() { return this->size; } template T* tList::enumerate() { //if( this->last == this->first == NULL) return NULL; if( this->size == 0) return NULL; this->currentEl = this->first; return this->currentEl->curr; } template tIterator* tList::getIterator() { if( this->size == 0) return NULL; tIterator* iterator = new tIterator(this->first); return iterator; } template T* tList::nextElement() { // if( this->last == this->first == NULL) return NULL; if( this->size == 0) return NULL; this->currentEl = this->currentEl->next; if( this->currentEl == NULL) return NULL; return this->currentEl->curr; } /** \brief this returns the next element after toEntity or the first if toEntity is last */ template T* tList::nextElement(T* toEntity) { //if( this->last == this->first == NULL) return NULL; if(this->size == 0) return NULL; if( toEntity == NULL) return this->first->curr; if( toEntity == this->last->curr ) return this->first->curr; this->currentEl = this->first; while(this->currentEl->curr != toEntity && this->currentEl->next != NULL) { this->currentEl = this->currentEl->next; } if(this->currentEl == NULL) return NULL; return this->currentEl->next->curr; } template T* tList::toArray() {} #endif /* _LIST_H */