Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

list.h

Go to the documentation of this file.
00001 /***************************************************************************
00002     list.h  -  Definition of List template class.
00003                              -------------------
00004     begin                : June 21 2002
00005     copyright            : (C) 2003 by Vojtìch Toman
00006     email                : vtoman@lit.cz
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00025 #ifndef LIST_H
00026 #define LIST_H
00027 
00028 #include "defs.h"
00029 #include "collection.h"
00030 #include "debug.h"
00031 
00032 
00033 
00037 template<class T_> struct ListLinkedList
00038 {
00040   struct ListLinkedList<T_> *prev_;
00041   
00043   struct ListLinkedList<T_> *next_;
00044   
00046   T_ *data_;
00047 
00048 }; //SLListLinkedList
00049 
00050 
00051 
00055 template<class T_> class List : public Collection<T_>
00056 {
00057 public:
00063   List(void) : Collection<T_>() { initList(); }
00064 
00070   virtual ~List(void) { clear(); }
00071 
00072 
00080   virtual T_ *first(void)
00081   { 
00082     if (!firstNode_)
00083       return 0;
00084     else
00085       {
00086         currentNode_ = firstNode_;
00087         return firstNode_->data_;
00088       }
00089   }
00090 
00091 
00099   virtual T_ *last(void)
00100   { 
00101     if (!lastNode_)
00102       return 0;
00103     else
00104       {
00105         currentNode_ = lastNode_;
00106         return lastNode_->data_;
00107       }
00108   }
00109 
00110 
00118   virtual T_ *current(void)
00119   { 
00120     if (!currentNode_)
00121       return 0;
00122     else
00123       {
00124         return currentNode_->data_;
00125       }
00126   }
00127 
00128 
00138   virtual T_ *next(void)
00139   {
00140     if (!currentNode_)
00141       return 0;
00142     else
00143       if (!currentNode_->next_)
00144         {
00145           currentNode_ = 0;
00146           return 0;
00147         }
00148       else
00149         {
00150           currentNode_ = currentNode_->next_;
00151           return currentNode_->data_;
00152         }
00153   }
00154 
00155 
00165   virtual T_ *prev(void)
00166   {
00167     if (!currentNode_)
00168       return 0;
00169     else
00170       if (!currentNode_->prev_)
00171         {
00172           currentNode_ = 0;
00173           return 0;
00174         }
00175       else
00176         {
00177           currentNode_ = currentNode_->prev_;
00178           return currentNode_->data_;
00179         }
00180   }
00181 
00182 
00192   virtual void insert(T_ *item)
00193   {
00194     ListLinkedList<T_> *pom;
00195 
00196     NEW(pom, ListLinkedList<T_>);
00197 
00198     pom->data_ = item;
00199 
00200 
00201     if (!firstNode_)
00202       {
00203         pom->prev_ = 0;
00204         pom->next_ = 0;
00205         firstNode_ = pom;
00206         lastNode_ = pom;
00207       }
00208     else
00209       {
00210         if (!currentNode_)
00211           {
00212             pom->prev_ = 0;
00213             pom->next_ = firstNode_;
00214             firstNode_->prev_ = pom;
00215           }
00216         else
00217           {
00218             pom->prev_ = currentNode_;
00219             pom->next_ = currentNode_->next_;
00220             currentNode_->next_ = pom;
00221             
00222             if (!pom->next_)
00223               lastNode_ = pom;
00224             else
00225               pom->next_->prev_ = pom;
00226           }
00227       }
00228 
00229     currentNode_ = pom;
00230 
00231     cnt_++;
00232   }
00233 
00234 
00244   virtual void append(T_ *item)
00245   {
00246     ListLinkedList<T_> *pom;
00247 
00248     NEW(pom, ListLinkedList<T_>);
00249 
00250     pom->next_ = 0;
00251     pom->prev_ = lastNode_;
00252     pom->data_ = item;
00253 
00254     if (!firstNode_)
00255       firstNode_ = pom;
00256     else
00257       lastNode_->next_ = pom;
00258 
00259     lastNode_ = pom;
00260     currentNode_ = pom;
00261 
00262     cnt_++;
00263   }
00264 
00265 
00275   virtual void prepend(T_ *item)
00276   {
00277     ListLinkedList<T_> *pom;
00278 
00279     NEW(pom, ListLinkedList<T_>);
00280 
00281     pom->prev_ = 0;
00282     pom->next_ = firstNode_;
00283     pom->data_ = item;
00284 
00285 
00286     !lastNode_ ? lastNode_ = pom : firstNode_->prev_ = pom;
00287 
00288     firstNode_ = pom;
00289 
00290     currentNode_ = pom;
00291 
00292     cnt_++;
00293   }
00294 
00295 
00301   virtual T_ *getFirst(void)
00302   {
00303     if (!firstNode_)
00304       return 0;
00305     else
00306       {
00307         ListLinkedList<T_> *pom;
00308         T_ *data;
00309         
00310         pom = firstNode_;
00311         data = firstNode_->data_;
00312 
00313 
00314         firstNode_ = firstNode_->next_;
00315         currentNode_ = firstNode_;
00316 
00317 
00318         if (firstNode_)
00319           {
00320             firstNode_->prev_ = 0;
00321           }
00322 
00323         cnt_--;
00324 
00325         DELETE(pom);
00326         return data;
00327       }
00328   }
00329 
00330 
00341   virtual bool remove(void)
00342   {
00343     if (!currentNode_)
00344       return false;
00345     else
00346       {
00347         ListLinkedList<T_> *pom1, *pom2;
00348         
00349         pom1 = currentNode_->prev_;
00350         pom2 = currentNode_->next_;
00351 
00352 
00353         if (pom1)
00354           pom1->next_ = pom2;
00355         else
00356           firstNode_ = pom2;
00357 
00358         if (pom2)
00359           pom2->prev_ = pom1;
00360         else
00361           lastNode_ = pom1;
00362 
00363         if (autoDelete_)
00364           {
00365             DELETE(currentNode_->data_);
00366           }
00367 
00368 
00369         DELETE(currentNode_);
00370         cnt_--;
00371 
00372         currentNode_ = pom2;
00373 
00374 
00375         return true;
00376       }
00377   }
00378 
00379 
00380 
00391   virtual bool remove(T_ *item)
00392   {
00393     ListLinkedList<T_> *pom;
00394 
00395     pom = firstNode_;
00396 
00397     while (pom)
00398       {
00399         if (pom->data_ == item)
00400           {
00401             currentNode_ = pom;
00402             return remove();
00403           }
00404         else
00405           pom = pom->next_;
00406       }
00407     
00408     return false;
00409   }
00410 
00411 
00412 
00421   virtual bool contains(T_ *item)
00422   {
00423     ListLinkedList<T_> *pom;
00424 
00425     pom = firstNode_;
00426 
00427     while (pom)
00428       {
00429         if (pom->data_ == item)
00430           return true;
00431         else
00432           pom = pom->next_;
00433       }
00434     
00435     return false;
00436   }
00437 
00438 
00446   virtual void clear(void)
00447   {
00448     ListLinkedList<T_> *pom;
00449 
00450     while (firstNode_)
00451       {
00452         pom = firstNode_;
00453         firstNode_ = firstNode_->next_;
00454 
00455         if (autoDelete_)
00456           DELETE(pom->data_);
00457 
00458         DELETE(pom);
00459       }
00460 
00461     firstNode_ = 0;
00462     lastNode_ = 0;
00463     currentNode_ = 0;
00464     cnt_ = 0;
00465   }
00466 
00467 
00468 protected:
00470   struct ListLinkedList<T_> *firstNode_;
00471 
00473   struct ListLinkedList<T_> *lastNode_;
00474 
00476   struct ListLinkedList<T_> *currentNode_;
00477 
00478 
00480   virtual void initList(void)
00481   {
00482     firstNode_ = 0;
00483     lastNode_ = 0;
00484     currentNode_ = 0;
00485   }
00486 
00487 }; //List
00488 
00489 
00490 #endif //LIST_H

Generated on Wed Feb 5 10:43:02 2003 for Exalt by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002