TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::Queue< T > Class Template Reference

#include <Queue.h>

Public Member Functions

 Queue ()
 
 Queue (const Queue &other)
 
virtual ~Queue ()
 
void pushFront (const T &e)
 
void pushBack (const T &e)
 
void enqueue (const T &e)
 
popBack ()
 
popFront ()
 
dequeue ()
 
void clear (bool freeStorage=true)
 
void fastClear ()
 
Queueoperator= (const Queue &other)
 
int size () const
 
int length () const
 
T & operator[] (int n)
 
const T & operator[] (int n) const
 
const T & last () const
 
T & last ()
 
bool empty () const
 
bool contains (const T &e) const
 
void deleteAll ()
 

Private Member Functions

void _copy (const Queue &other)
 
int index (int i) const
 
void repackAndRealloc (int newSize)
 
void reserveSpace ()
 

Private Attributes

T * data
 
int head
 
int num
 
int numAllocated
 

Detailed Description

template<class T>
class G3D::Queue< T >

Dynamic queue that uses a circular buffer for performance.

Faster than std::deque for objects with constructors.

Constructor & Destructor Documentation

template<class T >
G3D::Queue< T >::Queue ( )
inline
144  :
145  data(NULL),
146  head(0),
147  num(0),
148  numAllocated(0) {
149  }
arena_t NULL
Definition: jemalloc_internal.h:624
T * data
Definition: Queue.h:58
int numAllocated
Definition: Queue.h:73
int num
Definition: Queue.h:68
int head
Definition: Queue.h:63
template<class T >
G3D::Queue< T >::Queue ( const Queue< T > &  other)
inline

Copy constructor

155  : data(NULL) {
156  _copy(other);
157  }
arena_t NULL
Definition: jemalloc_internal.h:624
T * data
Definition: Queue.h:58
void _copy(const Queue &other)
Definition: Queue.h:76

+ Here is the call graph for this function:

template<class T >
virtual G3D::Queue< T >::~Queue ( )
inlinevirtual

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.

166  {
167  clear();
168  }
void clear(bool freeStorage=true)
Definition: Queue.h:253

+ Here is the call graph for this function:

Member Function Documentation

template<class T >
void G3D::Queue< T >::_copy ( const Queue< T > &  other)
inlineprivate

If a clear was needed, assumes it already occured

76  {
77  debugAssert(data == NULL);
78  data = (T*)System::malloc(sizeof(T) * other.numAllocated);
80  head = other.head;
81  num = other.num;
82  numAllocated = other.numAllocated;
83 
84  FIND_ENDS;
85 
86  for (int i = head; i < firstEnd; ++i) {
87  new (data + i)T(other.data[i]);
88  }
89 
90  for (int i = 0; i < secondEnd; ++i) {
91  new (data + i)T(other.data[i]);
92  }
93  }
#define FIND_ENDS
Definition: Queue.h:29
arena_t NULL
Definition: jemalloc_internal.h:624
T * data
Definition: Queue.h:58
static void * malloc(size_t bytes)
Definition: System.cpp:1441
int numAllocated
Definition: Queue.h:73
#define debugAssert(exp)
Definition: debugAssert.h:160
int num
Definition: Queue.h:68
int head
Definition: Queue.h:63

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T >
void G3D::Queue< T >::clear ( bool  freeStorage = true)
inline

Removes all elements (invoking their destructors).

Parameters
freeStorageIf false, the underlying array is not deallocated (allowing fast push in the future), however, the size of the Queue is reported as zero.
253  {
254 
255  FIND_ENDS;
256 
257  // Invoke the destructors on the elements
258  int i;
259  for (i = head; i < firstEnd; ++i) {
260  (data + i)->~T();
261  }
262 
263  for (i = 0; i < secondEnd; ++i) {
264  (data + i)->~T();
265  }
266 
267  num = 0;
268  head = 0;
269  if (freeStorage) {
270  numAllocated = 0;
272  data = NULL;
273  }
274  }
#define FIND_ENDS
Definition: Queue.h:29
arena_t NULL
Definition: jemalloc_internal.h:624
T * data
Definition: Queue.h:58
int numAllocated
Definition: Queue.h:73
static void free(void *p)
Definition: System.cpp:1473
int num
Definition: Queue.h:68
int head
Definition: Queue.h:63

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T >
bool G3D::Queue< T >::contains ( const T &  e) const
inline

