Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
BoundedQueue.h
Go to the documentation of this file.
1
32#pragma once
33
34#include <ogdf/basic/basic.h>
36
37#include <ostream>
38
39namespace ogdf {
40
42
49template<class E, class INDEX = int>
52 E* m_pEnd;
55
56public:
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
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) {
188 }
190 }
191
193 E pop() {
195 E x = *m_pStart++;
196 if (m_pStart == m_pStop) {
198 }
199 return x;
200 }
201
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
218private:
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
237template<class E, class INDEX>
238std::ostream& operator<<(std::ostream& os, const BoundedQueue<E, INDEX>& Q) {
239 Q.print(os);
240 return os;
241}
242
243}
Basic declarations, included by all source files.
The parameterized class BoundedQueue implements queues with bounded size.
E * m_pFirst
Pointer to one past last element of total array.
bool empty()
Returns true iff the queue is empty.
void append(const E &x)
Adds x at the end of queue.
INDEX size() const
Returns current size of the queue.
void clear()
Makes the queue empty.
BoundedQueue< E > & operator=(BoundedQueue< E > &&Q)
Assignment operator (move semantics).
void copy(const BoundedQueue< E > &Q)
E & bottom()
Returns back element.
void print(std::ostream &os, char delim=' ') const
Prints the queue to output stream os with the seperator delim.
E & top()
Returns front element.
BoundedQueue(BoundedQueue< E > &&Q)
Constructs a bounded queue containing the elements of Q (move semantics).
const E & top() const
Returns front element.
BoundedQueue(const BoundedQueue< E > &Q)
Constructs a bounded queue that is a copy of Q.
~BoundedQueue()
Destruction.
INDEX capacity() const
Returns the capacity of the bounded queue.
const E & bottom() const
Returns back element.
BoundedQueue< E > & operator=(const BoundedQueue< E > &Q)
Assignment operator.
void init(INDEX n)
Reinitializes the bounded queue to a bounded queue for at most n elements.
void init()
Reinitializes the bounded queue to a non-valid bounded queue.
E * m_pEnd
Pointer to first element of current sequence.
BoundedQueue(INDEX n)
Constructs an empty bounded queue for at most n elements.
BoundedQueue()
Pointer to first element of total array.
E * m_pStop
Pointer to one past last element of current sequence.
E pop()
Removes front element and returns it.
bool full()
Returns true iff the queue is full.
Exception thrown when not enough memory is available to execute an algorithm.
Definition exceptions.h:209
Definition of exception classes.
#define OGDF_THROW(CLASS)
Replacement for throw.
Definition exceptions.h:67
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
The namespace for all OGDF objects.
std::ostream & operator<<(std::ostream &os, const ogdf::Array< E, INDEX > &a)
Prints array a to output stream os.
Definition Array.h:983