tpl_listQueue.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_LISTQUEUE_H
00045 # define TPL_LISTQUEUE_H
00046 
00047 # include <tpl_snode.H>
00048 
00049 using namespace Aleph;
00050 
00051 namespace Aleph {
00052 
00064     template <typename T>
00065 class ListQueue 
00066 {
00067   
00068   public:
00069 
00071     typedef Snode<T> Node;
00072 
00073   private:
00074 
00075   Node * rear_ptr;
00076   size_t num_nodes;
00077 
00078 public:
00079 
00081   ListQueue() : rear_ptr(NULL), num_nodes(0) { /* empty */ }
00088   void put(Node * node)
00089   {
00090 
00091     I(node->is_empty());
00092 
00093     if (num_nodes > 0)
00094       rear_ptr->insert_next(node);
00095 
00096     num_nodes++;
00097     rear_ptr = node;
00098   }
00104   Node * get() throw(std::exception, std::underflow_error)
00105   {
00106     if (num_nodes == 0)
00107       throw std::underflow_error("stack is empty");
00108 
00109     Node * ret_val = rear_ptr->remove_next(); 
00110     num_nodes--;
00111 
00112     if (num_nodes == 0)
00113       rear_ptr = NULL;
00114 
00115     return ret_val;
00116   }
00122   Node * front() const throw(std::exception, std::underflow_error)
00123   {
00124     if (num_nodes == 0)
00125       throw std::underflow_error("stack is empty");
00126 
00127     return rear_ptr->get_next();
00128   }
00129 
00135   Node * rear() const throw(std::exception, std::underflow_error)
00136   {
00137     if (num_nodes == 0)
00138       throw std::underflow_error("stack is empty");
00139 
00140     return rear_ptr;
00141   }
00143   size_t size() const { return num_nodes; }
00144 
00146   bool is_empty() const { return num_nodes == 0; }
00147 };
00148 
00149 } // end namespace Aleph
00150 
00151 # endif /* TPL_LISTQUEUE_H */
00152 

Leandro R. León