Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

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 ? h == m_pStop - m_pFirst - 1 : h == -1;
149  }
150 
153  delete[] m_pFirst;
154  copy(Q);
155  return *this;
156  }
157 
159 
164  delete[] m_pFirst;
165 
166  m_pStart = Q.m_pStart;
167  m_pEnd = Q.m_pEnd;
168  m_pStop = Q.m_pStop;
169  m_pFirst = Q.m_pFirst;
170  Q.m_pStart = Q.m_pEnd = Q.m_pFirst = Q.m_pStop = nullptr;
171 
172  return *this;
173  }
174 
175 
177  void append(const E &x) {
178  *m_pEnd++ = x;
179  if (m_pEnd == m_pStop) m_pEnd = m_pFirst;
180  OGDF_ASSERT(m_pStart != m_pEnd);
181  }
182 
184  E pop() {
185  OGDF_ASSERT(m_pStart != m_pEnd);
186  E x = *m_pStart++;
187  if (m_pStart == m_pStop) m_pStart = m_pFirst;
188  return x;
189  }
190 
192  void clear() { m_pStart = m_pEnd = m_pFirst; }
193 
195  void print(std::ostream &os, char delim = ' ') const
196  {
197  for (const E *pX = m_pStart; pX != m_pEnd; ) {
198  if (pX != m_pStart) os << delim;
199  os << *pX;
200  if (++pX == m_pStop) pX = m_pFirst;
201  }
202  }
203 
204 private:
205  void copy(const BoundedQueue<E> &Q) {
206  int n = Q.size()+1;
207  m_pEnd = m_pStart = m_pFirst = new E[n];
208  if (m_pFirst == nullptr) OGDF_THROW(InsufficientMemoryException);
209 
210  m_pStop = m_pStart + n;
211  for (E *pX = Q.m_pStart; pX != Q.m_pEnd; ) {
212  *m_pEnd++ = *pX++;
213  if (pX == Q.m_pStop) pX = Q.m_pFirst;
214  }
215  }
216 };
217 
219 template<class E, class INDEX>
220 std::ostream &operator<<(std::ostream &os, const BoundedQueue<E,INDEX> &Q)
221 {
222  Q.print(os);
223  return os;
224 }
225 
226 }
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:65
void print(std::ostream &os, char delim=' ') const
Prints the queue to output stream os with the seperator delim.
Definition: BoundedQueue.h:195
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:177
void clear()
Makes the queue empty.
Definition: BoundedQueue.h:192
E * m_pEnd
Pointer to first element of current sequence.
Definition: BoundedQueue.h:49
Definition of exception classes.
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:213
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:205
BoundedQueue< E > & operator=(BoundedQueue< E > &&Q)
Assignment operator (move semantics).
Definition: BoundedQueue.h:163
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:152
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:184