00001 /* 00002 * $Revision: 2299 $ 00003 * 00004 * last checkin: 00005 * $Author: gutwenger $ 00006 * $Date: 2012-05-07 15:57:08 +0200 (Mon, 07 May 2012) $ 00007 ***************************************************************/ 00008 00044 #ifdef _MSC_VER 00045 #pragma once 00046 #endif 00047 00048 #ifndef OGDF_STACK_H 00049 #define OGDF_STACK_H 00050 00051 00052 #include <ogdf/basic/SList.h> 00053 00054 00055 namespace ogdf { 00056 00057 00060 // * In contrast to Stack<E>, instances of \a StackPure<E> do not store the 00061 // * number of elements contained in the stack. 00062 // */ 00063 //template<class E> class StackPure : private SListPure<E> { 00064 //public: 00065 // //! Constructs an empty stack. 00066 // StackPure() { } 00067 // 00068 // //! Constructs a stack that is a copy of \a S. 00069 // StackPure(const StackPure<E> &S) : SListPure<E>(S) { } 00070 // 00071 // // destruction 00072 // ~StackPure() { } 00073 // 00074 // //! Returns true iff the stack is empty. 00075 // bool empty() const { return SListPure<E>::empty(); } 00076 // 00077 // //! Returns a reference to the top element. 00078 // const E &top() const { 00079 // return SListPure<E>::front(); 00080 // } 00081 // 00082 // //! Returns a reference to the top element. 00083 // E &top() { 00084 // return SListPure<E>::front(); 00085 // } 00086 // 00087 // //! Assignment operator. 00088 // StackPure<E> &operator=(const StackPure<E> &S) { 00089 // SListPure<E>::operator=(S); 00090 // return *this; 00091 // } 00092 // 00093 // //! Adds element \a x as top-most element to the stack. 00094 // SListIterator<E> push(const E &x) { 00095 // return SListPure<E>::pushFront(x); 00096 // } 00097 // 00098 // //! Removes the top-most element from the stack and returns it. 00099 // E pop() { 00100 // E x = top(); 00101 // SListPure<E>::popFront(); 00102 // return x; 00103 // } 00104 // 00105 // //! Makes the stack empty. 00106 // void clear() { SListPure<E>::clear(); } 00107 // 00108 // const SListPure<E> &getListPure() const { return *this; } 00109 // 00110 // OGDF_NEW_DELETE 00111 //}; // class StackPure 00112 // 00113 // 00116 // * In contrast to StackPure<E>, instances of \a Stack<E> store the 00117 // * number of elements contained in the stack. 00118 // */ 00119 //template<class E> class Stack : private SList<E> { 00120 //public: 00121 // //! Constructs an empty stack. 00122 // Stack() { } 00123 // 00124 // //! Constructs a stack that is a copy of \a S. 00125 // Stack(const Stack<E> &S) : SList<E>(S) { } 00126 // 00127 // // destruction 00128 // ~Stack() { } 00129 // 00130 // //! Returns true iff the stack is empty. 00131 // bool empty() const { return SList<E>::empty(); } 00132 // 00133 // //! Returns the number of elements contained in the stack. 00134 // int size() const { return SList<E>::size(); } 00135 // 00136 // //! Returns a reference to the top element. 00137 // const E &top() const { 00138 // return SList<E>::front(); 00139 // } 00140 // 00141 // //! Returns a reference to the top element. 00142 // E &top() { 00143 // return SList<E>::front(); 00144 // } 00145 // 00146 // //! Assignment operator. 00147 // Stack<E> &operator=(const Stack<E> &S) { 00148 // SList<E>::operator=(S); 00149 // return *this; 00150 // } 00151 // 00152 // //! Adds element \a x as top-most element to the stack. 00153 // SListIterator<E> push(const E &x) { 00154 // return SList<E>::pushFront(x); 00155 // } 00156 // 00157 // //! Removes the top-most element from the stack and returns it. 00158 // E pop() { 00159 // E x = top(); 00160 // SList<E>::popFront(); 00161 // return x; 00162 // } 00163 // 00164 // //! Makes the stack empty. 00165 // void clear() { SList<E>::clear(); } 00166 // 00167 // const SList<E> &getList() const { return *this; } 00168 // 00169 // OGDF_NEW_DELETE 00170 //}; // class Stack 00171 // 00172 // 00174 //template<class E> 00175 //void print(ostream &os, const StackPure<E> &S, char delim = ' ') 00176 //{ print(os,S.getListPure(),delim); } 00177 // 00178 //template<class E> 00179 //void print(ostream &os, const Stack<E> &S, char delim = ' ') 00180 //{ print(os,S.getListPure(),delim); } 00181 // 00182 // 00184 //template<class E> 00185 //ostream &operator<<(ostream &os, const StackPure<E> &S) 00186 //{ 00187 // print(os,S); return os; 00188 //} 00189 // 00190 //template<class E> 00191 //ostream &operator<<(ostream &os, const Stack<E> &S) 00192 //{ 00193 // print(os,S); return os; 00194 //} 00195 // 00196 00197 00199 00203 template<class E> class StackPure 00204 { 00205 struct Element { 00206 Element(const E &x, Element *pNext) : m_next(pNext), m_x(x) { } 00207 Element *m_next; 00208 E m_x; 00209 OGDF_NEW_DELETE 00210 }; 00211 00212 Element *m_head; 00213 00214 public: 00216 StackPure() { m_head = 0; } 00217 00219 StackPure(const StackPure<E> &S) { 00220 m_head = 0; 00221 copy(S); 00222 } 00223 00224 // destruction 00225 ~StackPure() { 00226 clear(); 00227 } 00228 00230 bool empty() const { return m_head == 0; } 00231 00233 const E &top() const { 00234 return m_head->m_x; 00235 } 00236 00238 E &top() { 00239 return m_head->m_x; 00240 } 00241 00243 StackPure<E> &operator=(const StackPure<E> &S) { 00244 clear(); 00245 copy(S); 00246 return *this; 00247 } 00248 00250 void push(const E &x) { 00251 m_head = OGDF_NEW Element(x,m_head); 00252 } 00253 00255 E pop() { 00256 OGDF_ASSERT(m_head != 0) 00257 Element *pX = m_head; 00258 m_head = m_head->m_next; 00259 E x = pX->m_x; 00260 delete pX; 00261 return x; 00262 } 00263 00265 void clear() { 00266 while(m_head) { 00267 Element *pX = m_head; 00268 m_head = m_head->m_next; 00269 delete pX; 00270 } 00271 } 00272 00273 void print(ostream &os, char delim = ' ') const 00274 { 00275 Element *pX = m_head; 00276 if (pX != 0) { 00277 os << pX->m_x; 00278 for(pX = pX->m_next; pX != 0; pX = pX->m_next) 00279 os << delim << pX->m_x; 00280 } 00281 } 00282 00283 private: 00284 void copy(const StackPure<E> &S) { 00285 Element **p = &m_head; 00286 00287 for(Element *q = S.m_head; q != 0; q = q->m_next) { 00288 *p = OGDF_NEW Element(q->m_x,0); 00289 p = &(*p)->m_next; 00290 } 00291 } 00292 00293 OGDF_NEW_DELETE 00294 }; // class StackPure 00295 00296 00298 00302 template<class E> class Stack : private StackPure<E> 00303 { 00304 int m_count; 00305 00306 public: 00308 Stack() { m_count = 0; } 00309 00311 Stack(const Stack<E> &S) : StackPure<E>(S) { m_count = S.m_count; } 00312 00313 // destruction 00314 ~Stack() { } 00315 00317 bool empty() const { return StackPure<E>::empty(); } 00318 00320 int size() const { return m_count; } 00321 00323 const E &top() const { 00324 return StackPure<E>::top(); 00325 } 00326 00328 E &top() { 00329 return StackPure<E>::top(); 00330 } 00331 00333 Stack<E> &operator=(const Stack<E> &S) { 00334 StackPure<E>::operator=(S); 00335 m_count = S.m_count; 00336 return *this; 00337 } 00338 00340 void push(const E &x) { 00341 ++m_count; 00342 return StackPure<E>::push(x); 00343 } 00344 00346 E pop() { 00347 --m_count; 00348 return StackPure<E>::pop(); 00349 } 00350 00352 void clear() { 00353 StackPure<E>::clear(); 00354 m_count = 0; 00355 } 00356 00357 void print(ostream &os, char delim = ' ') const { 00358 StackPure<E>::print(os,delim); 00359 } 00360 00361 OGDF_NEW_DELETE 00362 }; // class Stack 00363 00364 00365 00366 template<class E> 00367 ostream &operator<<(ostream &os, const StackPure<E> &S) 00368 { 00369 S.print(os); 00370 return os; 00371 } 00372 00373 00374 template<class E> 00375 ostream &operator<<(ostream &os, const Stack<E> &S) 00376 { 00377 S.print(os); 00378 return os; 00379 } 00380 00381 } // end namespace ogdf 00382 00383 00384 #endif