CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
FQueue.h
Go to the documentation of this file.
1 #ifndef COMMONTOOLS_RECOALGOS_FQUEUE_H
2 #define COMMONTOOLS_RECOALGOS_FQUEUE_H
3 
4 #include <vector>
5 
6 template <class T>
7 class FQueue {
8 public:
9  FQueue() {
10  theSize = 0;
11  theFront = 0;
12  theTail = 0;
13  theCapacity = 0;
14  }
15 
16  FQueue(unsigned int initialCapacity) {
17  theBuffer.resize(initialCapacity);
18  theSize = 0;
19  theFront = 0;
20  theTail = 0;
21  theCapacity = initialCapacity;
22  }
23 
24  unsigned int size() const { return theSize; }
25 
26  bool empty() const { return theSize == 0; }
27 
28  T front() const { return theBuffer[theFront]; }
29 
30  T& tail() { return theBuffer[theTail]; }
31 
32  constexpr unsigned int wrapIndex(unsigned int i) { return i & (theBuffer.size() - 1); }
33 
34  void push_back(const T& value) {
35  if (theSize >= theCapacity) {
36  theBuffer.resize(theCapacity * 2);
37  if (theFront != 0) {
38  std::copy(theBuffer.begin(), theBuffer.begin() + theTail, theBuffer.begin() + theCapacity);
39 
40  theTail += theSize;
41 
42  } else {
44  }
45  theCapacity *= 2;
46  }
48  theTail = wrapIndex(theTail + 1);
49  theSize++;
50  }
51 
52  void pop_front() {
53  if (theSize > 0) {
55  theSize--;
56  }
57  }
58 
59  void pop_front(const unsigned int numberOfElementsToPop) {
60  unsigned int elementsToErase = theSize > numberOfElementsToPop ? numberOfElementsToPop : theSize;
61  theSize -= elementsToErase;
62  theFront = wrapIndex(theFront + elementsToErase);
63  }
64 
65  void reserve(unsigned int capacity) { theBuffer.reserve(capacity); }
66 
67  T& operator[](unsigned int index) { return theBuffer[wrapIndex(theFront + index)]; }
68 
69  void clear() {
70  theBuffer.clear();
71  theSize = 0;
72  theFront = 0;
73  theTail = 0;
74  }
75 
76 private:
77  unsigned int theSize;
78  unsigned int theFront;
79  unsigned int theTail;
80  std::vector<T> theBuffer;
81  unsigned int theCapacity;
82 };
83 
84 #endif
unsigned int theTail
Definition: FQueue.h:79
FQueue()
Definition: FQueue.h:9
constexpr unsigned int wrapIndex(unsigned int i)
Definition: FQueue.h:32
unsigned int theFront
Definition: FQueue.h:78
void clear()
Definition: FQueue.h:69
std::vector< T > theBuffer
Definition: FQueue.h:80
Definition: FQueue.h:7
unsigned int size() const
Definition: FQueue.h:24
T & operator[](unsigned int index)
Definition: FQueue.h:67
T front() const
Definition: FQueue.h:28
FQueue(unsigned int initialCapacity)
Definition: FQueue.h:16
void push_back(const T &value)
Definition: FQueue.h:34
bool empty() const
Definition: FQueue.h:26
void reserve(unsigned int capacity)
Definition: FQueue.h:65
unsigned int theCapacity
Definition: FQueue.h:81
T & tail()
Definition: FQueue.h:30
void pop_front(const unsigned int numberOfElementsToPop)
Definition: FQueue.h:59
long double T
void pop_front()
Definition: FQueue.h:52
unsigned int theSize
Definition: FQueue.h:77