Skip to content

Commit d56640d

Browse files
authored
Merge pull request #869 from cmcaine/fix-mutable-binheap-unions
Fix MutableBinaryHeap{T} when T is Union/abstract
2 parents b67c498 + d332e60 commit d56640d

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/heaps/mutable_binary_heap.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ function Base.push!(h::MutableBinaryHeap{T}, v) where T
220220
nodemap = h.node_map
221221
i = length(nodemap) + 1
222222
nd_id = length(nodes) + 1
223-
push!(nodes, MutableBinaryHeapNode(convert(T, v), i))
223+
push!(nodes, MutableBinaryHeapNode{T}(convert(T, v), i))
224224
push!(nodemap, nd_id)
225225
_heap_bubble_up!(h.ordering, nodes, nodemap, nd_id)
226226
return i
@@ -266,7 +266,7 @@ function update!(h::MutableBinaryHeap{T}, i::Int, v) where T
266266
nd_id = nodemap[i]
267267
v0 = nodes[nd_id].value
268268
x = convert(T, v)
269-
nodes[nd_id] = MutableBinaryHeapNode(x, i)
269+
nodes[nd_id] = MutableBinaryHeapNode{T}(x, i)
270270
if Base.lt(ordering, x, v0)
271271
_heap_bubble_up!(ordering, nodes, nodemap, nd_id)
272272
else

test/test_mutable_binheap.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,16 @@ end
343343
@test isequal(heap_values(h), [0.5, 10.1, 3.0, 20.0])
344344
end
345345

346+
@testset "T is a Union" begin
347+
h = MutableBinaryMinHeap{Union{Int, Float64}}()
348+
push!(h, 1)
349+
push!(h, 2.0)
350+
update!(h, 1, 1.5)
351+
update!(h, 2, 3)
352+
@test pop!(h) === 1.5
353+
@test pop!(h) === 3
354+
end
355+
346356
@testset "empty!" begin
347357
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]
348358
vs2 = collect(enumerate(vs))

0 commit comments

Comments
 (0)