Skip to content
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

[backport #35274] fix #35272, searchsorted(3:-1:1, 2.5, rev=true) #35435

Merged
merged 1 commit into from
Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ function searchsortedlast(a::AbstractRange{<:Integer}, x::Real, o::DirectOrderin
if step(a) == 0
lt(o, x, first(a)) ? 0 : length(a)
else
clamp( fld(floor(Integer, x) - first(a), step(a)) + 1, 0, length(a))
if o isa ForwardOrdering
clamp( fld(floor(Integer, x) - first(a), step(a)) + 1, 0, length(a))
else
clamp( fld(ceil(Integer, x) - first(a), step(a)) + 1, 0, length(a))
end
end
end

Expand All @@ -262,7 +266,11 @@ function searchsortedfirst(a::AbstractRange{<:Integer}, x::Real, o::DirectOrderi
if step(a) == 0
lt(o, first(a), x) ? length(a)+1 : 1
else
clamp(-fld(floor(Integer, -x) + first(a), step(a)) + 1, 1, length(a) + 1)
if o isa ForwardOrdering
clamp(-fld(floor(Integer, -x) + first(a), step(a)) + 1, 1, length(a) + 1)
else
clamp(-fld(ceil(Integer, -x) + first(a), step(a)) + 1, 1, length(a) + 1)
end
end
end

Expand Down
11 changes: 11 additions & 0 deletions test/sorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ end
@test searchsortedlast(500:1.0:600, -1.0e20) == 0
@test searchsortedlast(500:1.0:600, 1.0e20) == 101
end
@testset "issue #35272" begin
for v0 = (3:-1:1, 3.0:-1.0:1.0), v = (v0, collect(v0))
@test searchsorted(v, 3, rev=true) == 1:1
@test searchsorted(v, 3.0, rev=true) == 1:1
@test searchsorted(v, 2.5, rev=true) == 2:1
@test searchsorted(v, 2, rev=true) == 2:2
@test searchsorted(v, 1.2, rev=true) == 3:2
@test searchsorted(v, 1, rev=true) == 3:3
@test searchsorted(v, 0.1, rev=true) == 4:3
end
end
end
# exercise the codepath in searchsorted* methods for ranges that check for zero step range
struct ConstantRange{T} <: AbstractRange{T}
Expand Down