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

Loosen searchsorted* index type (fixes #30763) #31633

Merged
merged 1 commit into from
Apr 29, 2019
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
33 changes: 18 additions & 15 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,12 @@ 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
m = (lo+hi)>>>1
function searchsortedfirst(v::AbstractVector, x, lo::T, hi::T, o::Ordering) where T<:Integer
u = T(1)
lo = lo - u
hi = hi + u
@inbounds while lo < hi - u
m = (lo + hi) >>> 1
if lt(o, v[m], x)
lo = m
else
Expand All @@ -186,11 +187,12 @@ 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
m = (lo+hi)>>>1
function searchsortedlast(v::AbstractVector, x, lo::T, hi::T, o::Ordering) where T<:Integer
u = T(1)
lo = lo - u
hi = hi + u
@inbounds while lo < hi - u
m = (lo + hi) >>> 1
if lt(o, x, v[m])
hi = m
else
Expand All @@ -203,11 +205,12 @@ 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
m = (lo+hi)>>>1
function searchsorted(v::AbstractVector, x, ilo::T, ihi::T, o::Ordering) where T<:Integer
u = T(1)
lo = ilo - u
hi = ihi + u
@inbounds while lo < hi - u
m = (lo + hi) >>> 1
if lt(o, v[m], x)
lo = m
elseif lt(o, x, v[m])
Expand Down
28 changes: 28 additions & 0 deletions test/sorting.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

module SortingTests

using Base.Order: Forward
using Random
using Test

@test sort([2,3,1]) == [1,2,3]
@test sort([2,3,1], rev=true) == [3,2,1]
Expand Down Expand Up @@ -371,3 +374,28 @@ 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)]

@testset "issue #30763" begin
for T in [:Int8, :Int16, :Int32, :Int64, :Int128, :UInt8, :UInt16, :UInt32, :UInt64, :UInt128]
@eval begin
struct T_30763{T}
n::T
end

Base.zero(::T_30763{$T}) = T_30763{$T}(0)
Base.convert(::Type{T_30763{$T}}, n::Integer) = T_30763{$T}($T(n))
Base.isless(a::T_30763{$T}, b::T_30763{$T}) = isless(a.n, b.n)
Base.:(-)(a::T_30763{$T}, b::T_30763{$T}) = T_30763{$T}(a.n - b.n)
Base.:(+)(a::T_30763{$T}, b::T_30763{$T}) = T_30763{$T}(a.n + b.n)
Base.:(*)(n::Integer, a::T_30763{$T}) = T_30763{$T}(n * a.n)
Base.rem(a::T_30763{$T}, b::T_30763{$T}) = T_30763{$T}(rem(a.n, b.n))

# The important part of this test is that the return type of length might be different from Int
Base.length(r::StepRange{T_30763{$T},T_30763{$T}}) = $T((last(r).n - first(r).n) ÷ step(r).n)

@test searchsorted(T_30763{$T}(1):T_30763{$T}(3), T_30763{$T}(2)) == 2:2
end
end
end

end