Skip to content

Commit 44cc805

Browse files
authored
Merge pull request #13 from gammazero/master
Remove() returns item removed.
2 parents f0262ae + 9036dda commit 44cc805

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

queue.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@ func (q *Queue) Get(i int) interface{} {
8383
return q.buf[(q.head+i)&(len(q.buf)-1)]
8484
}
8585

86-
// Remove removes the element from the front of the queue. If you actually
87-
// want the element, call Peek first. This call panics if the queue is empty.
88-
func (q *Queue) Remove() {
86+
// Remove removes and returns the element from the front of the queue. If the
87+
// queue is empty, the call will panic.
88+
func (q *Queue) Remove() interface{} {
8989
if q.count <= 0 {
9090
panic("queue: Remove() called on empty queue")
9191
}
92+
ret := q.buf[q.head]
9293
q.buf[q.head] = nil
9394
// bitwise modulus
9495
q.head = (q.head + 1) & (len(q.buf) - 1)
@@ -97,4 +98,5 @@ func (q *Queue) Remove() {
9798
if len(q.buf) > minQueueLen && (q.count<<2) == len(q.buf) {
9899
q.resize()
99100
}
101+
return ret
100102
}

queue_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ func TestQueueSimple(t *testing.T) {
1212
if q.Peek().(int) != i {
1313
t.Error("peek", i, "had value", q.Peek())
1414
}
15-
q.Remove()
15+
x := q.Remove()
16+
if x != i {
17+
t.Error("remove", i, "had value", x)
18+
}
1619
}
1720
}
1821

0 commit comments

Comments
 (0)