1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
/**
@file Queue.h
@maintainer Morgan McGuire, http://graphics.cs.williams.edu
@created 2002-07-09
@edited 2008-12-20
*/
#ifndef G3D_Queue_h
#define G3D_Queue_h
#include "G3D/platform.h"
#include "G3D/System.h"
#include "G3D/debug.h"
namespace G3D {
/**
Locate the indices of the break between of the two
sections of the circular queue. These are used to
construct two for loops that iterate over the whole
sequence without using the modulo operator.
[0 ... secondEnd) [head .... firstEnd)
\sa ThreadsafeQueue
*/
#define FIND_ENDS \
int firstEnd = head + num;\
int secondEnd = 0;\
if (firstEnd > numAllocated) {\
secondEnd = firstEnd - numAllocated;\
firstEnd = numAllocated;\
}
/**
Dynamic queue that uses a circular buffer for performance.
Faster than std::deque for objects with constructors.
*/
template <class T>
class Queue {
private:
//
// |<---- num ---->|
// [ | | | | | | | | | | | | | ]
// ^
// |
// head
//
//
/**
Only num elements are initialized.
*/
T* data;
/**
Index of the next element to be dequeue-d in data.
*/
int head;
/**
Number of elements (including head) that are visible and initialized.
*/
int num;
/**
Size of data array in elements.
*/
int numAllocated;
/** If a clear was needed, assumes it already occured */
void _copy(const Queue& other) {
debugAssert(data == NULL);
data = (T*)System::malloc(sizeof(T) * other.numAllocated);
debugAssert(data);
head = other.head;
num = other.num;
numAllocated = other.numAllocated;
FIND_ENDS;
for (int i = head; i < firstEnd; ++i) {
new (data + i)T(other.data[i]);
}
for (int i = 0; i < secondEnd; ++i) {
new (data + i)T(other.data[i]);
}
}
/**
Computes a data array index from a queue position. The queue position
may be negative.
*/
inline int index(int i) const {
return (head + i + numAllocated) % numAllocated;
}
/**
Allocates newSize elements and repacks the array.
*/
void repackAndRealloc(int newSize) {
// TODO: shrink queue
T* old = data;
data = (T*)System::malloc(newSize * sizeof(T));
debugAssert(data != NULL);
FIND_ENDS;
int j = 0;
for (int i = head; i < firstEnd; ++i, ++j) {
new (data + j)T(old[i]);
(old + i)->~T();
}
for (int i = 0; i < secondEnd; ++i, ++j) {
new (data + j)T(old[i]);
(old + i)->~T();
}
head = 0;
System::free(old);
numAllocated = newSize;
}
/**
Ensure that there is at least one element between
the tail and head, wrapping around in the circular
buffer.
*/
inline void reserveSpace() {
if (num == numAllocated) {
repackAndRealloc(numAllocated * 3 + 20);
}
}
public:
Queue() :
data(NULL),
head(0),
num(0),
numAllocated(0) {
}
/**
Copy constructor
*/
Queue(const Queue& other) : data(NULL) {
_copy(other);
}
/**
Destructor does not delete() the objects if T is a pointer type
(e.g. T = int*) instead, it deletes the pointers themselves and
leaves the objects. Call deleteAll if you want to dealocate
the objects referenced.
*/
virtual ~Queue() {
clear();
}
/**
Insert a new element into the front of the queue
(a traditional queue only uses pushBack).
*/
inline void pushFront(const T& e) {
reserveSpace();
// Get the index of head-1
int i = index(-1);
// Call the constructor on the newly exposed element.
new (data + i)T(e);
// Reassign the head to point to this index
head = i;
++num;
}
/**
Insert a new element at the end of the queue.
*/
inline void pushBack(const T& e) {
reserveSpace();
// Get the index of 1+tail
int i = index(num);
// Initialize that element
new (data + i)T(e);
++num;
}
/**
pushBack
*/
inline void enqueue(const T& e) {
pushBack(e);
}
/**
Remove the last element from the queue. The queue will never
shrink in size. (A typical queue only uses popFront).
*/
inline T popBack() {
int tail = index(num - 1);
T result(data[tail]);
// Call the destructor
(data + tail)->~T();
--num;
return result;
}
/**
Remove the next element from the head of the queue. The queue will never
shrink in size. */
inline T popFront() {
T result(data[head]);
// Call the destructor
(data + head)->~T();
head = (head + 1) % numAllocated;
--num;
return result;
}
/**
popFront
*/
inline T dequeue() {
return popFront();
}
/**
Removes all elements (invoking their destructors).
@param freeStorage If false, the underlying array is not deallocated
(allowing fast push in the future), however, the size of the Queue
is reported as zero.
*/
void clear(bool freeStorage = true) {
FIND_ENDS;
// Invoke the destructors on the elements
int i;
for (i = head; i < firstEnd; ++i) {
(data + i)->~T();
}
for (i = 0; i < secondEnd; ++i) {
(data + i)->~T();
}
num = 0;
head = 0;
if (freeStorage) {
numAllocated = 0;
System::free(data);
data = NULL;
}
}
/** Clear without freeing the underlying array. */
void fastClear() {
clear(false);
}
/**
Assignment operator.
*/
Queue& operator=(const Queue& other) {
clear();
_copy(other);
return *this;
}
/**
Number of elements in the queue.
*/
inline int size() const {
return num;
}
/**
Number of elements in the queue.
*/
inline int length() const {
return size();
}
/**
Performs bounds checks in debug mode
*/
inline T& operator[](int n) {
debugAssert((n >= 0) && (n < num));
return data[index(n)];
}
/**
Performs bounds checks in debug mode
*/
inline const T& operator[](int n) const {
debugAssert((n >= 0) && (n < num));
return data[index(n)];
}
/** Returns the back element */
inline const T& last() const {
return (*this)[size() - 1];
}
inline T& last() {
return (*this)[size() - 1];
}
bool empty() const {
return size() == 0;
}
/**
Returns true if the given element is in the queue.
*/
bool contains(const T& e) const {
for (int i = 0; i < size(); ++i) {
if ((*this)[i] == e) {
return true;
}
}
return false;
}
/**
Calls delete on all objects[0...size-1]
and sets the queue size to zero.
*/
void deleteAll() {
FIND_ENDS;
int i;
for (i = 0; i < secondEnd; ++i) {
delete data[i];
}
for (i = head; i < firstEnd; ++i) {
delete data[i];
}
clear();
}
};
#undef FIND_ENDS
}; // namespace
#endif
|