Skip to content

Commit a9ab17f

Browse files
committed
Fix MutableBinaryHeap{T} when T is Union/abstract
Each MutableBinaryHeapNode needs to be fully parameterised or Julia will throw a conversion error when trying to add them to MutableBinaryHeap's nodes field.
1 parent c4b8b5f commit a9ab17f

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

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
@@ -260,7 +260,7 @@ function update!(h::MutableBinaryHeap{T}, i::Int, v) where T
260260
nd_id = nodemap[i]
261261
v0 = nodes[nd_id].value
262262
x = convert(T, v)
263-
nodes[nd_id] = MutableBinaryHeapNode(x, i)
263+
nodes[nd_id] = MutableBinaryHeapNode{T}(x, i)
264264
if Base.lt(ordering, x, v0)
265265
_heap_bubble_up!(ordering, nodes, nodemap, nd_id)
266266
else

test/test_mutable_binheap.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,14 @@ end
342342
update!(h, 2, 20)
343343
@test isequal(heap_values(h), [0.5, 10.1, 3.0, 20.0])
344344
end
345+
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
345355
end # @testset MutableBinheap

0 commit comments

Comments
 (0)