-
Notifications
You must be signed in to change notification settings - Fork 249
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
Should PriorityQueue actually be a dictionary? #690
Comments
Just stumbled upon this issue, and it speaks to me since I implemented my own struct VectorPriorityQueue{K,V}
keys::Vector{K}
values::Vector{V}
end
VectorPriorityQueue{K,V}() where {K,V} = VectorPriorityQueue{K,V}(K[], V[])
function enqueue!(pq::VectorPriorityQueue{K,V}, k::K, v::V) where {K,V}
left = searchsortedfirst(pq.values, v)
insert!(pq.keys, left, k)
insert!(pq.values, left, v)
end
function Base.deleteat!(pq::VectorPriorityQueue, i::Integer)
deleteat!(pq.keys, i)
deleteat!(pq.values, i)
end
function dequeue!(pq::VectorPriorityQueue)
k = popfirst!(pq.keys)
popfirst!(pq.values)
return k
end
Base.length(pq::VectorPriorityQueue) = length(pq.keys)
Base.keys(pq::VectorPriorityQueue) = pq.keys
Base.values(pq::VectorPriorityQueue) = pq.values
Base.isempty(pq::VectorPriorityQueue) = isempty(pq.keys)
Base.pairs(pq::VectorPriorityQueue) = (k => v for (k, v) in zip(pq.keys, pq.values)) |
@gdalle, thanks for sharing. This is useful for me. Do you happen to know how to extend Edit: I suppose it could be a simple as
|
Currently, PriorityQueue is implemented as an AbstractDict by wrapping a
Dict
instance. Apparently, this design mixes two different data structures, and since this leads to performance issues (e.g., #596) I'm wondering if this should be changed.Instead, alternatively PriorityQueue could be implemented as a wrapper of a heap structure for pairs (by default a BinaryHeap but adjustable by the user) that provides a consistent interface for accessing the top element, its key and its value, removing the top element, and inserting elements, without additionally serving as a dictionary (similar to, e.g., the std::priority_queue in C++). If users need a dictionary structure as well, they can create one independently of the PriorityQueue. This separation would also make it easier to use alternative dictionary types such as SwissDict or RobinDict.
The text was updated successfully, but these errors were encountered: