-
Notifications
You must be signed in to change notification settings - Fork 260
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
base: master
Are you sure you want to change the base?
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) | ||
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 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 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) | ||
|
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