Skip to content

Commit 37f0220

Browse files
authored
Fix and test searchsorted for arrays whose first index is typemin(Int) (JuliaLang#56474)
This fixes the issue reported in JuliaLang#56457 (comment) which, combined with JuliaLang#56464 which fixed the issue in the OP, fixes JuliaLang#56457. `searchsortedfirst` was fine all along, but I added it to tests regardless.
1 parent d90c2e2 commit 37f0220

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

base/sort.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ function searchsortedlast(v::AbstractVector, x, lo::T, hi::T, o::Ordering)::keyt
206206
u = T(1)
207207
lo = lo - u
208208
hi = hi + u
209-
@inbounds while lo < hi - u
209+
@inbounds while lo != hi - u
210210
m = midpoint(lo, hi)
211211
if lt(o, x, v[m])
212212
hi = m
@@ -224,7 +224,7 @@ function searchsorted(v::AbstractVector, x, ilo::T, ihi::T, o::Ordering)::UnitRa
224224
u = T(1)
225225
lo = ilo - u
226226
hi = ihi + u
227-
@inbounds while lo < hi - u
227+
@inbounds while lo != hi - u
228228
m = midpoint(lo, hi)
229229
if lt(o, v[m], x)
230230
lo = m

test/sorting.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,22 @@ end
585585
# Issue #56457
586586
o2 = OffsetArray([2,2,3], typemax(Int)-3);
587587
@test searchsorted(o2, 2) == firstindex(o2):firstindex(o2)+1
588+
589+
struct IdentityVector <: AbstractVector{Int}
590+
lo::Int
591+
hi::Int
592+
end
593+
function Base.getindex(s::IdentityVector, i::Int)
594+
s.lo <= i <= s.hi || throw(BoundsError(s, i))
595+
i
596+
end
597+
Base.axes(s::IdentityVector) = (s.lo:s.hi,)
598+
Base.size(s::IdentityVector) = length.(axes(s))
599+
600+
o3 = IdentityVector(typemin(Int), typemin(Int)+5)
601+
@test searchsortedfirst(o3, typemin(Int)+2) === typemin(Int)+2
602+
@test searchsortedlast(o3, typemin(Int)+2) === typemin(Int)+2
603+
@test searchsorted(o3, typemin(Int)+2) === typemin(Int)+2:typemin(Int)+2
588604
end
589605

590606
function adaptive_sort_test(v; trusted=InsertionSort, kw...)

0 commit comments

Comments
 (0)