Skip to content

Commit 355a7a8

Browse files
authored
Merge pull request #680 from Firionus/fix-#678
Fix #678 BoundsError in MutableBinaryHeap delete! with 3 elements
2 parents 1bdd474 + 36846c7 commit 355a7a8

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DataStructures"
22
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
3-
version = "0.18.5"
3+
version = "0.18.6"
44

55

66
[deps]

src/heaps/mutable_binary_heap.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,12 @@ function _binary_heap_pop!(ord::Ordering,
108108
v = rt.value
109109
@inbounds nodemap[rt.handle] = 0
110110

111-
if length(nodes) == 1
112-
# clear
113-
empty!(nodes)
111+
# if node-to-remove is at end, we can just pop it
112+
# the same applies to 1-element heaps that are empty after removing the last element
113+
if nd_id == lastindex(nodes)
114+
pop!(nodes)
114115
else
115-
# move the last node to the position of the removed node
116+
# move the last node to the position of the node-to-remove
116117
@inbounds nodes[nd_id] = new_rt = nodes[end]
117118
pop!(nodes)
118119
@inbounds nodemap[new_rt.handle] = nd_id

test/test_mutable_binheap.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,15 @@ end
266266
@test isequal(heap_values(h), [1])
267267
end
268268

269+
@testset "test delete! at end of 3-element heap" begin
270+
h = MutableBinaryMinHeap{Int}()
271+
push!(h, 1)
272+
push!(h, 2)
273+
handle = push!(h, 3)
274+
delete!(h, handle)
275+
@test isequal(heap_values(h), [1, 2])
276+
end
277+
269278
@testset "test update! and top_with_handle" begin
270279
for (hf,m) = [(MutableBinaryMinHeap,-2.0), (MutableBinaryMaxHeap,2.0)]
271280
xs = rand(100)

0 commit comments

Comments
 (0)