Skip to content

Commit d503179

Browse files
authored
Define empty! for BinaryHeap and MutableBinaryHeap (#932)
1 parent 84dc88d commit d503179

File tree

6 files changed

+46
-0
lines changed

6 files changed

+46
-0
lines changed

docs/src/heaps.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ length(h) # returns the number of elements
1313

1414
isempty(h) # returns whether the heap is empty
1515

16+
empty!(h) # reset the heap
17+
1618
push!(h, v) # add a value to the heap
1719

1820
first(h) # return the first (top) value of a heap

src/heaps.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
# - isempty(h) returns whether the heap is
2121
# empty
2222
#
23+
# - empty!(h) reset the heap
24+
#
2325
# - push!(h, v) add a value to the heap
2426
#
2527
# - sizehint!(h, s) set size hint to a heap

src/heaps/binary_heap.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ Removes and returns the element at the top of the heap `h`.
106106
"""
107107
Base.pop!(h::BinaryHeap) = heappop!(h.valtree, h.ordering)
108108

109+
function Base.empty!(h::BinaryHeap)
110+
empty!(h.valtree)
111+
return h
112+
end
113+
109114
# Suggest that heap `h` reserve capacity for at least `n` elements. This can improve performance.
110115
function Base.sizehint!(h::BinaryHeap, n::Integer)
111116
sizehint!(h.valtree, n)

src/heaps/mutable_binary_heap.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ function Base.push!(h::MutableBinaryHeap{T}, v) where T
226226
return i
227227
end
228228

229+
function Base.empty!(h::MutableBinaryHeap)
230+
empty!(h.nodes)
231+
empty!(h.node_map)
232+
return h
233+
end
234+
229235
function Base.sizehint!(h::MutableBinaryHeap, s::Integer)
230236
sizehint!(h.nodes, s)
231237
sizehint!(h.node_map, s)

test/test_binheap.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,19 @@
237237

238238
@test isequal(h.valtree, [0.5, 5.0, 3.0, 10.1])
239239
end
240+
241+
@testset "empty!" begin
242+
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]
243+
vs2 = collect(enumerate(vs))
244+
ordering = Base.Order.By(last)
245+
246+
for h in (BinaryMinHeap(vs), BinaryMaxHeap(vs), BinaryHeap(ordering, vs2))
247+
@test length(h) == length(vs)
248+
@test !isempty(h)
249+
ret = empty!(h)
250+
@test ret === h
251+
@test length(ret) == 0
252+
@test isempty(ret)
253+
end
254+
end
240255
end # @testset BinaryHeap

test/test_mutable_binheap.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,20 @@ end
342342
update!(h, 2, 20)
343343
@test isequal(heap_values(h), [0.5, 10.1, 3.0, 20.0])
344344
end
345+
346+
@testset "empty!" begin
347+
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]
348+
vs2 = collect(enumerate(vs))
349+
ordering = Base.Order.By(last)
350+
351+
for h in (MutableBinaryMinHeap(vs), MutableBinaryMaxHeap(vs), MutableBinaryHeap(ordering, vs2))
352+
@test length(h) == length(vs)
353+
@test !isempty(h)
354+
ret = empty!(h)
355+
@test ret === h
356+
@test length(ret) == 0
357+
@test isempty(ret)
358+
@test verify_heap(ret)
359+
end
360+
end
345361
end # @testset MutableBinheap

0 commit comments

Comments
 (0)