CMS 3D CMS Logo

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 {
9  public:
11  {
12  theSize = 0;
13  theFront = 0;
14  theTail = 0;
15  theCapacity = 0;
16  }
17 
18  FQueue(unsigned int initialCapacity)
19  {
20  theBuffer.resize(initialCapacity);
21  theSize = 0;
22  theFront = 0;
23  theTail = 0;
24  theCapacity = initialCapacity;
25  }
26 
27  unsigned int size() const
28  {
29  return theSize;
30  }
31 
32  bool empty() const
33  {
34  return theSize == 0;
35  }
36 
37  T front() const
38  {
39  return theBuffer[theFront];
40  }
41 
42  T & tail()
43  {
44  return theBuffer[theTail];
45  }
46 
47  constexpr unsigned int wrapIndex(unsigned int i)
48  {
49  return i & (theBuffer.size() - 1);
50  }
51 
52  void push_back(const T & value)
53  {
54  if (theSize >= theCapacity)
55  {
56  theBuffer.resize(theCapacity * 2);
57  if (theFront != 0)
58  {
59  std::copy(theBuffer.begin(), theBuffer.begin() + theTail,
60  theBuffer.begin() + theCapacity);
61 
62  theTail += theSize;
63 
64  }
65  else
66  {
68  }
69  theCapacity *= 2;
70  }
72  theTail = wrapIndex(theTail + 1);
73  theSize++;
74 
75  }
76 
77  void pop_front()
78  {
79  if (theSize > 0)
80  {
82  theSize--;
83  }
84  }
85 
86  void pop_front(const unsigned int numberOfElementsToPop)
87  {
88  unsigned int elementsToErase =
89  theSize > numberOfElementsToPop ? numberOfElementsToPop : theSize;
90  theSize -= elementsToErase;
91  theFront = wrapIndex(theFront + elementsToErase);
92  }
93 
94  void reserve(unsigned int capacity)
95  {
96  theBuffer.reserve(capacity);
97  }
98 
99  T & operator[](unsigned int index)
100  {
101  return theBuffer[wrapIndex(theFront + index)];
102  }
103 
104  void clear()
105  {
106  theBuffer.clear();
107  theSize = 0;
108  theFront = 0;
109  theTail = 0;
110  }
111 
112  private:
113  unsigned int theSize;
114  unsigned int theFront;
115  unsigned int theTail;
116  std::vector<T> theBuffer;
117  unsigned int theCapacity;
118 
119 };
120 
121 #endif
unsigned int theTail
Definition: FQueue.h:115
FQueue()
Definition: FQueue.h:10
def copy(args, dbName)
constexpr unsigned int wrapIndex(unsigned int i)
Definition: FQueue.h:47
unsigned int theFront
Definition: FQueue.h:114
void clear()
Definition: FQueue.h:104
#define constexpr
std::vector< T > theBuffer
Definition: FQueue.h:116
Definition: FQueue.h:7
unsigned int size() const
Definition: FQueue.h:27
T & operator[](unsigned int index)
Definition: FQueue.h:99
Definition: value.py:1
T front() const
Definition: FQueue.h:37
FQueue(unsigned int initialCapacity)
Definition: FQueue.h:18
void push_back(const T &value)
Definition: FQueue.h:52
bool empty() const
Definition: FQueue.h:32
void reserve(unsigned int capacity)
Definition: FQueue.h:94
unsigned int theCapacity
Definition: FQueue.h:117
T & tail()
Definition: FQueue.h:42
void pop_front(const unsigned int numberOfElementsToPop)
Definition: FQueue.h:86
long double T
void pop_front()
Definition: FQueue.h:77
unsigned int theSize
Definition: FQueue.h:113