Skip to content

Commit

Permalink
Heap constructor dilemma with reverse ordering type
Browse files Browse the repository at this point in the history
  • Loading branch information
milesfrain committed Oct 31, 2019
1 parent 44f00a2 commit cf5e738
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/heaps/binary_heap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,36 @@ include("arrays_as_heaps.jl")
#################################################

#=
These structs may be substituted by Base.Forward and Base.Reverse,
but float comparison will be 2x slower to preserve ordering with NAN values.
FasterForward enables 2x faster float comparison versus Base.ForwardOrdering,
but ordering is undefined if the data contains NaN values.
Enable this higher-performance option by calling the BinaryHeap
constructor instead of the BinaryMinHeap helper constructor.
The same is true for FasterReverse and BinaryMaxHeap.
=#

This comment has been minimized.

Copy link
@oxinabox

oxinabox Nov 1, 2019

This should be a docstring.
On each.

struct FasterForward <: Base.Ordering end
struct FasterReverse <: Base.Ordering end
Base.lt(o::FasterForward, a, b) = a < b
Base.lt(o::FasterReverse, a, b) = a > b

mutable struct BinaryHeap{T, O <: Base.Ordering} <: AbstractHeap{T}
valtree::Vector{T}
ordering::O
valtree::Vector{T}

# min heap by default
function BinaryHeap(::Type{T}, ordering::O = FasterForward()) where {T,O}
new{T,O}(Vector{T}(), ordering)
function BinaryHeap{T, O}() where {T,O}
new{T,O}(O(), Vector{T}())
end

function BinaryHeap(xs::AbstractVector{T}, ordering::O = FasterForward()) where {T,O}
function BinaryHeap{T, O}(xs::AbstractVector{T}) where {T,O}
ordering = O()
valtree = heapify(xs, ordering)
new{T,O}(valtree, ordering)
new{T,O}(ordering, valtree)
end
end

BinaryMinHeap(xs::AbstractVector) = BinaryHeap(xs, FasterForward())
BinaryMaxHeap(xs::AbstractVector) = BinaryHeap(xs, FasterReverse())
BinaryMinHeap(::Type{T}) where T = BinaryHeap(T, FasterForward())
BinaryMaxHeap(::Type{T}) where T = BinaryHeap(T, FasterReverse())

const BinaryMinHeap{T} = BinaryHeap{T, Base.ForwardOrdering}
const BinaryMaxHeap{T} = BinaryHeap{T, Base.ReverseOrdering}
BinaryMinHeap(xs::AbstractVector{T}) where T = BinaryMinHeap{T}(xs)
BinaryMaxHeap(xs::AbstractVector{T}) where T = BinaryMaxHeap{T}(xs)

#################################################
#
Expand Down
14 changes: 14 additions & 0 deletions test/test_binheap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

@testset "BinaryHeaps" begin

BinaryHeap{Int, Base.ForwardOrdering}()
BinaryHeap{Int, Base.ForwardOrdering}([1,2])

BinaryMinHeap{Int}()
BinaryMinHeap{Int}([1,2])

# These fail
BinaryMaxHeap{Int}()
BinaryHeap{Int, Base.ReverseOrdering}()

@test true

#=
@testset "make heap" begin
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]
Expand Down Expand Up @@ -146,4 +159,5 @@
@test isequal(h.valtree, [0.5, 5.0, 3.0, 10.1])
end
=#
end # @testset BinaryHeap

0 comments on commit cf5e738

Please sign in to comment.