Returns true if the given element is in the queue.

337  {
338  for (int i = 0; i < size(); ++i) {
339  if ((*this)[i] == e) {
340  return true;
341  }
342  }
343 
344  return false;
345  }
int size() const
Definition: Queue.h:293

+ Here is the call graph for this function:

template<class T >
void G3D::Queue< T >::deleteAll ( )
inline

Calls delete on all objects[0...size-1] and sets the queue size to zero.

351  {
352  FIND_ENDS;
353 
354  int i;
355  for (i = 0; i < secondEnd; ++i) {
356  delete data[i];
357  }
358 
359  for (i = head; i < firstEnd; ++i) {
360  delete data[i];
361  }
362  clear();
363  }
#define FIND_ENDS
Definition: Queue.h:29
T * data
Definition: Queue.h:58
void clear(bool freeStorage=true)
Definition: Queue.h:253
int head
Definition: Queue.h:63

+ Here is the call graph for this function:

template<class T >
T G3D::Queue< T >::dequeue ( )
inline

popFront

241  {
242  return popFront();
243  }
T popFront()
Definition: Queue.h:228

+ Here is the call graph for this function:

template<class T >
bool G3D::Queue< T >::empty ( ) const
inline
330  {
331  return size() == 0;
332  }
int size() const
Definition: Queue.h:293

+ Here is the call graph for this function:

template<class T >
void G3D::Queue< T >::enqueue ( const T &  e)
inline

pushBack

205  {
206  pushBack(e);
207  }
void pushBack(const T &e)
Definition: Queue.h:191

+ Here is the call graph for this function:

template<class T >
void G3D::Queue< T >::fastClear ( )
inline

Clear without freeing the underlying array.

277  {
278  clear(false);
279  }
void clear(bool freeStorage=true)
Definition: Queue.h:253

+ Here is the call graph for this function:

template<class T >
int G3D::Queue< T >::index ( int  i) const
inlineprivate

Computes a data array index from a queue position. The queue position may be negative.

100  {
101  return (head + i + numAllocated) % numAllocated;
102  }
int numAllocated
Definition: Queue.h:73
int head
Definition: Queue.h:63

+ Here is the caller graph for this function:

template<class T >
const T& G3D::Queue< T >::last ( ) const
inline

Returns the back element

322  {
323  return (*this)[size() - 1];
324  }
int size() const
Definition: Queue.h:293

+ Here is the call graph for this function:

template<class T >
T& G3D::Queue< T >::last ( )
inline
326  {
327  return (*this)[size() - 1];
328  }
int size() const
Definition: Queue.h:293

+ Here is the call graph for this function:

template<class T >
int G3D::Queue< T >::length ( ) const
inline

Number of elements in the queue.

300  {
301  return size();
302  }
int size() const
Definition: Queue.h:293

+ Here is the call graph for this function:

template<class T >
Queue& G3D::Queue< T >::operator= ( const Queue< T > &  other)
inline

Assignment operator.

284  {
285  clear();
286  _copy(other);
287  return *this;
288  }
void clear(bool freeStorage=true)
Definition: Queue.h:253
void _copy(const Queue &other)
Definition: Queue.h:76

+ Here is the call graph for this function:

template<class T >
T& G3D::Queue< T >::operator[] ( int  n)
inline

Performs bounds checks in debug mode

307  {
308  debugAssert((n >= 0) && (n < num));
309  return data[index(n)];
310  }
T * data
Definition: Queue.h:58
#define debugAssert(exp)
Definition: debugAssert.h:160
int num
Definition: Queue.h:68
int index(int i) const
Definition: Queue.h:100

+ Here is the call graph for this function:

template<class T >
const T& G3D::Queue< T >::operator[] ( int  n) const
inline

Performs bounds checks in debug mode

315  {
316  debugAssert((n >= 0) && (n < num));
317  return data[index(n)];
318  }
T * data
Definition: Queue.h:58
#define debugAssert(exp)
Definition: debugAssert.h:160
int num
Definition: Queue.h:68
int index(int i) const
Definition: Queue.h:100

+ Here is the call graph for this function:

template<class T >
T G3D::Queue< T >::popBack ( )
inline

Remove the last element from the queue. The queue will never shrink in size. (A typical queue only uses popFront).

