Skip to content

Commit

Permalink
Merge pull request #472 from dillondaudert/update_heaps
Browse files Browse the repository at this point in the history
Update heap constructors
  • Loading branch information
oxinabox authored Jan 8, 2019
2 parents 26e0229 + 0d5db77 commit 5044489
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 49 deletions.
16 changes: 8 additions & 8 deletions docs/src/heaps.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ mutable binary heap (type `MutableBinaryHeap`) have been implemented.
Examples of constructing a heap:

```julia
h = binary_minheap(Int)
h = binary_maxheap(Int) # create an empty min/max binary heap of integers
h = BinaryMinHeap{Int}()
h = BinaryMaxHeap{Int}() # create an empty min/max binary heap of integers

h = binary_minheap([1,4,3,2])
h = binary_maxheap([1,4,3,2]) # create a min/max heap from a vector
h = BinaryMinHeap([1,4,3,2])
h = BinaryMaxHeap([1,4,3,2]) # create a min/max heap from a vector

h = mutable_binary_minheap(Int)
h = mutable_binary_maxheap(Int) # create an empty mutable min/max heap
h = MutableBinaryMinHeap{Int}()
h = MutableBinaryMaxHeap{Int}() # create an empty mutable min/max heap

h = mutable_binary_minheap([1,4,3,2])
h = mutable_binary_maxheap([1,4,3,2]) # create a mutable min/max heap from a vector
h = MutableBinaryMinHeap([1,4,3,2])
h = MutableBinaryMaxHeap([1,4,3,2]) # create a mutable min/max heap from a vector
```

## Min-max heaps
Expand Down
4 changes: 2 additions & 2 deletions src/DataStructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ module DataStructures
export IntDisjointSets, DisjointSets, num_groups, find_root, in_same_set, root_union!

export AbstractHeap, compare, extract_all!
export BinaryHeap, binary_minheap, binary_maxheap, nlargest, nsmallest
export MutableBinaryHeap, mutable_binary_minheap, mutable_binary_maxheap
export BinaryHeap, BinaryMinHeap, BinaryMaxHeap, nlargest, nsmallest
export MutableBinaryHeap, MutableBinaryMinHeap, MutableBinaryMaxHeap
export heapify!, heapify, heappop!, heappush!, isheap
export BinaryMinMaxHeap, popmin!, popmax!, popall!

Expand Down
2 changes: 1 addition & 1 deletion src/heaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function nextreme(comp::Comp, n::Int, arr::AbstractVector{T}) where {T, Comp}
return sort(arr, lt = (x, y) -> compare(comp, y, x))
end

buffer = BinaryHeap{T,Comp}(comp)
buffer = BinaryHeap{T,Comp}()

for i = 1 : n
@inbounds xi = arr[i]
Expand Down
31 changes: 17 additions & 14 deletions src/heaps/binary_heap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,26 @@ mutable struct BinaryHeap{T,Comp} <: AbstractHeap{T}
comparer::Comp
valtree::Vector{T}

function BinaryHeap{T,Comp}(comp::Comp) where {T,Comp}
new{T,Comp}(comp, Vector{T}())
end
BinaryHeap{T,Comp}() where {T,Comp} = new{T,Comp}(Comp(), Vector{T}())

function BinaryHeap{T,Comp}(comp::Comp, xs) where {T,Comp} # xs is an iterable collection of values
valtree = _make_binary_heap(comp, T, xs)
new{T,Comp}(comp, valtree)
function BinaryHeap{T,Comp}(xs::AbstractVector{T}) where {T,Comp}
valtree = _make_binary_heap(Comp(), T, xs)
new{T,Comp}(Comp(), valtree)
end
end

function binary_minheap(ty::Type{T}) where T
BinaryHeap{T,LessThan}(LessThan())
end

binary_maxheap(ty::Type{T}) where {T} = BinaryHeap{T,GreaterThan}(GreaterThan())
binary_minheap(xs::AbstractVector{T}) where {T} = BinaryHeap{T,LessThan}(LessThan(), xs)
binary_maxheap(xs::AbstractVector{T}) where {T} = BinaryHeap{T,GreaterThan}(GreaterThan(), xs)

