Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

BoundedQueue.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/basic.h>
35 #include <ogdf/basic/exceptions.h>
36 
37 #include <ostream>
38 
39 namespace ogdf {
40 
42 
49 template<class E, class INDEX = int>
50 class BoundedQueue {
51  E* m_pStart;
52  E* m_pEnd;
53  E* m_pStop;
54  E* m_pFirst;
55 
56 public:
58  BoundedQueue() { m_pStart = m_pEnd = m_pFirst = m_pStop = nullptr; }
59 
61  explicit BoundedQueue(INDEX n) {
62  OGDF_ASSERT(n >= 1);
63  m_pStart = m_pEnd = m_pFirst = new E[n + 1];
64  if (m_pFirst == nullptr) {
66  }
67 
68  m_pStop = m_pFirst + n + 1;
69  }
70 
72  BoundedQueue(const BoundedQueue<E>& Q) { copy(Q); }
73 
75 
80  m_pStart = Q.m_pStart;
81  m_pEnd = Q.m_pEnd;
82  m_pStop = Q.m_pStop;
83  m_pFirst = Q.m_pFirst;
84  Q.m_pStart = Q.m_pEnd = Q.m_pFirst = Q.m_pStop = nullptr;
85  }
86 
88  ~BoundedQueue() { delete[] m_pFirst; }
89 
91  void init() {
92  delete[] m_pFirst;
93  m_pStart = m_pEnd = m_pFirst = m_pStop = nullptr;
94  }
95 
97  void init(INDEX n) {
98  delete[] m_pFirst;
99 
100  OGDF_ASSERT(n >= 1);
101  m_pStart = m_pEnd = m_pFirst = new E[n + 1];
102  if (m_pFirst == nullptr) {
104  }
105 
106  m_pStop = m_pFirst + n + 1;
107  }
108 
110  const E& top() const {
112  return *m_pStart;
113  }
114 
116  E& top() {
118  return *m_pStart;
119  }
120 
122  const E& bottom() const {
124  if (m_pEnd == m_pFirst) {
125  return *(m_pStop - 1);
126  } else {
127  return *(m_pEnd - 1);
128  }
129  }
130 
132  E& bottom() {
134  if (m_pEnd == m_pFirst) {
135  return *(m_pStop - 1);
136  } else {
137  return *(m_pEnd - 1);
138  }
139  }
140 
142  INDEX size() const {
143  return (m_pEnd >= m_pStart) ? (INDEX)(m_pEnd - m_pStart)
144  : (INDEX)((m_pEnd - m_pFirst) + (m_pStop - m_pStart));
145  }
146 
148  INDEX capacity() const { return (INDEX)((m_pStop - m_pFirst) - 1); }
149 
151  bool empty() { return m_pStart == m_pEnd; }
152 
154  bool full() {
155  INDEX h = m_pEnd - m_pStart;
156  return h >= 0 ? h == m_pStop - m_pFirst - 1 : h == -1;
157  }
158 
161  delete[] m_pFirst;
162  copy(Q);
163  return *this;
164  }
165 
167 
172  delete[] m_pFirst;
173 
174  m_pStart = Q.m_pStart;
175  m_pEnd = Q.m_pEnd;
176  m_pStop = Q.m_pStop;
177  m_pFirst = Q.m_pFirst;
178  Q.m_pStart = Q.m_pEnd = Q.m_pFirst = Q.m_pStop = nullptr;
179 
180  return *this;
181  }
182 
184  void append(const E& x) {
185  *m_pEnd++ = x;
186  if (m_pEnd == m_pStop) {
187  m_pEnd = m_pFirst;
188  }
190  }
191 
193  E pop() {
195  E x = *m_pStart++;
196  if (m_pStart == m_pStop) {
197  m_pStart = m_pFirst;
198  }
199  return x;
200  }
201 
203  void clear() { m_pStart = m_pEnd = m_pFirst; }
204 
206  void print(std::ostream& os, char delim = ' ') const {
207  for (const E* pX = m_pStart; pX != m_pEnd;) {
208  if (pX != m_pStart) {
209  os << delim;
210  }
211  os << *pX;
212  if (++pX == m_pStop) {
213  pX = m_pFirst;
214  }
215  }
216  }
217 
218 private:
219  void copy(const BoundedQueue<E>& Q) {
220  int n = Q.size() + 1;
221  m_pEnd = m_pStart = m_pFirst = new E[n];
222  if (m_pFirst == nullptr) {
224  }
225 
226  m_pStop = m_pStart + n;
227  for (E* pX = Q.m_pStart; pX != Q.m_pEnd;) {
228  *m_pEnd++ = *pX++;
229  if (pX == Q.m_pStop) {
230  pX = Q.m_pFirst;
231  }
232  }
233  }
234 };
235 
237 template<class E, class INDEX>
238 std::ostream& operator<<(std::ostream& os, const BoundedQueue<E, INDEX>& Q) {
239  Q.print(os);
240  return os;
241 }
242 
243 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::BoundedQueue::operator=
BoundedQueue< E > & operator=(BoundedQueue< E > &&Q)
Assignment operator (move semantics).
Definition: BoundedQueue.h:171
exceptions.h
Definition of exception classes.
ogdf::BoundedQueue::m_pEnd
E * m_pEnd
Pointer to first element of current sequence.
Definition: BoundedQueue.h:52
ogdf::InsufficientMemoryException
Exception thrown when not enough memory is available to execute an algorithm.
Definition: exceptions.h:209
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::BoundedQueue::m_pStart
E * m_pStart
Definition: BoundedQueue.h:51
ogdf::BoundedQueue::clear
void clear()
Makes the queue empty.
Definition: BoundedQueue.h:203
ogdf::BoundedQueue::copy
void copy(const BoundedQueue< E > &Q)
Definition: BoundedQueue.h:219
ogdf::BoundedQueue::bottom
E & bottom()
Returns back element.
Definition: BoundedQueue.h:132
ogdf::BoundedQueue::full
bool full()
Returns true iff the queue is full.
Definition: BoundedQueue.h:154
ogdf::BoundedQueue::BoundedQueue
BoundedQueue(INDEX n)
Constructs an empty bounded queue for at most n elements.
Definition: BoundedQueue.h:61
ogdf::BoundedQueue::empty
bool empty()
Returns true iff the queue is empty.
Definition: BoundedQueue.h:151
ogdf::BoundedQueue::bottom
const E & bottom() const
Returns back element.
Definition: BoundedQueue.h:122
ogdf::BoundedQueue::capacity
INDEX capacity() const
Returns the capacity of the bounded queue.
Definition: BoundedQueue.h:148
ogdf::BoundedQueue::top
E & top()
Returns front element.
Definition: BoundedQueue.h:116
ogdf::BoundedQueue::BoundedQueue
BoundedQueue(const BoundedQueue< E > &Q)
Constructs a bounded queue that is a copy of Q.
Definition: BoundedQueue.h:72
ogdf::BoundedQueue::BoundedQueue
BoundedQueue(BoundedQueue< E > &&Q)
Constructs a bounded queue containing the elements of Q (move semantics).
Definition: BoundedQueue.h:79
ogdf::BoundedQueue::operator=
BoundedQueue< E > & operator=(const BoundedQueue< E > &Q)
Assignment operator.
Definition: BoundedQueue.h:160
ogdf::BoundedQueue::pop
E pop()
Removes front element and returns it.
Definition: BoundedQueue.h:193
OGDF_THROW
#define OGDF_THROW(CLASS)
Replacement for throw.
Definition: exceptions.h:74
ogdf::BoundedQueue::init
void init()
Reinitializes the bounded queue to a non-valid bounded queue.
Definition: BoundedQueue.h:91
ogdf::BoundedQueue
The parameterized class BoundedQueue implements queues with bounded size.
Definition: BoundedQueue.h:50
ogdf::operator<<
std::ostream & operator<<(std::ostream &os, const ogdf::Array< E, INDEX > &a)
Prints array a to output stream os.
Definition: Array.h:983
ogdf::BoundedQueue::size
INDEX size() const
Returns current size of the queue.
Definition: BoundedQueue.h:142
ogdf::BoundedQueue::BoundedQueue
BoundedQueue()
Pointer to first element of total array.
Definition: BoundedQueue.h:58
ogdf::BoundedQueue::print
void print(std::ostream &os, char delim=' ') const
Prints the queue to output stream os with the seperator delim.
Definition: BoundedQueue.h:206
ogdf::BoundedQueue::m_pStop
E * m_pStop
Pointer to one past last element of current sequence.
Definition: BoundedQueue.h:53
ogdf::BoundedQueue::~BoundedQueue
~BoundedQueue()
Destruction.
Definition: BoundedQueue.h:88
basic.h
Basic declarations, included by all source files.
ogdf::BoundedQueue::top
const E & top() const
Returns front element.
Definition: BoundedQueue.h:110
ogdf::BoundedQueue::init
void init(INDEX n)
Reinitializes the bounded queue to a bounded queue for at most n elements.
Definition: BoundedQueue.h:97
ogdf::BoundedQueue::m_pFirst
E * m_pFirst
Pointer to one past last element of total array.
Definition: BoundedQueue.h:54
ogdf::BoundedQueue::append
void append(const E &x)
Adds x at the end of queue.
Definition: BoundedQueue.h:184