GemaMesh
The GeMA Mesh Plugin
Loading...
Searching...
No Matches
uibhmQueue.h
Go to the documentation of this file.
1/************************************************************************
2**
3** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4** All rights reserved.
5**
6** This file is part of the "GeMA" software. It's use should respect
7** the terms in the license agreement that can be found together
8** with this source code.
9** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11** A PARTICULAR PURPOSE.
12**
13************************************************************************/
14
24#ifndef _UIBHM_QUEUE_H_
25#define _UIBHM_QUEUE_H_
26
27#include <QVector>
28#include <assert.h>
29
30
37template <class T> class UibhmQueue
38{
39public:
40 UibhmQueue() : _data(16), _head(0), _tail(0), _size(0) {}
41
42 bool empty() const { return !_size; }
43 int size() const { return _size; }
44
45 void enqueue(const T& value)
46 {
47 grow();
48 _data[_tail] = value;
49 _tail = (_tail + 1) % _data.size();
50 _size++;
51 }
52
53 T dequeue()
54 {
55 assert(!empty());
56 T value = _data[_head];
57 _head = (_head + 1) % _data.size();
58 _size--;
59 return value;
60 }
61
62private:
63 void grow() {
64 if(_size < _data.size())
65 return;
66 QVector<T> newData(_size * 2);
67
68 for(int i = 0; i < _size; i++)
69 newData[i] = _data[(_head + i) % _size];
70
71 _data.swap(newData);
72 _head = 0;
73 _tail = _size;
74 }
75
77 int _head;
78 int _tail;
79 int _size;
80};
81
82
83#endif
A helper queue class implemented over a QVector with amortized O(1) enqueue and dequeue operations....
Definition uibhmQueue.h:38
int _head
Index of the front of the queue (first used)
Definition uibhmQueue.h:77
QVector< T > _data
The underlying vector.
Definition uibhmQueue.h:76
int _size
Number of elements in the queue.
Definition uibhmQueue.h:79
int _tail
Index of the first empty in the back after used entries (wraps around the vector border).
Definition uibhmQueue.h:78
int size() const const
void swap(QVector< T > &other)