Open
Graph Drawing
Framework

 v.2010.10
 

BoundedQueue.h

Go to the documentation of this file.
00001 /*
00002  * $Revision: 2027 $
00003  * 
00004  * last checkin:
00005  *   $Author: gutwenger $ 
00006  *   $Date: 2010-09-01 11:55:17 +0200 (Wed, 01 Sep 2010) $ 
00007  ***************************************************************/
00008  
00052 #ifdef _MSC_VER
00053 #pragma once
00054 #endif
00055 
00056 #ifndef OGDF_B_QUEUE_H
00057 #define OGDF_B_QUEUE_H
00058 
00059 
00060 #include <ogdf/basic/basic.h>
00061 
00062 
00063 namespace ogdf {
00064 
00065 template<class E, class INDEX> class BoundedQueue;
00066 
00067 // output
00068 template<class E, class INDEX>
00069 void print(ostream &os, const BoundedQueue<E,INDEX> &S, char delim = ' ');
00070 
00071 
00073 template<class E, class INDEX = int> class BoundedQueue {
00074 
00075     E *m_pStart; 
00076     E *m_pEnd;   
00077     E *m_pStop;  
00078     E *m_pFirst; 
00079 
00080 public:
00082     explicit BoundedQueue(INDEX n) {
00083         OGDF_ASSERT(n >= 1)
00084         m_pStart = m_pEnd = m_pFirst = new E[n+1];
00085         if (m_pFirst == 0) OGDF_THROW(InsufficientMemoryException);
00086 
00087         m_pStop = m_pFirst+n+1;
00088     }
00089 
00091     BoundedQueue(const BoundedQueue<E> &Q) {
00092         copy(Q);
00093     }
00094 
00095     // destruction
00096     ~BoundedQueue() { delete [] m_pFirst; }
00097 
00099     const E &top() const {
00100         OGDF_ASSERT(m_pStart != m_pEnd)
00101         return *m_pStart;
00102     }
00103 
00105     E &top() {
00106         OGDF_ASSERT(m_pStart != m_pEnd)
00107         return *m_pStart;
00108     }
00109 
00111     const E &bottom() const {
00112         OGDF_ASSERT(m_pStart != m_pEnd)
00113         if (m_pEnd == m_pFirst) return *(m_pStop-1);
00114         else return *(m_pEnd-1);
00115     }
00116 
00118     E &bottom() {
00119         OGDF_ASSERT(m_pStart != m_pEnd)
00120         if (m_pEnd == m_pFirst) return *(m_pStop-1);
00121         else return *(m_pEnd-1);
00122     }
00123 
00125     INDEX size() const {
00126         return (m_pEnd >= m_pStart) ?
00127             (m_pEnd - m_pStart) :
00128             (m_pEnd-m_pFirst)+(m_pStop-m_pStart);
00129     }
00130 
00132     INDEX capacity() const { return (m_pStop - m_pFirst)-1; }
00133 
00135     bool empty() { return m_pStart == m_pEnd; }
00136 
00138     bool full() {
00139         INDEX h = m_pEnd-m_pStart
00140         return ( h>=0 ) ? 
00141             (h == m_pStop-m_pFirst-1) :
00142             (h == -1);
00143     }
00144 
00146     BoundedQueue<E> &operator=(const BoundedQueue<E> &Q) {
00147         delete [] m_pFirst;
00148         copy(Q);
00149         return *this;
00150     }
00151 
00153     void append(const E &x) {
00154         *m_pEnd++ = x;
00155         if (m_pEnd == m_pStop) m_pEnd = m_pFirst;
00156         OGDF_ASSERT(m_pStart != m_pEnd)
00157     }
00158 
00160     E pop() {
00161         OGDF_ASSERT(m_pStart != m_pEnd)
00162         E x = *m_pStart++;
00163         if (m_pStart == m_pStop) m_pStart = m_pFirst;
00164         return x;
00165     }
00166 
00168     void clear() { m_pStart = m_pEnd = m_pFirst; }
00169 
00171     void print(ostream &os, char delim = ' ') const
00172     {
00173         for (const E *pX = m_pStart; pX != m_pEnd; ) {
00174             if (pX != m_pStart) os << delim;
00175             os << *pX;
00176             if (++pX == m_pStop) pX = m_pFirst;
00177         }
00178     }
00179 
00180 private:
00181     void copy(const BoundedQueue<E> &Q) {
00182         int n = Q.size()+1;
00183         m_pEnd = m_pStart = m_pFirst = new E[n];
00184         if (m_pFirst == 0) OGDF_THROW(InsufficientMemoryException);
00185 
00186         m_pStop = m_pStart + n;
00187         for (E *pX = Q.m_pStart; pX != Q.m_pEnd; ) {
00188             *m_pEnd++ = *pX++;
00189             if (pX == Q.m_pStop) pX = Q.m_pFirst;
00190         }
00191     }
00192 }; // class BoundedQueue
00193 
00194 
00195 
00196 // output operator
00197 template<class E, class INDEX>
00198 ostream &operator<<(ostream &os, const BoundedQueue<E,INDEX> &Q)
00199 {
00200     Q.print(os);
00201     return os;
00202 }
00203 
00204 } // end namespace ogdf
00205 
00206 
00207 #endif