Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5115 in orxonox.OLD for trunk/src/lib/util/list.h


Ignore:
Timestamp:
Aug 24, 2005, 1:30:13 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: reimplemented the list functions, as i did before in revision 5110.
This time, i looked out for the bugs, and i think i found one

@patrick: i know, that you do not want to code at the moment… :/ → see mail

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/util/list.h

    r5113 r5115  
    1212#endif
    1313
    14 
     14template<class T> class tIterator;
    1515
    1616//! a list element of the tList,
     
    2323
    2424/**
    25  *  an iterator class
    26 
    27    this enables the user to iterate through a list very easely
    28 */
    29 template<class T> class tIterator
    30 {
    31  public:
    32   tIterator(listElement<T>* startElement);
    33   ~tIterator();
    34 
    35   T* nextElement();
    36   T* seekElement(T* element);
    37 
    38  private:
    39   listElement<T>*    currentEl;                      //!< pointer to the current list element in the iterator
    40   listElement<T>*    tmpEl;                          //!< temp listElemnt pointer
    41   listElement<T>*    startElement;                   //!< pointer to the start of the list
    42 };
    43 
    44 
    45 /**
    46  *  iterator constructor
    47  * @param startElement:  the first list element from the tList
    48 
    49    normaly you will use it like this:
    50 
    51    tIterator<char>* nameIterator = nameList->getIterator();
    52    char name* = nameIterator->nextElement();
    53    while( name != NULL)
    54    {
    55      PRINTF(3)("found name: %s in list\n", name);
    56      name = nameIterator->nextElement();
    57    }
    58    delete nameIterator;
    59 */
    60 template<class T>
    61 inline tIterator<T>::tIterator (listElement<T>* startElement)
    62 {
    63   this->currentEl = startElement;
    64   this->tmpEl = NULL;
    65   this->startElement = startElement;
    66 }
    67 
    68 
    69 /**
    70  *  the destructor
    71 */
    72 template<class T>
    73 inline tIterator<T>::~tIterator ()
    74 {
    75   this->currentEl = NULL;
    76 }
    77 
    78 
    79 /**
    80  *  use it to iterate through the list
    81  * @returns next list element
    82 */
    83 template<class T>
    84 inline T* tIterator<T>::nextElement ()
    85 {
    86   if( this->currentEl == NULL)
    87     return NULL;
    88 
    89   this->tmpEl = this->currentEl;
    90   this->currentEl = this->currentEl->next;
    91   return this->tmpEl->curr;
    92 }
    93 
    94 /**
    95  *  gets the element after the selected one, sets the iterator to this point in the list
    96  * @param element the element to seek
    97  * @returns next list element
    98 
    99   Attention: if you seek an element, the iterator pointer will point to the NEXT listelement after the argument!
    100  */
    101 template<class T>
    102 inline T* tIterator<T>::seekElement (T* element)
    103 {
    104   for(this->tmpEl = this->startElement; this->tmpEl != NULL; this->tmpEl = this->tmpEl->next)
    105   {
    106     if( unlikely(this->tmpEl->curr == element))
    107     {
    108       if( this->tmpEl->next != NULL)
    109       {
    110         this->currentEl = this->tmpEl->next->next;
    111         return this->tmpEl->next->curr;
    112       }
    113       return NULL;
    114     }
    115   }
    116   return NULL;
    117 }
    118 
    119 
    120 
    121 /**
    12225 *  the list template class
    12326
     
    12629template<class T> class tList
    12730{
     31  friend class tIterator<T>;
     32
    12833 public:
    12934  tList ();
     
    374279inline tIterator<T>* tList<T>::getIterator() const
    375280{
    376   tIterator<T>* iterator = new tIterator<T>(this->first);
    377   return iterator;
     281  return new tIterator<T>(this);
    378282}
    379283
     
    414318}
    415319
     320
     321
     322
     323/**
     324 *  an iterator class
     325
     326   this enables the user to iterate through a list very easely
     327 */
     328template<class T> class tIterator
     329{
     330  public:
     331    tIterator(const tList<T>* list);
     332    ~tIterator();
     333
     334    T* firstElement();
     335    T* nextElement();
     336    T* seekElement(T* element);
     337    T* iteratorElement(const tIterator<T>* iterator);
     338
     339  private:
     340    listElement<T>*    currentEl;                      //!< pointer to the current list element in the iterator
     341    listElement<T>*    tmpEl;                          //!< temp listElemnt pointer
     342    const tList<T>*    list;                           //!< The List, that we want to iterate through
     343};
     344
     345
     346/**
     347 *  iterator constructor
     348 * @param startElement:  the first list element from the tList
     349 *
     350 * normaly you will use it like this:
     351 **********************************************************
     352 * tIterator<char>* nameIterator = nameList->getIterator();
     353 * char name* = nameIterator->firstElement();
     354 * while( name != NULL)
     355 *  {
     356 *   PRINTF(3)("found name: %s in list\n", name);
     357 *   name = nameIterator->nextElement();
     358 *  }
     359 * delete nameIterator;
     360 * ********************************************************
     361 */
     362template<class T>
     363  inline tIterator<T>::tIterator (const tList<T>* list)
     364{
     365  this->currentEl = list->first;
     366  this->tmpEl = NULL;
     367  this->list = list;
     368}
     369
     370
     371/**
     372 *  the destructor
     373 */
     374template<class T>
     375    inline tIterator<T>::~tIterator ()
     376{
     377  this->currentEl = NULL;
     378}
     379
     380template<class T>
     381    inline T* tIterator<T>::firstElement ()
     382{
     383  this->currentEl = this->list->first;
     384  if (this->currentEl == NULL)
     385    return NULL;
     386  else
     387    return this->currentEl->curr;
     388}
     389
     390
     391/**
     392 *  use it to iterate through the list
     393 * @returns next list element
     394 */
     395template<class T>
     396    inline T* tIterator<T>::nextElement ()
     397{
     398  if( this->currentEl == NULL)
     399    return NULL;
     400
     401  this->currentEl = this->currentEl->next;
     402  if (this->currentEl != NULL)
     403    return this->currentEl->curr;
     404  else
     405    return NULL;
     406}
     407
     408/**
     409 *  gets the element after the selected one, sets the iterator to this point in the list
     410 * @param element the element to seek
     411 * @returns current list element
     412 */
     413template<class T>
     414    inline T* tIterator<T>::seekElement (T* element)
     415{
     416  if (element == NULL)
     417  {
     418    this->currentEl = NULL;
     419    return NULL;
     420  }
     421  this->tmpEl = this->list->first;
     422  while( this->tmpEl != NULL)
     423  {
     424    if( unlikely(this->tmpEl->curr == element))
     425    {
     426      this->currentEl = this->tmpEl;
     427      return this->currentEl->curr;
     428    }
     429    this->tmpEl = this->tmpEl->next;
     430  }
     431  this->currentEl = NULL;
     432  return NULL;
     433}
     434
     435/**
     436 * grabs the iterator entity from another iterator
     437 * @param iterator the iterator to grab the local currentEl from
     438 * @returns the grabbed element (current).
     439 */
     440template<class T>
     441    T* tIterator<T>::iteratorElement(const tIterator<T>* iterator)
     442{
     443  if (iterator != NULL)
     444  {
     445    this->currentEl = iterator->currentEl;
     446    if (this->currentEl != NULL)
     447      return this->currentEl->curr;
     448    else
     449      return NULL;
     450  }
     451  else
     452  {
     453    this->currentEl = NULL;
     454    return NULL;
     455  }
     456}
     457
     458
    416459#endif /* _LIST_H */
Note: See TracChangeset for help on using the changeset viewer.