const BinaryMinHeap{T} = BinaryHeap{T, LessThan}
const BinaryMaxHeap{T} = BinaryHeap{T, GreaterThan}

BinaryMinHeap(xs::AbstractVector{T}) where T = BinaryMinHeap{T}(xs)
BinaryMaxHeap(xs::AbstractVector{T}) where T = BinaryMaxHeap{T}(xs)

# deprecated constructors

@deprecate binary_minheap(::Type{T}) where {T} BinaryMinHeap{T}()
@deprecate binary_minheap(xs::AbstractVector{T}) where {T} BinaryMinHeap(xs)
@deprecate binary_maxheap(::Type{T}) where {T} BinaryMaxHeap{T}()
@deprecate binary_maxheap(xs::AbstractVector{T}) where {T} BinaryMaxHeap(xs)

#################################################
#
Expand Down
30 changes: 19 additions & 11 deletions src/heaps/mutable_binary_heap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,31 @@ mutable struct MutableBinaryHeap{VT, Comp} <: AbstractMutableHeap{VT,Int}
nodes::Vector{MutableBinaryHeapNode{VT}}
node_map::Vector{Int}

function MutableBinaryHeap{VT, Comp}(comp::Comp) where {VT, Comp}
function MutableBinaryHeap{VT, Comp}() where {VT, Comp}
nodes = Vector{MutableBinaryHeapNode{VT}}()
node_map = Vector{Int}()
new{VT, Comp}(comp, nodes, node_map)
new{VT, Comp}(Comp(), nodes, node_map)
end

function MutableBinaryHeap{VT, Comp}(comp::Comp, xs) where {VT, Comp} # xs is an iterable collection of values
nodes, node_map = _make_mutable_binary_heap(comp, VT, xs)
new{VT, Comp}(comp, nodes, node_map)
function MutableBinaryHeap{VT, Comp}(xs::AbstractVector{VT}) where {VT, Comp}
nodes, node_map = _make_mutable_binary_heap(Comp(), VT, xs)
new{VT, Comp}(Comp(), nodes, node_map)
end
end

mutable_binary_minheap(ty::Type{T}) where {T} = MutableBinaryHeap{T,LessThan}(LessThan())
mutable_binary_maxheap(ty::Type{T}) where {T} = MutableBinaryHeap{T,GreaterThan}(GreaterThan())

mutable_binary_minheap(xs::AbstractVector{T}) where {T} = MutableBinaryHeap{T,LessThan}(LessThan(), xs)
mutable_binary_maxheap(xs::AbstractVector{T}) where {T} = MutableBinaryHeap{T,GreaterThan}(GreaterThan(), xs)

const MutableBinaryMinHeap{T} = MutableBinaryHeap{T, LessThan}
const MutableBinaryMaxHeap{T} = MutableBinaryHeap{T, GreaterThan}

MutableBinaryMinHeap(xs::AbstractVector{T}) where T = MutableBinaryMinHeap{T}(xs)
MutableBinaryMaxHeap(xs::AbstractVector{T}) where T = MutableBinaryMaxHeap{T}(xs)

# deprecated constructors

@deprecate mutable_binary_minheap(::Type{T}) where {T} MutableBinaryMinHeap{T}()
@deprecate mutable_binary_minheap(xs::AbstractVector{T}) where {T} MutableBinaryMinHeap(xs)
@deprecate mutable_binary_maxheap(::Type{T}) where {T} MutableBinaryMaxHeap{T}()
@deprecate mutable_binary_maxheap(xs::AbstractVector{T}) where {T} MutableBinaryMaxHeap(xs)


function show(io::IO, h::MutableBinaryHeap)
print(io, "MutableBinaryHeap(")
Expand Down
20 changes: 14 additions & 6 deletions test/test_binheap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]

@testset "make min heap" begin
h = binary_minheap(vs)
h = BinaryMinHeap(vs)

@test length(h) == 10
@test !isempty(h)
Expand All @@ -15,7 +15,7 @@
end

@testset "make max heap" begin
h = binary_maxheap(vs)
h = BinaryMaxHeap(vs)

