I tried this today:
immutable State
a::Int
b::Int
end
import Base.Collections: PriorityQueue, enqueue!, dequeue!, peek
pq = PriorityQueue(Int,Int)
enqueue!(pq, State(5,3), 4)
enqueue!(pq, State(5,3), 2)
but it fails with the error
LoadError: ArgumentError: PriorityQueue keys must be unique
I assume this is because the states are immutable, and thus compare equal if all their values compare equal. Is there a way to make a priority queue insert the same state on several priorities, i.e. to support my example above?
// T
julia> Base.isless(s1::State, s2::State) = isless(s1.a, s1.a) || (s1.a == s2.b && isless(s
1.a, s2.b))
isless (generic function with 32 methods)
julia> h = binary_minheap(Pair{Int,State})
DataStructures.BinaryHeap{Pair{Int64,State},DataStructures.LessThan}(DataStructures.LessTh
an(),Pair{Int64,State}[])
julia> push!(h, 4 => State(5, 3))
julia> push!(h, 2 => State(5, 3))
julia> top(h)
2=>State(5,3)
julia> pop!(h)
2=>State(5,3)
julia> top(h)
4=>State(5,3)Base.isless(s1::State, s2::State) = isless(s1.a, s2.a) || (s1.a == s2.a && isless(s1.b, s2.b))