Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

BoundedQueue.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/exceptions.h>
35 
36 namespace ogdf {
37 
39 
46 template<class E, class INDEX = int> class BoundedQueue {
47 
48  E *m_pStart;
49  E *m_pEnd;
50  E *m_pStop;
51  E *m_pFirst;
52 
53 public:
56  m_pStart = m_pEnd = m_pFirst = m_pStop = nullptr;
57  }
58 
60  explicit BoundedQueue(INDEX n) {
61  OGDF_ASSERT(n >= 1);
62  m_pStart = m_pEnd = m_pFirst = new E[n+1];
63  if (m_pFirst == nullptr) OGDF_THROW(InsufficientMemoryException);
64 
65  m_pStop = m_pFirst+n+1;
66  }
67 
70  copy(Q);
71  }
72 
74 
79  m_pStart = Q.m_pStart;
80  m_pEnd = Q.m_pEnd;
81  m_pStop = Q.m_pStop;
82  m_pFirst = Q.m_pFirst;
83  Q.m_pStart = Q.m_pEnd = Q.m_pFirst = Q.m_pStop = nullptr;
84  }
85 
87  ~BoundedQueue() { delete[] m_pFirst; }
88 
90  void init() {
91  delete[] m_pFirst;
92  m_pStart = m_pEnd = m_pFirst = m_pStop = nullptr;
93  }
94 
96  void init(INDEX n) {
97  delete[] m_pFirst;
98 
99  OGDF_ASSERT(n >= 1);
100  m_pStart = m_pEnd = m_pFirst = new E[n+1];
101  if (m_pFirst == nullptr) OGDF_THROW(InsufficientMemoryException);
102 
103  m_pStop = m_pFirst+n+1;
104  }
105 
107  const E &top() const {
108  OGDF_ASSERT(m_pStart != m_pEnd);
109  return *m_pStart;
110  }
111 
113  E &top() {
114  OGDF_ASSERT(m_pStart != m_pEnd);
115  return *m_pStart;
116  }
117 
119  const E &bottom() const {
120  OGDF_ASSERT(m_pStart != m_pEnd);
121  if (m_pEnd == m_pFirst) return *(m_pStop-1);
122  else return *(m_pEnd-1);
123  }
124 
126  E &bottom() {
127  OGDF_ASSERT(m_pStart != m_pEnd);
128  if (m_pEnd == m_pFirst) return *(m_pStop-1);
129  else return *(m_pEnd-1);
130  }
131 
133  INDEX size() const {
134  return (m_pEnd >= m_pStart) ?
135  (INDEX)(m_pEnd - m_pStart) :
136  (INDEX)((m_pEnd-m_pFirst)+(m_pStop-m_pStart));
137  }
138 
140  INDEX capacity() const { return (INDEX)((m_pStop - m_pFirst)-1); }
141 
143  bool empty() { return m_pStart == m_pEnd; }
144 
146  bool full() {
147  INDEX h = m_pEnd-m_pStart;
148  return ( h >= 0 ) ?
149  (h == m_pStop-m_pFirst-1) :
150  (h == -1);
151  }
152 
155  delete[] m_pFirst;
156  copy(Q);
157  return *this;
158  }
159 
161 
166  delete[] m_pFirst;
167 
168  m_pStart = Q.m_pStart;
169  m_pEnd = Q.m_pEnd;
170  m_pStop = Q.m_pStop;
171  m_pFirst = Q.m_pFirst;
172  Q.m_pStart = Q.m_pEnd = Q.m_pFirst = Q.m_pStop = nullptr;
173 
174  return *this;
175  }
176 
177 
179  void append(const E &x) {
180  *m_pEnd++ = x;
181  if (m_pEnd == m_pStop) m_pEnd = m_pFirst;
182  OGDF_ASSERT(m_pStart != m_pEnd);
183  }
184 
186  E pop() {
187  OGDF_ASSERT(m_pStart != m_pEnd);
188  E x = *m_pStart++;
189  if (m_pStart == m_pStop) m_pStart = m_pFirst;
190  return x;
191  }
192 
194  void clear() { m_pStart = m_pEnd = m_pFirst; }
195 
197  void print(ostream &os, char delim = ' ') const
198  {
199  for (const E *pX = m_pStart; pX != m_pEnd; ) {
200  if (pX != m_pStart) os << delim;
201  os << *pX;
202  if (++pX == m_pStop) pX = m_pFirst;
203  }
204  }
205 
206 private:
207  void copy(const BoundedQueue<E> &Q) {
208  int n = Q.size()+1;
209  m_pEnd = m_pStart = m_pFirst = new E[n];
210  if (m_pFirst == nullptr) OGDF_THROW(InsufficientMemoryException);
211 
212  m_pStop = m_pStart + n;
213  for (E *pX = Q.m_pStart; pX != Q.m_pEnd; ) {
214  *m_pEnd++ = *pX++;
215  if (pX == Q.m_pStop) pX = Q.m_pFirst;
216  }
217  }
218 }; // class BoundedQueue
219 
220 
222 template<class E, class INDEX>
223 ostream &operator<<(ostream &os, const BoundedQueue<E,INDEX> &Q)
224 {
225  Q.print(os);
226  return os;
227 }
228 
229 } // end namespace ogdf
const E & bottom() const
Returns back element.
Definition: BoundedQueue.h:119
INDEX size() const
Returns current size of the queue.
Definition: BoundedQueue.h:133
void init()
Reinitializes the bounded queue to a non-valid bounded queue.
Definition: BoundedQueue.h:90
#define OGDF_THROW(CLASS)
Replacement for throw.
Definition: exceptions.h:60
BoundedQueue()
Pointer to first element of total array.
Definition: BoundedQueue.h:55
E * m_pStop
Pointer to one past last element of current sequence.
Definition: BoundedQueue.h:50
const E & top() const
Returns front element.
Definition: BoundedQueue.h:107
~BoundedQueue()
Destruction.
Definition: BoundedQueue.h:87
void init(INDEX n)
Reinitializes the bounded queue to a bounded queue for at most n elements.
Definition: BoundedQueue.h:96
void append(const E &x)
Adds x at the end of queue.
Definition: BoundedQueue.h:179
void clear()
Makes the queue empty.
Definition: BoundedQueue.h:194
E * m_pEnd
Pointer to first element of current sequence.
Definition: BoundedQueue.h:49
Definition of exception classes.
void print(ostream &os, char delim=' ') const
Prints the queue to output stream os with the seperator delim.
Definition: BoundedQueue.h:197
E * m_pFirst
Pointer to one past last element of total array.
Definition: BoundedQueue.h:51
Exception thrown when not enough memory is available to execute an algorithm.
Definition: exceptions.h:235
bool empty()
Returns true iff the queue is empty.
Definition: BoundedQueue.h:143
E & bottom()
Returns back element.
Definition: BoundedQueue.h:126
void copy(const BoundedQueue< E > &Q)
Definition: BoundedQueue.h:207
BoundedQueue< E > & operator=(BoundedQueue< E > &&Q)
Assignment operator (move semantics).
Definition: BoundedQueue.h:165
BoundedQueue(BoundedQueue< E > &&Q)
Constructs a bounded queue containing the elements of Q (move semantics).
Definition: BoundedQueue.h:78
INDEX capacity() const
Returns the capacity of the bounded queue.
Definition: BoundedQueue.h:140
BoundedQueue(INDEX n)
Constructs an empty bounded queue for at most n elements.
Definition: BoundedQueue.h:60
bool full()
Returns true iff the queue is full.
Definition: BoundedQueue.h:146
BoundedQueue< E > & operator=(const BoundedQueue< E > &Q)
Assignment operator.
Definition: BoundedQueue.h:154
The namespace for all OGDF objects.
E & top()
Returns front element.
Definition: BoundedQueue.h:113
BoundedQueue(const BoundedQueue< E > &Q)
Constructs a bounded queue that is a copy of Q.
Definition: BoundedQueue.h:69
The parameterized class BoundedQueue implements queues with bounded size.
Definition: BoundedQueue.h:46
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
E pop()
Removes front element and returns it.
Definition: BoundedQueue.h:186