Add ContentSize() to cheaply calculate the size of the ring buffer
This commit is contained in:
committed by
Dolf Schimmel (Freeaqingme)
parent
17637388c9
commit
edcf891e47
15
ring.go
15
ring.go
@@ -35,6 +35,21 @@ func (r Ring) Capacity() int {
|
||||
return len(r.buff)
|
||||
}
|
||||
|
||||
/*
|
||||
ContentSize returns the current number of elements inside the ring buffer.
|
||||
*/
|
||||
func (r *Ring) ContentSize() int {
|
||||
if r.head == -1 {
|
||||
return 0
|
||||
} else {
|
||||
difference := (r.head - r.tail)
|
||||
if difference < 0 {
|
||||
difference = -difference
|
||||
}
|
||||
return difference + 1
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Enqueue a value into the Ring buffer.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user