Skip to content

Commit

Permalink
Merge pull request #269 from CarloLucibello/heap
Browse files Browse the repository at this point in the history
indexing interface for MutableBinaryHeap
  • Loading branch information
kmsquire authored Jul 13, 2017
2 parents 7f5032d + 74a5f2c commit e8c2e9b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/heaps/mutable_binary_heap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ pop!{T}(h::MutableBinaryHeap{T}) = _binary_heap_pop!(h.comparer, h.nodes, h.node
update!{T}(h::MutableBinaryHeap{T}, i::Int, v::T)
Replace the element at index `i` in heap `h` with `v`.
This is equivalent to `h[i]=v`.
"""
function update!{T}(h::MutableBinaryHeap{T}, i::Int, v::T)
nodes = h.nodes
Expand All @@ -241,3 +242,6 @@ function update!{T}(h::MutableBinaryHeap{T}, i::Int, v::T)
_heap_bubble_down!(comp, nodes, nodemap, nd_id)
end
end

setindex!(h::MutableBinaryHeap, v, i::Int) = update!(h, i, v)
getindex(h::MutableBinaryHeap, i::Int) = h.nodes[h.node_map[i]].value
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ tests = ["int_set",
"multi_dict",
"circular_buffer",
"sorting",
"priorityqueue"]
"priorityqueue"
]

if length(ARGS) > 0
tests = ARGS
Expand Down
9 changes: 9 additions & 0 deletions test/test_mutable_binheap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,14 @@ for (hf,m) = [(mutable_binary_minheap,-2.0), (mutable_binary_maxheap,2.0)]
@test verify_heap(h)
@test isequal(list_values(h), xs)
@test top_with_handle(h) == (v, i)

i = rand(1:100)
v = rand()
h[i] = v
xs[i] = v
@test length(h) == 100
@test verify_heap(h)
@test isequal(list_values(h), xs)
@test v == h[i]
end
end

0 comments on commit e8c2e9b

Please sign in to comment.