@@ -13,33 +13,35 @@ heapleft(i::Integer) = 2i
1313heapright (i:: Integer ) = 2 i + 1
1414heapparent (i:: Integer ) = div (i, 2 )
1515
16-
1716# Binary min-heap percolate down.
18- function percolate_down! (xs:: AbstractArray , i:: Integer , x= xs[i], o:: Ordering = Forward, len:: Integer = length (xs))
17+ Base. @propagate_inbounds function percolate_down! (xs:: AbstractArray , i:: Integer , x, o:: Ordering = Forward, len:: Integer = length (xs))
18+ @boundscheck checkbounds (xs, i)
19+ @boundscheck checkbounds (xs, len)
20+
1921 @inbounds while (l = heapleft (i)) <= len
2022 r = heapright (i)
2123 j = r > len || lt (o, xs[l], xs[r]) ? l : r
2224 lt (o, xs[j], x) || break
2325 xs[i] = xs[j]
2426 i = j
2527 end
26- xs[i] = x
28+ @inbounds xs[i] = x
2729end
28-
29- percolate_down! (xs:: AbstractArray , i:: Integer , o:: Ordering , len:: Integer = length (xs)) = percolate_down! (xs, i, xs[i], o, len)
30+ Base. @propagate_inbounds percolate_down! (xs:: AbstractArray , i:: Integer , o:: Ordering , len:: Integer = length (xs)) = percolate_down! (xs, i, xs[i], o, len)
3031
3132
3233# Binary min-heap percolate up.
33- function percolate_up! (xs:: AbstractArray , i:: Integer , x= xs[i], o:: Ordering = Forward)
34+ Base. @propagate_inbounds function percolate_up! (xs:: AbstractArray , i:: Integer , x, o:: Ordering = Forward)
35+ @boundscheck checkbounds (xs, i)
36+
3437 @inbounds while (j = heapparent (i)) >= 1
3538 lt (o, x, xs[j]) || break
3639 xs[i] = xs[j]
3740 i = j
3841 end
39- xs[i] = x
42+ @inbounds xs[i] = x
4043end
41-
42- @inline percolate_up! (xs:: AbstractArray , i:: Integer , o:: Ordering ) = percolate_up! (xs, i, xs[i], o)
44+ Base. @propagate_inbounds percolate_up! (xs:: AbstractArray , i:: Integer , o:: Ordering ) = percolate_up! (xs, i, xs[i], o)
4345
4446"""
4547 heappop!(v, [ord])
@@ -48,10 +50,11 @@ Given a binary heap-ordered array, remove and return the lowest ordered element.
4850For efficiency, this function does not check that the array is indeed heap-ordered.
4951"""
5052function heappop! (xs:: AbstractArray , o:: Ordering = Forward)
53+ Base. require_one_based_indexing (xs)
5154 x = xs[1 ]
5255 y = pop! (xs)
5356 if ! isempty (xs)
54- percolate_down! (xs, 1 , y, o)
57+ @inbounds percolate_down! (xs, 1 , y, o)
5558 end
5659 return x
5760end
@@ -63,8 +66,9 @@ Given a binary heap-ordered array, push a new element `x`, preserving the heap p
6366For efficiency, this function does not check that the array is indeed heap-ordered.
6467"""
6568@inline function heappush! (xs:: AbstractArray , x, o:: Ordering = Forward)
69+ Base. require_one_based_indexing (xs)
6670 push! (xs, x)
67- percolate_up! (xs, length (xs), o)
71+ @inbounds percolate_up! (xs, length (xs), o)
6872 return xs
6973end
7074
7680In-place [`heapify`](@ref).
7781"""
7882@inline function heapify! (xs:: AbstractArray , o:: Ordering = Forward)
83+ Base. require_one_based_indexing (xs)
7984 for i in heapparent (length (xs)): - 1 : 1
80- percolate_down! (xs, i, o)
85+ @inbounds percolate_down! (xs, i, o)
8186 end
8287 return xs
8388end
@@ -129,6 +134,7 @@ false
129134```
130135"""
131136function isheap (xs:: AbstractArray , o:: Ordering = Forward)
137+ Base. require_one_based_indexing (xs)
132138 for i in 1 : div (length (xs), 2 )
133139 if lt (o, xs[heapleft (i)], xs[i]) ||
134140 (heapright (i) <= length (xs) && lt (o, xs[heapright (i)], xs[i]))
0 commit comments