214  {
215  int tail = index(num - 1);
216  T result(data[tail]);
217 
218  // Call the destructor
219  (data + tail)->~T();
220  --num;
221 
222  return result;
223  }
T * data
Definition: Queue.h:58
int num
Definition: Queue.h:68
int index(int i) const
Definition: Queue.h:100

+ Here is the call graph for this function:

template<class T >
T G3D::Queue< T >::popFront ( )
inline

Remove the next element from the head of the queue. The queue will never shrink in size.

228  {
229  T result(data[head]);
230  // Call the destructor
231  (data + head)->~T();
232  head = (head + 1) % numAllocated;
233  --num;
234  return result;
235  }
T * data
Definition: Queue.h:58
int numAllocated
Definition: Queue.h:73
int num
Definition: Queue.h:68
int head
Definition: Queue.h:63

+ Here is the caller graph for this function:

template<class T >
void G3D::Queue< T >::pushBack ( const T &  e)
inline

Insert a new element at the end of the queue.

191  {
192  reserveSpace();
193 
194  // Get the index of 1+tail
195  int i = index(num);
196 
197  // Initialize that element
198  new (data + i)T(e);
199  ++num;
200  }
void reserveSpace()
Definition: Queue.h:136
T * data
Definition: Queue.h:58
int num
Definition: Queue.h:68
int index(int i) const
Definition: Queue.h:100

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T >
void G3D::Queue< T >::pushFront ( const T &  e)
inline

Insert a new element into the front of the queue (a traditional queue only uses pushBack).

174  {
175  reserveSpace();
176 
177  // Get the index of head-1
178  int i = index(-1);
179 
180  // Call the constructor on the newly exposed element.
181  new (data + i)T(e);
182 
183  // Reassign the head to point to this index
184  head = i;
185  ++num;
186  }
void reserveSpace()
Definition: Queue.h:136
T * data
Definition: Queue.h:58
int num
Definition: Queue.h:68
int index(int i) const
Definition: Queue.h:100
int head
Definition: Queue.h:63

+ Here is the call graph for this function:

template<class T >
void G3D::Queue< T >::repackAndRealloc ( int  newSize)
inlineprivate

Allocates newSize elements and repacks the array.

107  {
108  // TODO: shrink queue
109  T* old = data;
110  data = (T*)System::malloc(newSize * sizeof(T));
111  debugAssert(data != NULL);
112 
113  FIND_ENDS;
114 
115  int j = 0;
116  for (int i = head; i < firstEnd; ++i, ++j) {
117  new (data + j)T(old[i]);
118  (old + i)->~T();
119  }
120 
121  for (int i = 0; i < secondEnd; ++i, ++j) {
122  new (data + j)T(old[i]);
123  (old + i)->~T();
124  }
125 
126  head = 0;
127  System::free(old);
128  numAllocated = newSize;
129  }
#define FIND_ENDS
Definition: Queue.h:29
arena_t NULL
Definition: jemalloc_internal.h:624
T * data
Definition: Queue.h:58
static void * malloc(size_t bytes)
Definition: System.cpp:1441
int numAllocated
Definition: Queue.h:73
#define debugAssert(exp)
Definition: debugAssert.h:160
static void free(void *p)
Definition: System.cpp:1473
int head
Definition: Queue.h:63

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T >
void G3D::Queue< T >::reserveSpace ( )
inlineprivate

Ensure that there is at least one element between the tail and head, wrapping around in the circular buffer.

136  {
137  if (num == numAllocated) {
138  repackAndRealloc(numAllocated * 3 + 20);
139  }
140  }
void repackAndRealloc(int newSize)
Definition: Queue.h:107
int numAllocated
Definition: Queue.h:73
int num
Definition: Queue.h:68

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T >
int G3D::Queue< T >::size ( ) const
inline

Number of elements in the queue.

293  {
294  return num;
295  }
int num
Definition: Queue.h:68

+ Here is the caller graph for this function:

Member Data Documentation

template<class T >
T* G3D::Queue< T >::data
private

Only num elements are initialized.

template<class T >
int G3D::Queue< T >::head
private

Index of the next element to be dequeue-d in data.

template<class T >
int G3D::Queue< T >::num
private

Number of elements (including head) that are visible and initialized.

template<class T >
int G3D::Queue< T >::numAllocated
private

Size of data array in elements.


The documentation for this class was generated from the following file: