Add (keys()
and) values()
interfaces for (mutable) binary heaps
#796
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Occasionally we need to iterate through all of the elements of a heap but don't care about the order. In this situation, using
extract_all!(h)
is wasteful because it costsO(n log n)
to return the elements in sorted order (and also mutates the heap). In DataStructures.jl, accessingh.valtree
directly is more efficient atO(n)
; however, this field is not documented.This PR adds the more Julian interface
Base.values(h) = h.valtree
forBinaryHeap
s andBinaryMinMaxHeaps
.For
MutableBinaryHeaps
, it also addsBase.keys(h) = h.node_map
andBase.values(h)
which returns an iterator over the values.I have included unit tests and documentation.
(This is my first time submitting a pull request on GitHub. I have made every effort to follow the contrib guide, but I apologize in advance if I have done anything wrong. The first commit in my fork mentions the names
drain()
anddrain!()
, which are the names used for this operation in Rust, but I renamed them in a subsequent commit to match similar Julia built-ins.)