stack.hh
Go to the documentation of this file.00001 #ifndef DUNE_STACK_HH 00002 #define DUNE_STACK_HH 00003 00004 #include <dune/common/dlist.hh> 00005 #include <dune/common/exceptions.hh> 00006 00007 namespace Dune { 00008 00020 00021 class StackException : public RangeError {}; 00022 00031 template<class T> 00032 class Stack : private DoubleLinkedList<T> { 00033 public : 00034 00036 bool empty () const; 00037 00039 bool full () const; 00040 00042 void push (T& t); 00043 00045 T pop (); 00046 00048 T top () const; 00049 00051 int size() const; 00052 } ; 00053 00054 template<class T> 00055 inline int Stack<T>::size () const 00056 { 00057 return DoubleLinkedList<T>::size(); 00058 } 00059 00060 template<class T> 00061 inline bool Stack<T>::empty () const 00062 { 00063 return size()==0; 00064 } 00065 00066 template<class T> 00067 inline bool Stack<T>::full () const 00068 { 00069 return false; 00070 } 00071 00072 template<class T> 00073 inline void Stack<T>::push (T& t) 00074 { 00075 insert_after(this->rbegin(),t); 00076 } 00077 00078 template<class T> 00079 inline T Stack<T>::pop () 00080 { 00081 if (empty()) 00082 DUNE_THROW(StackException, "cannot pop() empty stack!"); 00083 00084 typename DoubleLinkedList<T>::Iterator i=this->rbegin(); 00085 T t = *i; 00086 erase(i); 00087 return t; 00088 } 00089 00090 template<class T> 00091 inline T Stack<T>::top () const 00092 { 00093 if (empty()) 00094 DUNE_THROW(StackException, "no top() in empty stack!"); 00095 00096 typename DoubleLinkedList<T>::Iterator i=this->rbegin(); 00097 T t = *i; 00098 return t; 00099 } 00100 00101 00110 template<class T, int n> 00111 class FiniteStack { 00112 public : 00113 00115 bool empty () const 00116 { 00117 return f==0; 00118 } 00119 00121 bool full () const 00122 { 00123 return f>=n; 00124 } 00125 00127 void push (const T& t) 00128 { 00129 s[f++] = t; 00130 } 00131 00133 T pop () 00134 { 00135 return s[--f]; 00136 } 00137 00139 T top () const 00140 { 00141 return s[f-1]; 00142 } 00143 00145 int size () const 00146 { 00147 return f; 00148 } 00149 00151 FiniteStack () 00152 { 00153 f = 0; 00154 } 00155 00156 private: 00157 T s[n]; 00158 int f; 00159 }; 00160 00161 } 00162 00164 00165 #endif