tpl_dlist.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_DLIST_H
00045 # define TPL_DLIST_H
00046 
00047 # include <tpl_dnode.H>
00048 
00049 using namespace Aleph;
00050 
00051 namespace Aleph {
00052 
00059     template <typename T>
00060 class Dlist_Node : public Dnode<T>
00061 {
00062 
00063 public:
00064 
00065   Dlist_Node() : Dnode<T>() { /* Empty */ }
00066 
00067   Dlist_Node(const T & _data) : Dnode<T>(_data) { /* Empty */ }
00068 };
00069 
00076     template <typename T>
00077 class Dlist_Node_Vtl : public Dlist_Node<T>
00078 {
00079 
00080 public:
00081 
00082   Dlist_Node_Vtl() : Dlist_Node<T>() { /* Empty */ }
00083 
00084   virtual ~Dlist_Node_Vtl() { /* Empty */ }
00085 
00086   Dlist_Node_Vtl(const T & _data) : Dlist_Node<T>(_data) { /* Empty */ }
00087 };  
00088     template <template <class> class Node, typename T>
00089 class MetaDlistNode : public Node<T>
00090 {
00091 
00092 public:
00093 
00094   MetaDlistNode() { /* Empty */ }
00095 
00096   MetaDlistNode(const T& _data) : Node<T>(_data) { /* Empty */ }
00097 
00098   MetaDlistNode(const MetaDlistNode<Node, T> & node) : Node<T>(node) 
00099   { /* Empty */ }
00100 
00101   MetaDlistNode<Node, T> *& get_next() 
00102   {
00103     return reinterpret_cast<MetaDlistNode<Node, T> *&>(Node<T>::get_next()); 
00104   }
00105 
00106   MetaDlistNode<Node, T> *& get_prev() 
00107   {
00108     return reinterpret_cast<MetaDlistNode<Node, T> *&>(Node<T>::get_prev()); 
00109   }
00110 };
00125     template <template <class> class Node_Type, typename T>
00126 class GenDlist : public MetaDlistNode<Node_Type, T>
00127 {
00128 
00129 public:
00130 
00132   typedef MetaDlistNode<Node_Type,T> Node;
00140   Node * get_first() throw(std::exception, std::underflow_error)
00141   {
00142     if (this->is_empty())
00143       throw std::underflow_error ("List is empty");
00144 
00145     return this->get_next();
00146   }
00147 
00155   Node * get_last() throw(std::exception, std::underflow_error)
00156   {
00157     if (this->is_empty())
00158       throw std::underflow_error ("List is empty");
00159 
00160     return this->get_prev();
00161   }
00170   Node * remove_first() throw(std::exception, std::underflow_error)
00171   {
00172     if (this->is_empty())
00173       throw std::underflow_error ("List is empty");
00174 
00175     Node* retValue = this->get_next();
00176     retValue->del();
00177 
00178     return retValue;
00179   }
00180 
00189   Node* remove_last() throw(std::exception, std::underflow_error)
00190   {
00191     if (this->is_empty())
00192       throw std::underflow_error ("List is empty");
00193 
00194     Node* retValue = this->get_prev();
00195     retValue->del();
00196 
00197     return retValue;
00198   }
00204   class Iterator : public Node::Iterator
00205   {
00206 
00207   public:
00208 
00210     Iterator(GenDlist<Node_Type, T> & list) : Dnode<T>::Iterator(&list) 
00211     {
00212       // Empty
00213     }
00214 
00216     Iterator(GenDlist<Node_Type, T> & list, GenDlist<Node_Type, T> * curr) 
00217       : Dnode<T>::Iterator(&list, curr) 
00218     {
00219       // Empty
00220     }
00221 
00222 
00224     Iterator() : Dnode<T>::Iterator() { /* empty */ }
00225 
00227     Iterator(const Iterator & it) : Dnode<T>::Iterator(it) { /* Empty */ }
00228 
00230     Iterator & operator = (const Iterator & it) 
00231     {
00232       if (this == &it)
00233         return *this;
00234 
00235       Dnode<T>::Iterator::operator = (it);
00236 
00237       return *this;
00238     }
00239 
00240     Node * get_current() 
00241     { 
00242       return static_cast<Node*>(Dnode<T>::Iterator::get_current()); 
00243     }
00244 
00246     Node * del() { return static_cast<Node*>(Dnode<T>::Iterator::del()); }
00247   };
00248 };
00254     template <typename T> 
00255 class Dlist : public GenDlist<Dlist_Node, T> 
00256 {
00257 
00258 public:
00259 
00261   typedef typename GenDlist<Dlist_Node, T>::Node Node;
00262 
00264   typedef typename GenDlist<Dlist_Node, T>::Iterator Iterator;
00265 };
00266 
00272     template <typename T> 
00273 class DlistVtl : public GenDlist<Dlist_Node_Vtl, T> 
00274 {
00275 
00276 public:
00277 
00279   typedef typename GenDlist<Dlist_Node_Vtl, T>::Node Node;
00280 
00282   typedef typename GenDlist<Dlist_Node_Vtl, T>::Iterator Iterator ;
00283 };
00284 
00285 } // end namespace Aleph
00286 
00287 # endif /* TPL_DLIST_H */
00288 

Leandro R. León