-
Notifications
You must be signed in to change notification settings - Fork 262
Improve bounds checks in heap operations #954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,6 +109,7 @@ end | |
| return an array of the first `n` values of `arr` sorted by `ord`. | ||
| """ | ||
| function nextreme(ord::Base.Ordering, n::Int, arr::AbstractVector{T}) where T | ||
| Base.require_one_based_indexing(arr) | ||
| if n <= 0 | ||
| return T[] # sort(arr)[1:n] returns [] for n <= 0 | ||
| elseif n >= length(arr) | ||
|
|
@@ -117,10 +118,10 @@ function nextreme(ord::Base.Ordering, n::Int, arr::AbstractVector{T}) where T | |
|
|
||
| rev = Base.ReverseOrdering(ord) | ||
|
|
||
| buffer = heapify(arr[1:n], rev) | ||
| buffer = heapify!(arr[1:n], rev) | ||
|
|
||
| for i = n + 1 : length(arr) | ||
| @inbounds xi = arr[i] | ||
| @inbounds for i = n + 1 : length(arr) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this wrong? Since we do not know how If we want this i think we need a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for reviewing this! I was sprinkling # Binary heap indexing
heapleft(i::Integer) = 2i
heapright(i::Integer) = 2i + 1
heapparent(i::Integer) = div(i, 2)So I wonder if the entire
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would keep the checks, and optionally document it. |
||
| xi = arr[i] | ||
| if Base.lt(rev, buffer[1], xi) | ||
| buffer[1] = xi | ||
| percolate_down!(buffer, 1, rev) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,33 +13,35 @@ heapleft(i::Integer) = 2i | |
| heapright(i::Integer) = 2i + 1 | ||
| heapparent(i::Integer) = div(i, 2) | ||
|
|
||
|
|
||
| # Binary min-heap percolate down. | ||
| function percolate_down!(xs::AbstractArray, i::Integer, x=xs[i], o::Ordering=Forward, len::Integer=length(xs)) | ||
| Base.@propagate_inbounds function percolate_down!(xs::AbstractArray, i::Integer, x, o::Ordering=Forward, len::Integer=length(xs)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a breaking change? It seems you made the third argument mandatory.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, thanks for letting me know and apologies for the mistake. I think this could be fixed by adding a default ordering argument to the method below, i.e. - Base.@propagate_inbounds percolate_down!(xs::AbstractArray, i::Integer, o::Ordering, len::Integer=length(xs)) = percolate_down!(xs, i, xs[i], o, len)
+ Base.@propagate_inbounds percolate_down!(xs::AbstractArray, i::Integer, o::Ordering=Forward, len::Integer=length(xs)) = percolate_down!(xs, i, xs[i], o, len)Do you agree, @oxinabox?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PR to try it out: #960
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the PR above is in good shape to be merged and fix this issue. Sorry everyone, I didn't realize the method was de facto public, so I wasn't thinking about its impact on compatibility. |
||
| @boundscheck checkbounds(xs, i) | ||
| @boundscheck checkbounds(xs, len) | ||
|
|
||
| @inbounds while (l = heapleft(i)) <= len | ||
| r = heapright(i) | ||
| j = r > len || lt(o, xs[l], xs[r]) ? l : r | ||
| lt(o, xs[j], x) || break | ||
| xs[i] = xs[j] | ||
| i = j | ||
| end | ||
| xs[i] = x | ||
| @inbounds xs[i] = x | ||
| end | ||
|
|
||
| percolate_down!(xs::AbstractArray, i::Integer, o::Ordering, len::Integer=length(xs)) = percolate_down!(xs, i, xs[i], o, len) | ||
| Base.@propagate_inbounds percolate_down!(xs::AbstractArray, i::Integer, o::Ordering, len::Integer=length(xs)) = percolate_down!(xs, i, xs[i], o, len) | ||
|
|
||
|
|
||
| # Binary min-heap percolate up. | ||
| function percolate_up!(xs::AbstractArray, i::Integer, x=xs[i], o::Ordering=Forward) | ||
| Base.@propagate_inbounds function percolate_up!(xs::AbstractArray, i::Integer, x, o::Ordering=Forward) | ||
| @boundscheck checkbounds(xs, i) | ||
|
|
||
| @inbounds while (j = heapparent(i)) >= 1 | ||
| lt(o, x, xs[j]) || break | ||
| xs[i] = xs[j] | ||
| i = j | ||
| end | ||
| xs[i] = x | ||
| @inbounds xs[i] = x | ||
| end | ||
|
|
||
| @inline percolate_up!(xs::AbstractArray, i::Integer, o::Ordering) = percolate_up!(xs, i, xs[i], o) | ||
| Base.@propagate_inbounds percolate_up!(xs::AbstractArray, i::Integer, o::Ordering) = percolate_up!(xs, i, xs[i], o) | ||
|
|
||
| """ | ||
| heappop!(v, [ord]) | ||
|
|
@@ -48,10 +50,11 @@ Given a binary heap-ordered array, remove and return the lowest ordered element. | |
| For efficiency, this function does not check that the array is indeed heap-ordered. | ||
| """ | ||
| function heappop!(xs::AbstractArray, o::Ordering=Forward) | ||
| Base.require_one_based_indexing(xs) | ||
| x = xs[1] | ||
| y = pop!(xs) | ||
| if !isempty(xs) | ||
| percolate_down!(xs, 1, y, o) | ||
| @inbounds percolate_down!(xs, 1, y, o) | ||
| end | ||
| return x | ||
| end | ||
|
|
@@ -63,8 +66,9 @@ Given a binary heap-ordered array, push a new element `x`, preserving the heap p | |
| For efficiency, this function does not check that the array is indeed heap-ordered. | ||
| """ | ||
| @inline function heappush!(xs::AbstractArray, x, o::Ordering=Forward) | ||
| Base.require_one_based_indexing(xs) | ||
| push!(xs, x) | ||
| percolate_up!(xs, length(xs), o) | ||
| @inbounds percolate_up!(xs, length(xs), o) | ||
| return xs | ||
| end | ||
|
|
||
|
|
@@ -76,8 +80,9 @@ end | |
| In-place [`heapify`](@ref). | ||
| """ | ||
| @inline function heapify!(xs::AbstractArray, o::Ordering=Forward) | ||
| Base.require_one_based_indexing(xs) | ||
| for i in heapparent(length(xs)):-1:1 | ||
| percolate_down!(xs, i, o) | ||
| @inbounds percolate_down!(xs, i, o) | ||
| end | ||
| return xs | ||
| end | ||
|
|
@@ -129,6 +134,7 @@ false | |
| ``` | ||
| """ | ||
| function isheap(xs::AbstractArray, o::Ordering=Forward) | ||
| Base.require_one_based_indexing(xs) | ||
| for i in 1:div(length(xs), 2) | ||
| if lt(o, xs[heapleft(i)], xs[i]) || | ||
| (heapright(i) <= length(xs) && lt(o, xs[heapright(i)], xs[i])) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is safe since that slice is a copy. Good catch