Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-07 22:33:27

0001 #ifndef L1Trigger_TrackFindingTracklet_interface_CircularBuffer_h
0002 #define L1Trigger_TrackFindingTracklet_interface_CircularBuffer_h
0003 
0004 #include <cassert>
0005 #include <vector>
0006 
0007 namespace trklet {
0008 
0009   template <class T>
0010   class CircularBuffer {
0011   public:
0012     CircularBuffer(unsigned int nbits) {
0013       size_ = 1 << nbits;
0014       buffer_.resize(size_);
0015       reset();
0016     }
0017 
0018     ~CircularBuffer() = default;
0019 
0020     void reset() {
0021       rptr_ = 0;
0022       wptr_ = 0;
0023     }
0024 
0025     //Full if writer ptr incremented is same as read ptr
0026     bool full() const { return ((wptr_ + 1) % size_) == rptr_; }
0027 
0028     //Almost full if writer ptr incremented by 1 or 2 is same as read ptr
0029     bool almostfull() const { return (((wptr_ + 1) % size_) == rptr_) || (((wptr_ + 2) % size_) == rptr_); }
0030 
0031     //near full if writer ptr incremented by 1, 2, or 3 is same as read ptr
0032     bool nearfull() const {
0033       return (((wptr_ + 1) % size_) == rptr_) || (((wptr_ + 2) % size_) == rptr_) || (((wptr_ + 3) % size_) == rptr_);
0034     }
0035 
0036     //Empty buffer is write ptr is same as read ptr
0037     bool empty() const { return wptr_ == rptr_; }
0038 
0039     const T& read() {
0040       assert(!empty());
0041       unsigned int oldrptr = rptr_;
0042       rptr_ = (rptr_ + 1) % size_;
0043       return buffer_[oldrptr];
0044     }
0045 
0046     const T& peek() const {
0047       assert(!empty());
0048       return buffer_[rptr_];
0049     }
0050 
0051     void store(T element) {
0052       assert(!full());
0053       buffer_[wptr_++] = element;
0054       wptr_ = wptr_ % size_;
0055       assert(wptr_ != rptr_);
0056     }
0057 
0058     //these are needed for comparison of emulation with HLS FW
0059     unsigned int rptr() const { return rptr_; }
0060     unsigned int wptr() const { return wptr_; }
0061 
0062   private:
0063     std::vector<T> buffer_;
0064 
0065     //buffer size
0066     unsigned int size_;
0067 
0068     //read and write poiters into buffer
0069     unsigned int rptr_;
0070     unsigned int wptr_;
0071   };
0072 };  // namespace trklet
0073 #endif