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 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
FQueue::wrapIndex
constexpr unsigned int wrapIndex(unsigned int i)
Definition: FQueue.h:32
mps_fire.i
i
Definition: mps_fire.py:355
FQueue::pop_front
void pop_front(const unsigned int numberOfElementsToPop)
Definition: FQueue.h:59
filterCSVwithJSON.copy
copy
Definition: filterCSVwithJSON.py:36
FQueue::theBuffer
std::vector< T > theBuffer
Definition: FQueue.h:80
FQueue::theSize
unsigned int theSize
Definition: FQueue.h:77
FQueue::operator[]
T & operator[](unsigned int index)
Definition: FQueue.h:67
FQueue
Definition: FQueue.h:7
FQueue::theFront
unsigned int theFront
Definition: FQueue.h:78
FQueue::clear
void clear()
Definition: FQueue.h:69
FQueue::push_back
void push_back(const T &value)
Definition: FQueue.h:34
FQueue::front
T front() const
Definition: FQueue.h:28
FQueue::size
unsigned int size() const
Definition: FQueue.h:24
FQueue::theCapacity
unsigned int theCapacity
Definition: FQueue.h:81
FQueue::empty
bool empty() const
Definition: FQueue.h:26
value
Definition: value.py:1
FQueue::tail
T & tail()
Definition: FQueue.h:30
FQueue::pop_front
void pop_front()
Definition: FQueue.h:52
T
long double T
Definition: Basic3DVectorLD.h:48
relativeConstraints.value
value
Definition: relativeConstraints.py:53
FQueue::theTail
unsigned int theTail
Definition: FQueue.h:79
FQueue::FQueue
FQueue(unsigned int initialCapacity)
Definition: FQueue.h:16
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
FQueue::FQueue
FQueue()
Definition: FQueue.h:9
FQueue::reserve
void reserve(unsigned int capacity)
Definition: FQueue.h:65