From edcf891e47f5c80e802428956cbc88ab802758c6 Mon Sep 17 00:00:00 2001 From: Paul Bergeron Date: Sat, 27 Feb 2016 13:35:50 -0800 Subject: [PATCH] Add ContentSize() to cheaply calculate the size of the ring buffer --- ring.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ring.go b/ring.go index 2214c4b..3564555 100644 --- a/ring.go +++ b/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. */