tpl_arrayStack.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_ARRAYSTACK_H
00045 # define TPL_ARRAYSTACK_H
00046 
00047 # include <aleph.H>
00048 
00049 using namespace Aleph;
00050 
00051 namespace Aleph {
00052 
00079   template <typename T, const size_t dim = 100>
00080 class ArrayStack
00081 {
00082   T      array[dim]; 
00083   size_t head;
00084 
00085 public:
00086 
00088   ArrayStack() : head(0) { /* empty */ }
00098   T & push(const T & data) throw(std::exception, std::overflow_error)
00099   {
00100 
00101     if (head >= dim) 
00102       throw std::overflow_error ("array stack overflow");
00103 
00104     array[head++] = data;
00105     return array[head - 1];
00106   }
00119   T & pushn(const size_t & n = 1) throw(std::exception, std::overflow_error)
00120   {
00121 
00122     if (head + n > dim)
00123       throw std::overflow_error ("array stack overflow");
00124 
00125     head += n;
00126     return array[head - 1];
00127   }
00135   T pop() throw(std::exception, std::underflow_error)
00136   {
00137     if (head == 0) 
00138       throw std::underflow_error ("array stack underflow");
00139     return array[--head];
00140   }
00151   T popn(const int & n) throw(std::exception, std::underflow_error)
00152   {
00153 
00154     if (head < n)
00155       throw std::underflow_error ("array stack underflow");
00156 
00157     head -= n;
00158     return array[head];
00159   }
00161   T & top() throw(std::exception,std::underflow_error)
00162   {
00163     if (head == 0) 
00164       throw std::underflow_error ("array stack underflow");
00165 
00166     return array[head - 1]; 
00167   }
00180   T & top(const int & i) throw(std::exception,std::underflow_error)
00181   {
00182 
00183     if (i > head) 
00184       throw std::underflow_error ("array stack underflow");
00185 
00186     return array[head - i - 1];   
00187   }
00189   void empty() { head = 0; }
00191   bool is_empty() const { return head == 0; }
00193   const size_t & size() const { return head; }
00194 };
00195 
00221     template <typename T, const size_t dim = 100>
00222 class FixedStack
00223 {
00224   T      array[dim]; 
00225   size_t head;
00226 
00227 public:
00228 
00230   FixedStack() : head(0) { /* empty */ }
00231 
00240   T & push(const T & data) throw(std::exception, std::overflow_error)
00241   {
00242     I(head < dim );
00243 
00244     array[head++] = data;
00245     return array[head - 1];
00246   }
00247 
00259   T & pushn(const size_t & n = 1) throw(std::exception, std::overflow_error)
00260   {
00261     I(head + n <= dim);
00262 
00263     head += n;
00264     return array[head - 1];
00265   }
00266 
00273   T pop() throw(std::exception, std::underflow_error)
00274   {
00275     I(head > 0);
00276 
00277     return array[--head];
00278   }
00279 
00289   T popn(const int & n) throw(std::exception, std::underflow_error)
00290   {
00291     I((int (head - n)) >= 0);
00292 
00293     head -= n;
00294     return array[head];
00295   }
00296 
00298   T & top() throw(std::exception,std::underflow_error)
00299   {
00300     I(head > 0);
00301 
00302     return array[head - 1]; 
00303   }
00304 
00315   T & top(const int & i) throw(std::exception,std::underflow_error)
00316 
00317   {
00318     I(i < head);
00319 
00320     return array[head - i - 1];   
00321   }
00322 
00324   bool is_empty() const { return head == 0; }
00325 
00327   void empty() { head = 0; }
00328 
00330   const size_t & size() const { return head; }  
00331 };
00332 
00333 } // end namespace Aleph
00334 
00335 # endif /* TPL_ARRAYSTACK_H */
00336 

Leandro R. León