Index: trunk/src/lib/util/list.h
===================================================================
--- trunk/src/lib/util/list.h	(revision 5211)
+++ trunk/src/lib/util/list.h	(revision 5229)
@@ -26,32 +26,32 @@
 
    you will use this as a generic list for all type of objects
-*/
+ */
 template<class T> class tList
 {
   friend class tIterator<T>;
 
- public:
-  tList ();
-  ~tList ();
-
-  void add(T* entity);
-  void addAtBeginning(T* entity); //!< @todo This should be made with an ENUM
-  void remove(const T* entity);
-  void removeLast();
-  void flush();
-  T* firstElement() const;
-  T* lastElement() const;
-  bool isEmpty() const;
-  unsigned int getSize() const;
-  bool inList(T* entity);
-  tIterator<T>* getIterator() const;
-  T* nextElement(T* toEntity);
-  T* toArray();
-
- private:
-  unsigned int       size;             //!< the size (lenght) of the list
-  listElement<T>*    first;            //!< pointer to the first element
-  listElement<T>*    last;             //!< pointer to the last element
-  listElement<T>*    currentEl;        //!< pointer to the current element
+  public:
+    tList ();
+    ~tList ();
+
+    void add(T* entity);
+    void addAtBeginning(T* entity); //!< @todo This should be made with an ENUM
+    void remove(const T* entity);
+    void removeLast();
+    void flush();
+    T* firstElement() const;
+    T* lastElement() const;
+    bool isEmpty() const;
+    unsigned int getSize() const;
+    bool inList(T* entity) const;
+    tIterator<T>* getIterator() const;
+    T* nextElement(T* toEntity);
+    T* toArray();
+
+  private:
+    unsigned int       size;             //!< the size (lenght) of the list
+    listElement<T>*    first;            //!< pointer to the first element
+    listElement<T>*    last;             //!< pointer to the last element
+    listElement<T>*    currentEl;        //!< pointer to the current element
 };
 
