-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathring.go
30 lines (26 loc) · 866 Bytes
/
ring.go
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
package main
// byteRing is a simple ring queue
type byteRing struct {
n uint // capacity of the ring buffer
ring []byte // the ring buffer itself
i uint // current index in the ring buffer
}
// NewbyteRing creates a new byteRing with a specified capacity.
func NewbyteRing(capacity uint) *byteRing {
return &byteRing{n: capacity, i: 0, ring: make([]byte, capacity)}
}
// Insert inserts a byte into the ring buffer at the current index.
// If the buffer is full, it overwrites the oldest entry.
func (r *byteRing) Insert(b byte) {
r.ring[r.i] = b
r.i = (r.i + 1) % r.n
}
// GetHistory returns a copy of the current state of the ring buffer,
// with the most recently inserted byte first.
func (r *byteRing) GetHistory() []byte {
hist := make([]byte, r.n)
for j := uint(0); j < r.n; j++ {
hist[j] = r.ring[(r.i+r.n-j-1)%r.n]
}
return hist
}