00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
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) { }
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) { }
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 }
00334
00335 # endif
00336