Skip to content

Commit

Permalink
Loosen searchsorted* index type (fixes JuliaLang#30763)
Browse files Browse the repository at this point in the history
* Might also address JuliaLang#31618
* Types of start and stop indicies are still restricted to
  Union{Int32,Int64} and must be the same
  • Loading branch information
kmsquire committed Apr 6, 2019
1 parent b4e1d0e commit 584e42e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
24 changes: 12 additions & 12 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ partialsort(v::AbstractVector, k::Union{Int,OrdinalRange}; kws...) =

# index of the first value of vector a that is greater than or equal to x;
# returns length(v)+1 if x is greater than all values in v.
function searchsortedfirst(v::AbstractVector, x, lo::Int, hi::Int, o::Ordering)
lo = lo-1
hi = hi+1
@inbounds while lo < hi-1
function searchsortedfirst(v::AbstractVector, x, lo::INT, hi::INT, o::Ordering) where INT<:Union{Int32,Int64}
lo = lo-INT(1)
hi = hi+INT(1)
@inbounds while lo < hi-INT(1)
m = (lo+hi)>>>1
if lt(o, v[m], x)
lo = m
Expand All @@ -186,10 +186,10 @@ end

# index of the last value of vector a that is less than or equal to x;
# returns 0 if x is less than all values of v.
function searchsortedlast(v::AbstractVector, x, lo::Int, hi::Int, o::Ordering)
lo = lo-1
hi = hi+1
@inbounds while lo < hi-1
function searchsortedlast(v::AbstractVector, x, lo::INT, hi::INT, o::Ordering) where INT<:Union{Int32,Int64}
lo = lo-INT(1)
hi = hi+INT(1)
@inbounds while lo < hi-INT(1)
m = (lo+hi)>>>1
if lt(o, x, v[m])
hi = m
Expand All @@ -203,10 +203,10 @@ end
# returns the range of indices of v equal to x
# if v does not contain x, returns a 0-length range
# indicating the insertion point of x
function searchsorted(v::AbstractVector, x, ilo::Int, ihi::Int, o::Ordering)
lo = ilo-1
hi = ihi+1
@inbounds while lo < hi-1
function searchsorted(v::AbstractVector, x, ilo::INT, ihi::INT, o::Ordering) where INT<:Union{Int32,Int64}
lo = ilo-INT(1)
hi = ihi+INT(1)
@inbounds while lo < hi-INT(1)
m = (lo+hi)>>>1
if lt(o, v[m], x)
lo = m
Expand Down
5 changes: 5 additions & 0 deletions test/sorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,8 @@ end
end
# https://discourse.julialang.org/t/sorting-big-int-with-v-0-6/1241
@test sort([big(3), big(2)]) == [big(2), big(3)]

module SortDatesTest30763
using Dates
@test searchsorted(Date(2019,1,1):Month(2):Date(2020,1,1), Date(2019,3,2)) == 3:2
end

0 comments on commit 584e42e

Please sign in to comment.