@test length(h) == 10
@test !isempty(h)
Expand All @@ -25,7 +25,7 @@

@testset "push!" begin
@testset "push! hmin" begin
hmin = binary_minheap(Int)
hmin = BinaryMinHeap{Int}()
@test length(hmin) == 0
@test isempty(hmin)

Expand Down Expand Up @@ -56,7 +56,7 @@
end

@testset "push! hmax" begin
hmax = binary_maxheap(Int)
hmax = BinaryMaxHeap{Int}()
@test length(hmax) == 0
@test isempty(hmax)

Expand Down Expand Up @@ -89,7 +89,7 @@
end

@testset "hybrid push! and pop!" begin
h = binary_minheap(Int)
h = BinaryMinHeap{Int}()

@testset "push1" begin
push!(h, 5)
Expand Down Expand Up @@ -126,13 +126,21 @@
end

@testset "push! type conversion" begin # issue 399
h = binary_minheap(Float64)
h = BinaryMinHeap{Float64}()
push!(h, 3.0)
push!(h, 5)
push!(h, Rational(4, 8))
push!(h, Complex(10.1, 0.0))

@test isequal(h.valtree, [0.5, 5.0, 3.0, 10.1])
end

# test deprecated constructors
@testset "deprecated constructors" begin
@test_deprecated binary_minheap(Int)
@test_deprecated binary_minheap([1., 2., 3.])
@test_deprecated binary_maxheap(Int)
@test_deprecated binary_maxheap([1., 2., 3.])
end

end # @testset BinaryHeap
23 changes: 16 additions & 7 deletions test/test_mutable_binheap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ end
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]

@testset "make mutable binary minheap" begin
h = mutable_binary_minheap(vs)
h = MutableBinaryMinHeap(vs)

@test length(h) == 10
@test !isempty(h)
Expand All @@ -65,7 +65,7 @@ end
end

@testset "make mutable binary maxheap" begin
h = mutable_binary_maxheap(vs)
h = MutableBinaryMaxHeap(vs)

@test length(h) == 10
@test !isempty(h)
Expand All @@ -75,7 +75,7 @@ end
end

@testset "hmin / push! / pop!" begin
hmin = mutable_binary_minheap(Int)
hmin = MutableBinaryMinHeap{Int}()
@test length(hmin) == 0
@test isempty(hmin)

Expand Down Expand Up @@ -106,7 +106,7 @@ end
end

@testset "hmax / push! / pop!" begin
hmax = mutable_binary_maxheap(Int)
hmax = MutableBinaryMaxHeap{Int}()
@test length(hmax) == 0
@test isempty(hmax)

Expand Down Expand Up @@ -138,7 +138,7 @@ end
end

@testset "hybrid push! and pop!" begin
h = mutable_binary_minheap(Int)
h = MutableBinaryMinHeap{Int}()

push!(h, 5)
push!(h, 10)
Expand All @@ -160,7 +160,7 @@ end
end

@testset "test update! and top_with_handle" begin
for (hf,m) = [(mutable_binary_minheap,-2.0), (mutable_binary_maxheap,2.0)]
for (hf,m) = [(MutableBinaryMinHeap,-2.0), (MutableBinaryMaxHeap,2.0)]
xs = rand(100)
h = hf(xs)
@test length(h) == 100
Expand Down Expand Up @@ -210,7 +210,7 @@ end
end

@testset "test push! and update! conversion" begin # issue 399
h = mutable_binary_minheap(Float64)
h = MutableBinaryMinHeap{Float64}()
push!(h, 3.0)
push!(h, 5)
push!(h, Rational(4, 8))
Expand All @@ -220,5 +220,14 @@ end
update!(h, 2, 20)
@test isequal(heap_values(h), [0.5, 10.1, 3.0, 20.0])
end

# test deprecated constructors
@testset "deprecated constructors" begin
@test_deprecated mutable_binary_minheap(Int)
@test_deprecated mutable_binary_minheap([1., 2., 3.])
@test_deprecated mutable_binary_maxheap(Int)
@test_deprecated mutable_binary_maxheap([1., 2., 3.])
end


end # @testset MutableBinheap

0 comments on commit 5044489

Please sign in to comment.