-
Notifications
You must be signed in to change notification settings - Fork 247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimisation: Use heapify in MutableBinaryHeap #712
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,11 +52,12 @@ end | |
|
||
@testset "MutableBinheap" begin | ||
|
||
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7] | ||
vs2 = collect(enumerate(vs)) | ||
ordering = Base.Order.By(last) | ||
|
||
@testset "construct heap" begin | ||
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7] | ||
vs2 = collect(enumerate(vs)) | ||
|
||
MutableBinaryHeap{Int, Base.ForwardOrdering}() | ||
MutableBinaryHeap{Int, Base.ForwardOrdering}(vs) | ||
|
||
|
@@ -88,6 +89,7 @@ end | |
end | ||
|
||
@testset "make mutable binary minheap" begin | ||
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7] | ||
h = MutableBinaryMinHeap(vs) | ||
|
||
@test length(h) == 10 | ||
|
@@ -99,17 +101,20 @@ end | |
end | ||
|
||
@testset "make mutable binary maxheap" begin | ||
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7] | ||
h = MutableBinaryMaxHeap(vs) | ||
|
||
@test length(h) == 10 | ||
@test !isempty(h) | ||
@test first(h) == 16 | ||
@test isequal(list_values(h), vs) | ||
@test isequal(heap_values(h), [16, 14, 10, 8, 7, 3, 9, 1, 4, 2]) | ||
@test isequal(heap_values(h), [16, 14, 10, 8, 7, 9, 3, 2, 4, 1]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is just due to the difference in the implementation of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do these both follow the heap rule about being larger than children? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, they both are valid heaps. I also confirmed it using this function here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems fine then |
||
@test sizehint!(h, 100) === h | ||
end | ||
|
||
@testset "make mutable binary custom ordering heap" begin | ||
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7] | ||
vs2 = collect(enumerate(vs)) | ||
h = MutableBinaryHeap(ordering, vs2) | ||
|
||
@test length(h) == 10 | ||
|
@@ -121,6 +126,7 @@ end | |
end | ||
|
||
@testset "hmin / push! / pop!" begin | ||
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a good change anyway. Testsets hould be indipendent. |
||
hmin = MutableBinaryMinHeap{Int}() | ||
@test length(hmin) == 0 | ||
@test isempty(hmin) | ||
|
@@ -152,6 +158,7 @@ end | |
end | ||
|
||
@testset "hmax / push! / pop!" begin | ||
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7] | ||
hmax = MutableBinaryMaxHeap{Int}() | ||
@test length(hmax) == 0 | ||
@test isempty(hmax) | ||
|
@@ -183,6 +190,8 @@ end | |
end | ||
|
||
@testset "Custom ordering push! / pop!" begin | ||
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7] | ||
vs2 = collect(enumerate(vs)) | ||
heap = MutableBinaryHeap{Tuple{Int,Int}}(ordering) | ||
@test length(heap) == 0 | ||
@test isempty(heap) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new implementation was overwriting
vs
. So, the tests were failing because of updatedvs
(The value ofvs
was not as expected because it was updated during some other test). So, I updated it to use separatevs
andvs2
for each test (the values are still the same).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not OK for
MutableBinaryMinHeap(vs)
to mutatevs
.We should add a
copy
to the constructor if that is the case.(we can consider later adding a
MutableBinaryMinHeap!
or something that does do so. but lets not worry about it in this PR)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
list_values
related tests (i.e., this one) started failing after adding copy in the constructor. the reason for that is becausenodemap
is not updated as I mentioned here. Those tests were not failing before becausevs
was being mutated.I am trying to think if there is an efficient way to update
nodemap
after theheapify!
call.It might be that not being able to update
nodemap
efficiently (with O(n) implementation) is the reason they implemented it in O(nlogn) in the first place.