From 94889ac976149f435d701331054e665f06283147 Mon Sep 17 00:00:00 2001 From: CarloLucibello Date: Tue, 7 Feb 2017 18:52:33 +0100 Subject: [PATCH] indexing interface for MutableBinaryHeap --- src/heaps/mutable_binary_heap.jl | 4 ++++ test/runtests.jl | 6 ++++-- test/test_mutable_binheap.jl | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/heaps/mutable_binary_heap.jl b/src/heaps/mutable_binary_heap.jl index 5160f0869..da2ef2147 100644 --- a/src/heaps/mutable_binary_heap.jl +++ b/src/heaps/mutable_binary_heap.jl @@ -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 @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 35f46821d..22851f1b3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -9,7 +9,8 @@ const IntSet = DataStructures.IntSet import Compat: String using Primes -tests = ["int_set", +tests = [ + "int_set", "deque", "circ_deque", "sorted_containers", @@ -27,7 +28,8 @@ tests = ["int_set", "multi_dict", "circular_buffer", "sorting", - "priorityqueue"] + "priorityqueue" + ] if length(ARGS) > 0 tests = ARGS diff --git a/test/test_mutable_binheap.jl b/test/test_mutable_binheap.jl index 05bacb76e..ad5ad91cd 100644 --- a/test/test_mutable_binheap.jl +++ b/test/test_mutable_binheap.jl @@ -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