-
Notifications
You must be signed in to change notification settings - Fork 2
/
ring.go
49 lines (43 loc) · 793 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package workerpool
import (
"math/rand"
)
type ring[T any] struct {
buffer []T
start int
end int
Count int
}
func newRing[T any](capacity int) ring[T] {
return ring[T]{buffer: make([]T, capacity)}
}
func ringPush[T any](r *ring[T], v T) {
if r.Count >= len(r.buffer) {
panic("cannot push to full ring buffer")
}
r.buffer[r.end] = v
r.end++
if r.end == len(r.buffer) {
r.end = 0
}
r.Count++
}
func ringPop[T any](r *ring[T]) (T, bool) {
var zero T
if r.Count <= 0 {
return zero, false
}
v := r.buffer[r.start]
r.buffer[r.start] = zero
r.start++
if r.start == len(r.buffer) {
r.start = 0
}
r.Count--
return v, true
}
func (r *ring[T]) Shuffle() {
rand.Shuffle(len(r.buffer), func(i, j int) {
r.buffer[i], r.buffer[j] = r.buffer[j], r.buffer[i]
})
}