Skip to content

Commit

Permalink
Fix PQ popping elements on insert when no size limit is specified (#1)
Browse files Browse the repository at this point in the history
minkezhang authored Aug 17, 2022
1 parent 6724a78 commit c30826f
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pq/pq.go
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@ func (pq *PQ[T]) Push(p T, priority float64) {
p: p,
weight: priority,
})
for !pq.Empty() && pq.Len() > pq.n {
for !pq.Empty() && pq.Len() > pq.n && pq.n > 0 {
pq.Pop()
}
}
6 changes: 4 additions & 2 deletions pq/pq_test.go
Original file line number Diff line number Diff line change
@@ -47,15 +47,17 @@ func TestPQ(t *testing.T) {
},
},
{
name: "Trivial/NoSize",
name: "Trivial/NoSizeLimit",
data: []item{
{
p: X{1},
priority: 0,
},
},
size: 0,
want: nil,
want: []X{
X{1},
},
},

{

0 comments on commit c30826f

Please sign in to comment.