Skip to content

Commit

Permalink
Merge pull request #13 from gammazero/master
Browse files Browse the repository at this point in the history
Remove() returns item removed.
  • Loading branch information
eapache authored Aug 5, 2016
2 parents f0262ae + 9036dda commit 44cc805
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 5 additions & 3 deletions queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -97,4 +98,5 @@ func (q *Queue) Remove() {
if len(q.buf) > minQueueLen && (q.count<<2) == len(q.buf) {
q.resize()
}
return ret
}
5 changes: 4 additions & 1 deletion queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down

0 comments on commit 44cc805

Please sign in to comment.