Skip to content

Commit

Permalink
Fix MutableBinaryHeap, same issue as #686
Browse files Browse the repository at this point in the history
  • Loading branch information
dm3 authored and oxinabox committed Jan 19, 2021
1 parent 7663313 commit f3d15dc
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/heaps/arrays_as_heaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using Base.Order: Forward, Ordering, lt

const DefaultReverseOrdering = Base.ReverseOrdering{Base.ForwardOrdering}

# Heap operations on flat arrays
# ------------------------------
Expand Down
2 changes: 0 additions & 2 deletions src/heaps/binary_heap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ BinaryHeap(ordering::Base.Ordering, xs::AbstractVector{T}) where T = BinaryHeap{
BinaryHeap{T, O}() where {T, O<:Base.Ordering} = BinaryHeap{T}(O())
BinaryHeap{T, O}(xs::AbstractVector) where {T, O<:Base.Ordering} = BinaryHeap{T}(O(), xs)

const DefaultReverseOrdering = Base.ReverseOrdering{Base.ForwardOrdering}

# These constructors needed for BinaryMaxHeap, until we have https://github.com/JuliaLang/julia/pull/37822
BinaryHeap{T, DefaultReverseOrdering}() where {T} = BinaryHeap{T}(Base.Reverse)
BinaryHeap{T, DefaultReverseOrdering}(xs::AbstractVector) where {T} = BinaryHeap{T}(Base.Reverse, xs)
Expand Down
6 changes: 5 additions & 1 deletion src/heaps/mutable_binary_heap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,13 @@ MutableBinaryHeap(ordering::Base.Ordering, xs::AbstractVector{T}) where T = Muta
MutableBinaryHeap{T, O}() where {T, O<:Base.Ordering} = MutableBinaryHeap{T}(O())
MutableBinaryHeap{T, O}(xs::AbstractVector) where {T, O<:Base.Ordering} = MutableBinaryHeap{T}(O(), xs)

# These constructors needed for BinaryMaxHeap, until we have https://github.com/JuliaLang/julia/pull/37822
MutableBinaryHeap{T, DefaultReverseOrdering}() where {T} = MutableBinaryHeap{T}(Base.Reverse)
MutableBinaryHeap{T, DefaultReverseOrdering}(xs::AbstractVector) where {T} = MutableBinaryHeap{T}(Base.Reverse, xs)

# Forward/reverse ordering type aliases
const MutableBinaryMinHeap{T} = MutableBinaryHeap{T, Base.ForwardOrdering}
const MutableBinaryMaxHeap{T} = MutableBinaryHeap{T, Base.ReverseOrdering}
const MutableBinaryMaxHeap{T} = MutableBinaryHeap{T, DefaultReverseOrdering}

MutableBinaryMinHeap(xs::AbstractVector{T}) where T = MutableBinaryMinHeap{T}(xs)
MutableBinaryMaxHeap(xs::AbstractVector{T}) where T = MutableBinaryMaxHeap{T}(xs)
Expand Down
6 changes: 6 additions & 0 deletions test/test_mutable_binheap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ end
@test true
end

@testset "Type Aliases" begin
# https://github.com/JuliaCollections/DataStructures.jl/issues/686
@test MutableBinaryMaxHeap{Int}() isa MutableBinaryMaxHeap{Int}
@test MutableBinaryMinHeap{Int}() isa MutableBinaryMinHeap{Int}
end

@testset "basic tests" begin
h = MutableBinaryMinHeap{Int}()

Expand Down

0 comments on commit f3d15dc

Please sign in to comment.