Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Frame struct {
- frameId int
- data []byte
- untouched bool
- }
- type CircularBuffer struct {
- maxLen int
- buffer []Frame
- head int
- tail int
- }
- type CircularBufferAPI interface {
- IsEmpty() bool
- IsFull()
- Enqueue(Frame)
- Dequeue()
- Front() Frame
- Show() []Frame
- }
- func CBInit(maxLen int) *CircularBuffer {
- return &CircularBuffer{
- maxLen: maxLen,
- buffer: make([]Frame, maxLen),
- head: 0,
- tail: 0,
- }
- }
- func (cb *CircularBuffer) IsEmpty() bool {
- return cb.head == cb.tail
- }
- func (cb *CircularBuffer) IsFull() bool {
- return cb.tail == (cb.head-1)%cb.maxLen
- }
- func (cb *CircularBuffer) Enqueue(frame Frame) Frame {
- if !cb.IsFull() {
- item := cb.buffer[cb.tail]
- cb.buffer[cb.tail] = frame
- cb.tail = (cb.tail + 1) % cb.maxLen
- return item
- }
- return Frame{}
- }
- func (cb *CircularBuffer) Dequeue() Frame {
- if !cb.IsEmpty() {
- item := cb.buffer[cb.head]
- hollowFrame := Frame{frameId: 0, data: []byte{}}
- cb.buffer[cb.head] = hollowFrame
- cb.head = (cb.head + 1) % cb.maxLen
- return item
- }
- return Frame{}
- }
- func (cb *CircularBuffer) Front() Frame {
- return cb.buffer[cb.head]
- }
- func (cb *CircularBuffer) Show() []Frame {
- return cb.buffer
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement