Skip to content

Commit

Permalink
Merge pull request #680 from Firionus/fix-#678
Browse files Browse the repository at this point in the history
Fix #678 BoundsError in MutableBinaryHeap delete! with 3 elements
  • Loading branch information
oxinabox authored Sep 15, 2020
2 parents 1bdd474 + 36846c7 commit 355a7a8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DataStructures"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.18.5"
version = "0.18.6"


[deps]
Expand Down
9 changes: 5 additions & 4 deletions src/heaps/mutable_binary_heap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ function _binary_heap_pop!(ord::Ordering,
v = rt.value
@inbounds nodemap[rt.handle] = 0

if length(nodes) == 1
# clear
empty!(nodes)
# if node-to-remove is at end, we can just pop it
# the same applies to 1-element heaps that are empty after removing the last element
if nd_id == lastindex(nodes)
pop!(nodes)
else
# move the last node to the position of the removed node
# move the last node to the position of the node-to-remove
@inbounds nodes[nd_id] = new_rt = nodes[end]
pop!(nodes)
@inbounds nodemap[new_rt.handle] = nd_id
Expand Down
9 changes: 9 additions & 0 deletions test/test_mutable_binheap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,15 @@ end
@test isequal(heap_values(h), [1])
end

@testset "test delete! at end of 3-element heap" begin
h = MutableBinaryMinHeap{Int}()
push!(h, 1)
push!(h, 2)
handle = push!(h, 3)
delete!(h, handle)
@test isequal(heap_values(h), [1, 2])
end

@testset "test update! and top_with_handle" begin
for (hf,m) = [(MutableBinaryMinHeap,-2.0), (MutableBinaryMaxHeap,2.0)]
xs = rand(100)
Expand Down

2 comments on commit 355a7a8

@oxinabox
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/21446

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.18.6 -m "<description of version>" 355a7a8c3016808cc682a31f1b772d1b369cafc8
git push origin v0.18.6

Please sign in to comment.