tpl_slist.H

00001 
00002 /*
00003   This file is part of Aleph system
00004 
00005   Copyright (c) 2002, 2003, 2004, 2005, 2006
00006   UNIVERSITY LOS ANDES (ULA) Merida - REPÚBLICA BOLIVARIANA DE VENEZUELA  
00007 
00008   - Center of Studies in Microelectronics & Distributed Systems (CEMISID) 
00009   - ULA Computer Science Department
00010   - FUNDACITE Mérida - Ministerio de Ciencia y Tecnología
00011 
00012   PERMISSION TO USE, COPY, MODIFY AND DISTRIBUTE THIS SOFTWARE AND ITS
00013   DOCUMENTATION IS HEREBY GRANTED, PROVIDED THAT BOTH THE COPYRIGHT
00014   NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES OF THE
00015   SOFTWARE, DERIVATIVE WORKS OR MODIFIED VERSIONS, AND ANY PORTIONS
00016   THEREOF, AND THAT BOTH NOTICES APPEAR IN SUPPORTING DOCUMENTATION.
00017 
00018   Aleph is distributed in the hope that it will be useful, but WITHOUT
00019   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00020   or FITNESS FOR A PARTICULAR PURPOSE.
00021 
00022   UNIVERSIDAD DE LOS ANDES requests users of this software to return to 
00023 
00024   Leandro Leon
00025   CEMISID 
00026   Ed La Hechicera 
00027   3er piso, ala sur
00028   Facultad de Ingenieria 
00029   Universidad de Los Andes 
00030   Merida - REPÚBLICA BOLIVARIANA DE VENEZUELA    or
00031 
00032   lrleon@ula.ve
00033 
00034   any improvements or extensions that they make and grant Universidad 
00035   de Los Andes (ULA) the rights to redistribute these changes. 
00036 
00037   Aleph is (or was) granted by: 
00038   - Consejo de Desarrollo Cientifico, Humanistico, Tecnico de la ULA 
00039     (CDCHT)
00040   - Fundacite Mérida
00041 */
00042 
00043 
00044 # ifndef TPL_SLIST_H
00045 # define TPL_SLIST_H
00046 
00047 # include <ahDefs.H>
00048 # include <tpl_snode.H>
00049 
00050 using namespace Aleph;
00051 
00052 namespace Aleph {
00053 
00062     template <typename T>
00063 class Slist : public Snode<T>
00064 {
00065 
00066 public:
00067 
00068   typedef Snode<T> Node;
00070   Slist() { /* empty */ }
00078   void insert_first(Node * node)
00079   {
00080 
00081     I(node not_eq NULL);
00082     I(node->is_empty());
00083 
00084     this->insert_next(node);
00085   }
00093   Node * remove_first() throw(std::exception, std::underflow_error)
00094   {
00095 
00096     if (this->is_empty())
00097       throw std::underflow_error ("list is empty");
00098 
00099     return this->remove_next();
00100   }
00102   Node * get_first() const Exception_Prototypes(std::underflow_error)
00103   {
00104 
00105     if (this->is_empty())
00106       throw std::underflow_error ("list is empty");
00107 
00108     return this->get_next(); 
00109   }
00118   class Iterator
00119   {
00120 
00121   private:
00122 
00123     Slist * list;
00124     Node  * current;
00125 
00126   public:
00127 
00134     Iterator(Slist & _list) : list(&_list), current(list->get_first())
00135     {
00136       // Empty
00137     }
00138 
00140     bool has_current() const { return current != list; }
00141 
00148     Node * get_current() throw(std::exception, std::overflow_error)
00149     {
00150 
00151       if (not this->has_current())
00152         throw std::overflow_error ("");
00153 
00154       return current;
00155     }
00156 
00167     void next() throw(std::exception, std::overflow_error)
00168     {
00169 
00170       if (not this->has_current())
00171         throw std::overflow_error ("");
00172 
00173       current = current->get_next();
00174     }
00175 
00177     void reset_first() { current = list->get_next(); }
00178     Iterator & operator = (Node * node)
00179     {
00180       if (this == node)
00181         return *this;
00182 
00183       current = node;
00184       return *this;
00185     }
00186   };
00187 };
00188 
00189 } // end namespace Aleph
00190 
00191 # endif /* TPL_SLIST_H */
00192 

Leandro R. León