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
35
36namespace ogdf {
37
39
46template<class E, class INDEX = int>
49 E* m_pEnd;
52
53public:
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
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() {
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) {
185 }
187 }
188
190 E pop() {
192 E x = *m_pStart++;
193 if (m_pStart == m_pStop) {
195 }
196 return x;
197 }
198
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
215private:
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
234template<class E, class INDEX>
235std::ostream& operator<<(std::ostream& os, const BoundedQueue<E, INDEX>& Q) {
236 Q.print(os);
237 return os;
238}
239
240}
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:203
Definition of exception classes.
#define OGDF_THROW(CLASS)
Replacement for throw.
Definition exceptions.h:63
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:41
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
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:978