Add ContentSize() to cheaply calculate the size of the ring buffer

This commit is contained in:
Paul Bergeron
2016-02-27 13:35:50 -08:00
committed by Dolf Schimmel (Freeaqingme)
parent 17637388c9
commit edcf891e47

15
ring.go
View File

@@ -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.
*/