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  //near full if writer ptr incremented by 1, 2, or 3 is same as read ptr
32  bool nearfull() const {
33  return (((wptr_ + 1) % size_) == rptr_) || (((wptr_ + 2) % size_) == rptr_) || (((wptr_ + 3) % size_) == rptr_);
34  }
35 
36  //Empty buffer is write ptr is same as read ptr
37  bool empty() const { return wptr_ == rptr_; }
38 
39  const T& read() {
40  assert(!empty());
41  unsigned int oldrptr = rptr_;
42  rptr_ = (rptr_ + 1) % size_;
43  return buffer_[oldrptr];
44  }
45 
46  const T& peek() const {
47  assert(!empty());
48  return buffer_[rptr_];
49  }
50 
51  void store(T element) {
52  assert(!full());
53  buffer_[wptr_++] = element;
54  wptr_ = wptr_ % size_;
55  assert(wptr_ != rptr_);
56  }
57 
58  //these are needed for comparison of emulation with HLS FW
59  unsigned int rptr() const { return rptr_; }
60  unsigned int wptr() const { return wptr_; }
61 
62  private:
63  std::vector<T> buffer_;
64 
65  //buffer size
66  unsigned int size_;
67 
68  //read and write poiters into buffer
69  unsigned int rptr_;
70  unsigned int wptr_;
71  };
72 }; // namespace trklet
73 #endif
std::vector< T > buffer_
unsigned int rptr() const
unsigned int wptr() const
const T & peek() const
assert(be >=bs)
void store(T element)
long double T
CircularBuffer(unsigned int nbits)