@@ -59,8 +59,9 @@
 /**
  *  the constructor
-*/
-template<class T>
-inline tList<T>::tList ()
-{
+ */
+template<class T>
+    inline tList<T>::tList ()
+{
+  this->currentEl = NULL;
   this->first = NULL;
   this->last = NULL;
@@ -72,20 +73,20 @@
  *  the deconstructor
 
-   this will delete only the list. ATTENTION: the list is deleted, but the objects in the list will
-   not be deleted
-*/
-template<class T>
-inline tList<T>::~tList ()
+   this will delete only the list. ATTENTION: the list is deleted, but the objects in the list will remain
+ */
+template<class T>
+    inline tList<T>::~tList ()
 {
   this->currentEl = this->first;
+  listElement<T>* le;
   while(this->currentEl != NULL)
-    {
-      listElement<T>* le = this->currentEl->next;
-      //delete this->currentEl->curr;  //! THIS IS EXTREMELY UNSAFE (the list only stores pointers not instances) //
-      delete this->currentEl;
-      this->currentEl = le;
-    }
+  {
+    le = this->currentEl->next;
+    delete this->currentEl;
+    this->currentEl = le;
+  }
   this->first = NULL;
   this->last = NULL;
+  this->currentEl = NULL;
   this->size = 0;
 }
@@ -95,7 +96,7 @@
  *  add an entity to the list
  * @param entity: the entity to add
-*/
-template<class T>
-inline void tList<T>::add(T* entity)
+ */
+template<class T>
+    inline void tList<T>::add(T* entity)
 {
   if( unlikely(entity == NULL)) return;
@@ -127,5 +128,5 @@
   this->first = el;
 
-  if( unlikely(el->next == NULL)) this->first = el; /* if first element */
+  if( unlikely(el->next == NULL)) this->last = el; /* if first element */
   else el->next->prev = el;
   this->size++;
@@ -136,26 +137,29 @@
  *  remove an entity from the list
  * @param entity: the entity to be removed
-*/
-template<class T>
-inline void tList<T>::remove(const T* entity)
+ */
+template<class T>
+    inline void tList<T>::remove(const T* entity)
 {
   this->currentEl = this->first;
   while( this->currentEl != NULL)
+  {
+    if( unlikely(this->currentEl->curr == entity))
     {
-      if( unlikely(this->currentEl->curr == entity))
-        {
-          if( unlikely(this->currentEl->prev  == NULL)) this->first = this->currentEl->next;
-          else this->currentEl->prev->next = this->currentEl->next;
-
-          if( unlikely(this->currentEl->next == NULL)) this->last = this->currentEl->prev;
-          else this->currentEl->next->prev = this->currentEl->prev;
-
-          delete this->currentEl;
-          this->size--;
-          return;
-        }
-      this->currentEl = this->currentEl->next;
+      // erstes element?
+      if( unlikely(this->currentEl->prev == NULL)) this->first = this->currentEl->next;
+      else this->currentEl->prev->next = this->currentEl->next;
+
+      // letztes element?
+      if( unlikely(this->currentEl->next == NULL)) this->last = this->currentEl->prev;
+      else this->currentEl->next->prev = this->currentEl->prev;
+
+      delete this->currentEl;
+      this->size--;
+      return;
     }
-}
+    this->currentEl = this->currentEl->next;
+  }
+}
+
 
 /**
@@ -165,7 +169,9 @@
      inline void tList<T>::removeLast()
 {
-  if (this->last == NULL)
+  if( unlikely(this->last == NULL))
     return;
-  else if (this->last == this->first)
+
+  // only one element in the list (and its not NULL :D )
+  if( unlikely(this->last == this->first))
   {
     delete this->first;
@@ -183,18 +189,21 @@
 }
 
-/**
- *  this will deletes the objects from the list
-*/
-template<class T>
-inline void tList<T>::flush()
+
+/**
+ *  this will delete all objects from the list, this can be very dangerous if there are ghost objects in the list.
+ */
+template<class T>
+    inline void tList<T>::flush()
 {
   this->currentEl = this->first;
-  while(this->currentEl != NULL)
-    {
-      listElement<T>* le = this->currentEl->next;
-      delete this->currentEl->curr;
-      delete this->currentEl;
-      this->currentEl = le;
-    }
+
+  listElement<T>* le;
+  while( this->currentEl != NULL)
+  {
+    le = this->currentEl->next;
+    delete this->currentEl->curr;
+    delete this->currentEl;
+    this->currentEl = le;
+  }
   this->first = NULL;
   this->last = NULL;
@@ -206,7 +215,7 @@
  *  returns the first element of the list
  * @returns first element
-*/
-template<class T>
-inline T* tList<T>::firstElement() const
+ */
+template<class T>
+    inline T* tList<T>::firstElement() const
 {
   return this->first->curr;
@@ -217,7 +226,7 @@
  *  function returns the last element of the list
  * @returns the last element
-*/
-template<class T>
-inline T* tList<T>::lastElement() const
+ */
+template<class T>
+    inline T* tList<T>::lastElement() const
 {
   return this->last->curr;
@@ -228,41 +237,41 @@
  *  returns true if the list is empty
  * @returns true if the list is empty
-*/
-template<class T>
-inline bool tList<T>::isEmpty() const
-{
-  return (this->size==0)?true:false;
-}
-
-/**
- *  checks if an entity is in the List
+ */
+template<class T>
+    inline bool tList<T>::isEmpty() const
+{
+  return (this->size == 0)?true:false;
+}
+
+
+/**
+ *  checks if an entity is in the List.
  * @param entity The entity to check for in the entire List.
  * @returns true if it is, false otherwise
-*/
-template<class T>
-inline bool tList<T>::inList(T* entity)
+ */
+template<class T>
+    inline bool tList<T>::inList(T* entity) const
 {
   // pre checks
-  if(this->size == 0) return false;
-  if( entity == NULL) return false;
+  if( unlikely(entity == NULL)) return false;
 
   // search in the List
-  this->currentEl = this->first;
-  while(this->currentEl->curr != entity && this->currentEl != NULL)
-    this->currentEl = this->currentEl->next;
-
-  // post checks
-  if(this->currentEl == NULL)
-    return false;
-  else
-    return true;
-}
+  listElement<T>* el = this->first;
+  while( this->currentEl != NULL)
+  {
+    if( this->currentEl->curr == entity)
+      return true;
+
+    el = this->currentEl->next;
+  }
+}
+
 
 /**
  *  this returns the number of elements in the list
  * @returns number of elements
-*/
-template<class T>
-inline unsigned int tList<T>::getSize() const
+ */
+template<class T>
+    inline unsigned int tList<T>::getSize() const
 {
   return this->size;
@@ -275,7 +284,7 @@
 
    You will use this, if you want to iterate through the list
-*/
-template<class T>
-inline tIterator<T>* tList<T>::getIterator() const
+ */
+template<class T>
+    inline tIterator<T>* tList<T>::getIterator() const
 {
   return new tIterator<T>(this);
@@ -288,7 +297,7 @@
  * @param toEntity: the entity after which is an entity, that has to be returned, sorry, terrible phrase
  * @returns the element after toEntity
-*/
-template<class T>
-inline T* tList<T>::nextElement(T* toEntity)
+ */
+template<class T>
+    inline T* tList<T>::nextElement(T* toEntity)
 {
   //if( this->last == this->first == NULL) return NULL;
@@ -298,7 +307,7 @@
   this->currentEl = this->first;
   while(this->currentEl->curr != toEntity && this->currentEl->next != NULL)
-    {
-      this->currentEl = this->currentEl->next;
-    }
+  {
+    this->currentEl = this->currentEl->next;
+  }
   if(this->currentEl == NULL) return NULL;
   return this->currentEl->next->curr;
@@ -311,7 +320,7 @@
 
    ATTENTION: function is not implemented and wont do anything
-*/
-template<class T>
-T* tList<T>::toArray()
+ */
+template<class T>
+    T* tList<T>::toArray()
 {
   return NULL;
@@ -322,5 +331,5 @@
 
 /**
- *  an iterator class
+    *  an iterator class
 
    this enables the user to iterate through a list very easely
@@ -336,5 +345,5 @@
     T* nextElement();
     T* prevElement();
-    T* seekElement(T* element);
+    T* seekElement(const T* element);
     T* iteratorElement(const tIterator<T>* iterator);
 
@@ -376,12 +385,15 @@
 template<class T>
     inline tIterator<T>::~tIterator ()
-{
-}
-
+{}
+
+/**
+ * this returns the first element of the iterator list
+ * @returns first element
+ */
 template<class T>
     inline T* tIterator<T>::firstElement ()
 {
   this->tmpEl = this->list->first;
-  if (this->tmpEl != NULL)
+  if( likely(this->tmpEl != NULL))
   {
     this->currentEl = this->tmpEl->next;
@@ -392,9 +404,13 @@
 }
 
+/**
+ * this returns the last element of the iterator list
+ * @returns last element
+ */
 template<class T>
     inline T* tIterator<T>::lastElement ()
 {
   this->tmpEl = this->list->last;
-  if (this->tmpEl != NULL)
+  if( likely(this->tmpEl != NULL))
   {
     this->currentEl = tmpEl->prev;
@@ -413,5 +429,5 @@
     inline T* tIterator<T>::nextElement ()
 {
-  if( this->currentEl == NULL)
+  if( unlikely(this->currentEl == NULL))
     return NULL;
 
@@ -422,4 +438,5 @@
 }
 
+
 /**
  *  use it to iterate backwards through the list
@@ -429,5 +446,5 @@
     inline T* tIterator<T>::prevElement ()
 {
-  if( this->currentEl == NULL)
+  if( unlikely(this->currentEl == NULL))
     return NULL;
 
@@ -445,7 +462,7 @@
  */
 template<class T>
-    inline T* tIterator<T>::seekElement (T* element)
-{
-  if (element == NULL)
+    inline T* tIterator<T>::seekElement (const T* element)
+{
+  if( unlikely(element == NULL))
   {
     this->currentEl = NULL;
@@ -476,5 +493,5 @@
     T* tIterator<T>::iteratorElement(const tIterator<T>* iterator)
 {
-  if (iterator != NULL && iterator->list == this->list)
+  if( unlikely(iterator != NULL && iterator->list == this->list))
   {
     this->currentEl = iterator->currentEl;
