|
|
@ -27,14 +27,13 @@ func (r *Ring) Add(v any) {
|
|
|
|
r.lock.Lock()
|
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
ringLength := len(r.elements)
|
|
|
|
rlen := len(r.elements)
|
|
|
|
|
|
|
|
r.elements[r.index%rlen] = v
|
|
|
|
r.elements[r.index%ringLength] = v
|
|
|
|
|
|
|
|
r.index++
|
|
|
|
r.index++
|
|
|
|
|
|
|
|
|
|
|
|
// prevent ring index overflow
|
|
|
|
// prevent ring index overflow
|
|
|
|
if r.index/ringLength >= 2 {
|
|
|
|
if r.index >= rlen<<1 {
|
|
|
|
r.index = r.index - ringLength
|
|
|
|
r.index -= rlen
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -45,18 +44,18 @@ func (r *Ring) Take() []any {
|
|
|
|
|
|
|
|
|
|
|
|
var size int
|
|
|
|
var size int
|
|
|
|
var start int
|
|
|
|
var start int
|
|
|
|
ringLength := len(r.elements)
|
|
|
|
rlen := len(r.elements)
|
|
|
|
|
|
|
|
|
|
|
|
if r.index > ringLength {
|
|
|
|
if r.index > rlen {
|
|
|
|
size = ringLength
|
|
|
|
size = rlen
|
|
|
|
start = r.index % ringLength
|
|
|
|
start = r.index % rlen
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
size = r.index
|
|
|
|
size = r.index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
elements := make([]any, size)
|
|
|
|
elements := make([]any, size)
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
elements[i] = r.elements[(start+i)%ringLength]
|
|
|
|
elements[i] = r.elements[(start+i)%rlen]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return elements
|
|
|
|
return elements
|
|
|
|