Open
Graph Drawing
Framework

 v.2007.11
 

BoundedStack.h

Go to the documentation of this file.
00001 /*
00002  * $Revision: 1.7 $
00003  * 
00004  * last checkin:
00005  *   $Author: gutwenger $ 
00006  *   $Date: 2007-11-08 16:10:37 +0100 (Do, 08 Nov 2007) $ 
00007  ***************************************************************/
00008  
00051 #ifdef _MSC_VER
00052 #pragma once
00053 #endif
00054 
00055 #ifndef OGDF_B_STACK_H
00056 #define OGDF_B_STACK_H
00057 
00058 
00059 #include <ogdf/basic/basic.h>
00060 
00061 
00062 namespace ogdf {
00063 
00064 template<class E, class INDEX> class BoundedStack;
00065 
00066 // output
00067 template<class E, class INDEX>
00068 void print(ostream &os, const BoundedStack<E,INDEX> &S, char delim = ' ');
00069 
00070 
00072 template<class E, class INDEX = int> class BoundedStack {
00073 
00074     friend void print OGDF_FT_ENDING (ostream &, const BoundedStack<E,INDEX> &, char);
00075 
00076     E *m_pTop;   
00077     E *m_pStart; 
00078     E *m_pStop;  
00079 
00080 public:
00082 
00086     BoundedStack() {
00087         m_pTop = m_pStart = m_pStop = 0;
00088     }
00089 
00091     explicit BoundedStack(INDEX n) {
00092         OGDF_ASSERT(n >= 1)
00093         m_pStart = new E[n];
00094         if (m_pStart == 0) THROW(InsufficientMemoryException);
00095         m_pTop  = m_pStart - 1;
00096         m_pStop = m_pStart+n;
00097     }
00098 
00100     BoundedStack(const BoundedStack<E> &S) {
00101         copy(S);
00102     }
00103 
00104     // destruction
00105     ~BoundedStack() {
00106         delete [] m_pStart;
00107     }
00108 
00110     const E &top() const {
00111         OGDF_ASSERT(m_pTop != m_pStart-1)
00112         return *m_pTop;
00113     }
00114 
00116     E &top() {
00117         OGDF_ASSERT(m_pTop != m_pStart-1)
00118         return *m_pTop;
00119     }
00120 
00122     INDEX size() const { return m_pTop - (m_pStart-1); }
00123 
00125     bool empty() { return m_pTop == (m_pStart-1); }
00126 
00128     bool full() { return m_pTop == (m_pStop-1); }
00129 
00131     bool valid() const { return m_pStart != 0; }
00132 
00134     INDEX capacity() const { return m_pStop - m_pStart; }
00135 
00137     void init() {
00138         delete [] m_pStart;
00139         m_pTop = m_pStart = m_pStart = 0;
00140     }
00141 
00143     void init(INDEX n) {
00144         OGDF_ASSERT(n >= 1)
00145 
00146         delete [] m_pStart;
00147 
00148         m_pStart = new E[n];
00149         if (m_pStart == 0) THROW(InsufficientMemoryException);
00150         m_pTop = m_pStart - 1;
00151         m_pStop = m_pStart+n;
00152     }
00153 
00155     BoundedStack<E> &operator=(const BoundedStack &S) {
00156         delete [] m_pStart;
00157         copy(S);
00158         return *this;
00159     }
00160 
00162     void push(const E &x) {
00163         OGDF_ASSERT(m_pTop != m_pStop-1)
00164         *++m_pTop = x;
00165     }
00166 
00168     E pop() {
00169         OGDF_ASSERT(m_pTop != (m_pStart-1))
00170         return *m_pTop--;
00171     }
00172 
00174     void clear() { m_pTop = m_pStart-1; }
00175 
00176 private:
00177     void copy(const BoundedStack<E> &S)
00178     {
00179         if(!S.valid()) {
00180             m_pTop = m_pStart = m_pStop = 0;
00181         } else {
00182             INDEX sz = S.m_pStop - S.m_pStart;
00183             m_pStart = new E[sz+1];
00184             if (m_pStart == 0) THROW(InsufficientMemoryException);
00185             m_pStop = m_pStart + sz;
00186             m_pTop  = m_pStart-1;
00187             for (E *pX = S.m_pStart-1; pX != S.m_pTop; )
00188                 *++m_pTop = *++pX;
00189         }
00190     }
00191 }; // class BoundedStack
00192 
00193 
00194 // output
00195 template<class E, class INDEX>
00196 void print(ostream &os, const BoundedStack<E,INDEX> &S, char delim/* = ' '*/)
00197 {
00198     for (const E *pX = S.m_pStart; pX != S.m_pTop; )
00199         os << *++pX << delim;
00200 }
00201 
00202 /*template<class E>
00203 void print(ostream &os, const BoundedStack<E> &S)
00204 { print(os,S,' '); }*/
00205 
00206 
00207 // output operator
00208 template<class E, class INDEX>
00209 ostream &operator<<(ostream &os, const BoundedStack<E,INDEX> &S)
00210 {
00211     print(os,S);
00212     return os;
00213 }
00214 
00215 } // end namespace ogdf
00216 
00217 
00218 #endif


© 1999-2007 by oreas GmbH, © 2005-2007 by University Dortmund and University Cologne.

Generated on Thu Nov 22 19:40:00 2007 by doxygen 1.5.4.