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/exceptions.h>
35 
36 namespace ogdf {
37 
39 
46 template<class E, class INDEX = int>
47 class BoundedQueue {
48  E* m_pStart;
49  E* m_pEnd;
50  E* m_pStop;
51  E* m_pFirst;
52 
53 public:
55  BoundedQueue() { m_pStart = m_pEnd = m_pFirst = m_pStop = nullptr; }
56 
58  explicit BoundedQueue(INDEX n) {
59  OGDF_ASSERT(n >= 1);
60  m_pStart = m_pEnd = m_pFirst = new E[n + 1];
61  if (m_pFirst == nullptr) {
63  }
64 
65  m_pStop = m_pFirst + n + 1;
66  }
67 
69  BoundedQueue(const BoundedQueue<E>& Q) { copy(Q); }
70 
72 
77  m_pStart = Q.m_pStart;
78  m_pEnd = Q.m_pEnd;
79  m_pStop = Q.m_pStop;
80  m_pFirst = Q.m_pFirst;
81  Q.m_pStart = Q.m_pEnd = Q.m_pFirst = Q.m_pStop = nullptr;
82  }
83 
85  ~BoundedQueue() { delete[] m_pFirst; }
86 
88  void init() {
89  delete[] m_pFirst;
90  m_pStart = m_pEnd = m_pFirst = m_pStop = nullptr;
91  }
92 
94  void init(INDEX n) {
95  delete[] m_pFirst;
96 
97  OGDF_ASSERT(n >= 1);
98  m_pStart = m_pEnd = m_pFirst = new E[n + 1];
99  if (m_pFirst == nullptr) {
101  }
102 
103  m_pStop = m_pFirst + n + 1;
104  }
105 
107  const E& top() const {
109  return *m_pStart;
110  }
111 
113  E& top() {
115  return *m_pStart;
116  }
117 
119  const E& bottom() const {
121  if (m_pEnd == m_pFirst) {
122  return *(m_pStop - 1);
123  } else {
124  return *(m_pEnd - 1);
125  }
126  }
127 
129  E& bottom() {
131  if (m_pEnd == m_pFirst) {
132  return *(m_pStop - 1);
133  } else {
134  return *(m_pEnd - 1);
135  }
136  }
137 
139  INDEX size() const {
140  return (m_pEnd >= m_pStart) ? (INDEX)(m_pEnd - m_pStart)
141  : (INDEX)((m_pEnd - m_pFirst) + (m_pStop - m_pStart));
142  }
143 
145  INDEX capacity() const { return (INDEX)((m_pStop - m_pFirst) - 1); }
146 
148  bool empty() { return m_pStart == m_pEnd; }
149 
151  bool full() {
152  INDEX h = m_pEnd - m_pStart;
153  return h >= 0 ? h == m_pStop - m_pFirst - 1 : h == -1;
154  }
155 
158  delete[] m_pFirst;
159  copy(Q);
160  return *this;
161  }
162 
164 
169  delete[] m_pFirst;
170 
171  m_pStart = Q.m_pStart;
172  m_pEnd = Q.m_pEnd;
173  m_pStop = Q.m_pStop;
174  m_pFirst = Q.m_pFirst;
175  Q.m_pStart = Q.m_pEnd = Q.m_pFirst = Q.m_pStop = nullptr;
176 
177  return *this;
178  }
179 
181  void append(const E& x) {
182  *m_pEnd++ = x;
183  if (m_pEnd == m_pStop) {
184  m_pEnd = m_pFirst;
185  }
187  }
188 
190  E pop() {
192  E x = *m_pStart++;
193  if (m_pStart == m_pStop) {
194  m_pStart = m_pFirst;
195  }
196  return x;
197  }
198 
200  void clear() { m_pStart = m_pEnd = m_pFirst; }
201 
203  void print(std::ostream& os, char delim = ' ') const {
204  for (const E* pX = m_pStart; pX != m_pEnd;) {
205  if (pX != m_pStart) {
206  os << delim;
207  }
208  os << *pX;
209  if (++pX == m_pStop) {
210  pX = m_pFirst;
211  }
212  }
213  }
214 
215 private:
216  void copy(const BoundedQueue<E>& Q) {
217  int n = Q.size() + 1;
218  m_pEnd = m_pStart = m_pFirst = new E[n];
219  if (m_pFirst == nullptr) {
221  }
222 
223  m_pStop = m_pStart + n;
224  for (E* pX = Q.m_pStart; pX != Q.m_pEnd;) {
225  *m_pEnd++ = *pX++;
226  if (pX == Q.m_pStop) {
227  pX = Q.m_pFirst;
228  }
229  }
230  }
231 };
232 
234 template<class E, class INDEX>
235 std::ostream& operator<<(std::ostream& os, const BoundedQueue<E, INDEX>& Q) {
236  Q.print(os);
237  return os;
238 }
239 
240 }
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::BoundedQueue::operator=
BoundedQueue< E > & operator=(BoundedQueue< E > &&Q)
Assignment operator (move semantics).
Definition: BoundedQueue.h:168
exceptions.h
Definition of exception classes.
ogdf::BoundedQueue::m_pEnd
E * m_pEnd
Pointer to first element of current sequence.
Definition: BoundedQueue.h:49
ogdf::InsufficientMemoryException
Exception thrown when not enough memory is available to execute an algorithm.
Definition: exceptions.h:205
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:54
ogdf::BoundedQueue::m_pStart
E * m_pStart
Definition: BoundedQueue.h:48
ogdf::BoundedQueue::clear
void clear()
Makes the queue empty.
Definition: BoundedQueue.h:200
ogdf::BoundedQueue::copy
void copy(const BoundedQueue< E > &Q)
Definition: BoundedQueue.h:216
ogdf::BoundedQueue::bottom
E & bottom()
Returns back element.
Definition: BoundedQueue.h:129
ogdf::BoundedQueue::full
bool full()
Returns true iff the queue is full.
Definition: BoundedQueue.h:151
ogdf::BoundedQueue::BoundedQueue
BoundedQueue(INDEX n)
Constructs an empty bounded queue for at most n elements.
Definition: BoundedQueue.h:58
ogdf::BoundedQueue::empty
bool empty()
Returns true iff the queue is empty.
Definition: BoundedQueue.h:148
ogdf::BoundedQueue::bottom
const E & bottom() const
Returns back element.
Definition: BoundedQueue.h:119
ogdf::BoundedQueue::capacity
INDEX capacity() const
Returns the capacity of the bounded queue.
Definition: BoundedQueue.h:145
ogdf::BoundedQueue::top
E & top()
Returns front element.
Definition: BoundedQueue.h:113
ogdf::BoundedQueue::BoundedQueue
BoundedQueue(const BoundedQueue< E > &Q)
Constructs a bounded queue that is a copy of Q.
Definition: BoundedQueue.h:69
ogdf::BoundedQueue::BoundedQueue
BoundedQueue(BoundedQueue< E > &&Q)
Constructs a bounded queue containing the elements of Q (move semantics).
Definition: BoundedQueue.h:76
ogdf::BoundedQueue::operator=
BoundedQueue< E > & operator=(const BoundedQueue< E > &Q)
Assignment operator.
Definition: BoundedQueue.h:157
ogdf::BoundedQueue::pop
E pop()
Removes front element and returns it.
Definition: BoundedQueue.h:190
OGDF_THROW
#define OGDF_THROW(CLASS)
Replacement for throw.
Definition: exceptions.h:70
ogdf::BoundedQueue::init
void init()
Reinitializes the bounded queue to a non-valid bounded queue.
Definition: BoundedQueue.h:88
ogdf::BoundedQueue
The parameterized class BoundedQueue implements queues with bounded size.
Definition: BoundedQueue.h:47
ogdf::operator<<
std::ostream & operator<<(std::ostream &os, const ogdf::Array< E, INDEX > &a)
Prints array a to output stream os.
Definition: Array.h:978
ogdf::BoundedQueue::size
INDEX size() const
Returns current size of the queue.
Definition: BoundedQueue.h:139
ogdf::BoundedQueue::BoundedQueue
BoundedQueue()
Pointer to first element of total array.
Definition: BoundedQueue.h:55
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:203
ogdf::BoundedQueue::m_pStop
E * m_pStop
Pointer to one past last element of current sequence.
Definition: BoundedQueue.h:50
ogdf::BoundedQueue::~BoundedQueue
~BoundedQueue()
Destruction.
Definition: BoundedQueue.h:85
ogdf::BoundedQueue::top
const E & top() const
Returns front element.
Definition: BoundedQueue.h:107
ogdf::BoundedQueue::init
void init(INDEX n)
Reinitializes the bounded queue to a bounded queue for at most n elements.
Definition: BoundedQueue.h:94
ogdf::BoundedQueue::m_pFirst
E * m_pFirst
Pointer to one past last element of total array.
Definition: BoundedQueue.h:51
ogdf::BoundedQueue::append
void append(const E &x)
Adds x at the end of queue.
Definition: BoundedQueue.h:181