CMS 3D CMS Logo

CircularBuffer.h
Go to the documentation of this file.
1 #ifndef L1Trigger_TrackFindingTracklet_interface_CircularBuffer_h
2 #define L1Trigger_TrackFindingTracklet_interface_CircularBuffer_h
3 
4 #include <cassert>
5 #include <vector>
6 
7 namespace trklet {
8 
9  template <class T>
11  public:
12  CircularBuffer(unsigned int nbits) {
13  size_ = 1 << nbits;
14  buffer_.resize(size_);
15  reset();
16  }
17 
18  ~CircularBuffer() = default;
19 
20  void reset() {
21  rptr_ = 0;
22  wptr_ = 0;
23  }
24 
25  //Full if writer ptr incremented is same as read ptr
26  bool full() const { return ((wptr_ + 1) % size_) == rptr_; }
27 
28  //Almost full if writer ptr incremented by 1 or 2 is same as read ptr
29  bool almostfull() const { return (((wptr_ + 1) % size_) == rptr_) || (((wptr_ + 2) % size_) == rptr_); }
30 
31  //Empty buffer is write ptr is same as read ptr
32  bool empty() const { return wptr_ == rptr_; }
33 
34  const T& read() {
35  assert(!empty());
36  unsigned int oldrptr = rptr_;
37  rptr_ = (rptr_ + 1) % size_;
38  return buffer_[oldrptr];
39  }
40 
41  const T& peek() const {
42  assert(!empty());
43  return buffer_[rptr_];
44  }
45 
46  void store(T element) {
47  assert(!full());
48  buffer_[wptr_++] = element;
49  wptr_ = wptr_ % size_;
50  }
51 
52  private:
53  std::vector<T> buffer_;
54 
55  //buffer size
56  unsigned int size_;
57 
58  //read and write poiters into buffer
59  unsigned int rptr_;
60  unsigned int wptr_;
61  };
62 }; // namespace trklet
63 #endif
trklet::CircularBuffer::size_
unsigned int size_
Definition: CircularBuffer.h:56
trklet::CircularBuffer::peek
const T & peek() const
Definition: CircularBuffer.h:41
trklet::CircularBuffer::reset
void reset()
Definition: CircularBuffer.h:20
trklet::CircularBuffer::full
bool full() const
Definition: CircularBuffer.h:26
trklet::CircularBuffer::read
const T & read()
Definition: CircularBuffer.h:34
trklet::CircularBuffer::~CircularBuffer
~CircularBuffer()=default
cms::cuda::assert
assert(be >=bs)
trklet::CircularBuffer::CircularBuffer
CircularBuffer(unsigned int nbits)
Definition: CircularBuffer.h:12
trklet::CircularBuffer
Definition: CircularBuffer.h:10
trklet::CircularBuffer::almostfull
bool almostfull() const
Definition: CircularBuffer.h:29
trklet::CircularBuffer::store
void store(T element)
Definition: CircularBuffer.h:46
trklet::CircularBuffer::rptr_
unsigned int rptr_
Definition: CircularBuffer.h:59
trklet
Definition: AllProjectionsMemory.h:9
T
long double T
Definition: Basic3DVectorLD.h:48
trklet::CircularBuffer::wptr_
unsigned int wptr_
Definition: CircularBuffer.h:60
trklet::CircularBuffer::buffer_
std::vector< T > buffer_
Definition: CircularBuffer.h:53
trklet::CircularBuffer::empty
bool empty() const
Definition: CircularBuffer.h:32