From 9036dda23ebf78012566e19d9c8413e58880f05f Mon Sep 17 00:00:00 2001 From: Andrew Gillis Date: Thu, 4 Aug 2016 12:18:22 -0400 Subject: [PATCH] Remove() returns item removed. --- queue.go | 8 +++++--- queue_test.go | 5 ++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/queue.go b/queue.go index 3964acd..71d1acd 100644 --- a/queue.go +++ b/queue.go @@ -83,12 +83,13 @@ func (q *Queue) Get(i int) interface{} { return q.buf[(q.head+i)&(len(q.buf)-1)] } -// Remove removes the element from the front of the queue. If you actually -// want the element, call Peek first. This call panics if the queue is empty. -func (q *Queue) Remove() { +// Remove removes and returns the element from the front of the queue. If the +// queue is empty, the call will panic. +func (q *Queue) Remove() interface{} { if q.count <= 0 { panic("queue: Remove() called on empty queue") } + ret := q.buf[q.head] q.buf[q.head] = nil // bitwise modulus q.head = (q.head + 1) & (len(q.buf) - 1) @@ -97,4 +98,5 @@ func (q *Queue) Remove() { if len(q.buf) > minQueueLen && (q.count<<2) == len(q.buf) { q.resize() } + return ret } diff --git a/queue_test.go b/queue_test.go index 213c3c7..a875848 100644 --- a/queue_test.go +++ b/queue_test.go @@ -12,7 +12,10 @@ func TestQueueSimple(t *testing.T) { if q.Peek().(int) != i { t.Error("peek", i, "had value", q.Peek()) } - q.Remove() + x := q.Remove() + if x != i { + t.Error("remove", i, "had value", x) + } } }