Open
Graph Drawing
Framework

 v.2012.05
 

BoundedQueue.h
Go to the documentation of this file.
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  
00042 #ifdef _MSC_VER
00043 #pragma once
00044 #endif
00045 
00046 #ifndef OGDF_B_QUEUE_H
00047 #define OGDF_B_QUEUE_H
00048 
00049 
00050 #include <ogdf/basic/basic.h>
00051 
00052 
00053 namespace ogdf {
00054 
00055 template<class E, class INDEX> class BoundedQueue;
00056 
00057 // output
00058 template<class E, class INDEX>
00059 void print(ostream &os, const BoundedQueue<E,INDEX> &S, char delim = ' ');
00060 
00061 
00063 template<class E, class INDEX = int> class BoundedQueue {
00064 
00065     E *m_pStart; 
00066     E *m_pEnd;   
00067     E *m_pStop;  
00068     E *m_pFirst; 
00069 
00070 public:
00072     explicit BoundedQueue(INDEX n) {
00073         OGDF_ASSERT(n >= 1)
00074         m_pStart = m_pEnd = m_pFirst = new E[n+1];
00075         if (m_pFirst == 0) OGDF_THROW(InsufficientMemoryException);
00076 
00077         m_pStop = m_pFirst+n+1;
00078     }
00079 
00081     BoundedQueue(const BoundedQueue<E> &Q) {
00082         copy(Q);
00083     }
00084 
00085     // destruction
00086     ~BoundedQueue() { delete [] m_pFirst; }
00087 
00089     const E &top() const {
00090         OGDF_ASSERT(m_pStart != m_pEnd)
00091         return *m_pStart;
00092     }
00093 
00095     E &top() {
00096         OGDF_ASSERT(m_pStart != m_pEnd)
00097         return *m_pStart;
00098     }
00099 
00101     const E &bottom() const {
00102         OGDF_ASSERT(m_pStart != m_pEnd)
00103         if (m_pEnd == m_pFirst) return *(m_pStop-1);
00104         else return *(m_pEnd-1);
00105     }
00106 
00108     E &bottom() {
00109         OGDF_ASSERT(m_pStart != m_pEnd)
00110         if (m_pEnd == m_pFirst) return *(m_pStop-1);
00111         else return *(m_pEnd-1);
00112     }
00113 
00115     INDEX size() const {
00116         return (m_pEnd >= m_pStart) ?
00117             (m_pEnd - m_pStart) :
00118             (m_pEnd-m_pFirst)+(m_pStop-m_pStart);
00119     }
00120 
00122     INDEX capacity() const { return (m_pStop - m_pFirst)-1; }
00123 
00125     bool empty() { return m_pStart == m_pEnd; }
00126 
00128     bool full() {
00129         INDEX h = m_pEnd-m_pStart
00130         return ( h>=0 ) ? 
00131             (h == m_pStop-m_pFirst-1) :
00132             (h == -1);
00133     }
00134 
00136     BoundedQueue<E> &operator=(const BoundedQueue<E> &Q) {
00137         delete [] m_pFirst;
00138         copy(Q);
00139         return *this;
00140     }
00141 
00143     void append(const E &x) {
00144         *m_pEnd++ = x;
00145         if (m_pEnd == m_pStop) m_pEnd = m_pFirst;
00146         OGDF_ASSERT(m_pStart != m_pEnd)
00147     }
00148 
00150     E pop() {
00151         OGDF_ASSERT(m_pStart != m_pEnd)
00152         E x = *m_pStart++;
00153         if (m_pStart == m_pStop) m_pStart = m_pFirst;
00154         return x;
00155     }
00156 
00158     void clear() { m_pStart = m_pEnd = m_pFirst; }
00159 
00161     void print(ostream &os, char delim = ' ') const
00162     {
00163         for (const E *pX = m_pStart; pX != m_pEnd; ) {
00164             if (pX != m_pStart) os << delim;
00165             os << *pX;
00166             if (++pX == m_pStop) pX = m_pFirst;
00167         }
00168     }
00169 
00170 private:
00171     void copy(const BoundedQueue<E> &Q) {
00172         int n = Q.size()+1;
00173         m_pEnd = m_pStart = m_pFirst = new E[n];
00174         if (m_pFirst == 0) OGDF_THROW(InsufficientMemoryException);
00175 
00176         m_pStop = m_pStart + n;
00177         for (E *pX = Q.m_pStart; pX != Q.m_pEnd; ) {
00178             *m_pEnd++ = *pX++;
00179             if (pX == Q.m_pStop) pX = Q.m_pFirst;
00180         }
00181     }
00182 }; // class BoundedQueue
00183 
00184 
00185 
00186 // output operator
00187 template<class E, class INDEX>
00188 ostream &operator<<(ostream &os, const BoundedQueue<E,INDEX> &Q)
00189 {
00190     Q.print(os);
00191     return os;
00192 }
00193 
00194 } // end namespace ogdf
00195 
00196 
00197 #endif