forked from cms-sw/cmssw
-
Notifications
You must be signed in to change notification settings - Fork 5
/
CircularBuffer.h
73 lines (57 loc) · 1.82 KB
/
CircularBuffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef L1Trigger_TrackFindingTracklet_interface_CircularBuffer_h
#define L1Trigger_TrackFindingTracklet_interface_CircularBuffer_h
#include <cassert>
#include <vector>
namespace trklet {
template <class T>
class CircularBuffer {
public:
CircularBuffer(unsigned int nbits) {
size_ = 1 << nbits;
buffer_.resize(size_);
reset();
}
~CircularBuffer() = default;
void reset() {
rptr_ = 0;
wptr_ = 0;
}
//Full if writer ptr incremented is same as read ptr
bool full() const { return ((wptr_ + 1) % size_) == rptr_; }
//Almost full if writer ptr incremented by 1 or 2 is same as read ptr
bool almostfull() const { return (((wptr_ + 1) % size_) == rptr_) || (((wptr_ + 2) % size_) == rptr_); }
//near full if writer ptr incremented by 1, 2, or 3 is same as read ptr
bool nearfull() const {
return (((wptr_ + 1) % size_) == rptr_) || (((wptr_ + 2) % size_) == rptr_) || (((wptr_ + 3) % size_) == rptr_);
}
//Empty buffer is write ptr is same as read ptr
bool empty() const { return wptr_ == rptr_; }
const T& read() {
assert(!empty());
unsigned int oldrptr = rptr_;
rptr_ = (rptr_ + 1) % size_;
return buffer_[oldrptr];
}
const T& peek() const {
assert(!empty());
return buffer_[rptr_];
}
void store(T element) {
assert(!full());
buffer_[wptr_++] = element;
wptr_ = wptr_ % size_;
assert(wptr_ != rptr_);
}
//these are needed for comparison of emulation with HLS FW
unsigned int rptr() const { return rptr_; }
unsigned int wptr() const { return wptr_; }
private:
std::vector<T> buffer_;
//buffer size
unsigned int size_;
//read and write poiters into buffer
unsigned int rptr_;
unsigned int wptr_;
};
}; // namespace trklet
#endif