Skip to content

Commit

Permalink
Add Clear method to v1 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
invisiblefunnel committed Sep 22, 2023
1 parent db06055 commit 16d16e9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions flatqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,9 @@ func (q *FlatQueue) Peek() int {
func (q *FlatQueue) PeekValue() float64 {
return q.values[0]
}

func (q *FlatQueue) Clear() {
q.length = 0
q.ids = q.ids[:0]
q.values = q.values[:0]
}
34 changes: 34 additions & 0 deletions flatqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,40 @@ func TestPopEmpty(t *testing.T) {
New().Pop()
}

func TestClear(t *testing.T) {
var q FlatQueue

q.Clear() // ok to clear empty queue

q.Push(1, 1)
q.Push(2, 2)

if q.Len() != 2 {
t.Fatal()
}

q.Clear()

if q.Len() != 0 {
t.Fatal()
}

q.Push(3, 3)
q.Push(4, 4)

if q.Pop() != 3 {
t.Fatal()
}

if q.Pop() != 4 {
t.Fatal()
}

if q.Len() != 0 {
t.Fatal()
}
}

func BenchmarkPush(b *testing.B) {
q := New()

Expand Down

0 comments on commit 16d16e9

Please sign in to comment.