From c402273ac075f87f1e1d87847af3ff92844c4982 Mon Sep 17 00:00:00 2001 From: Darwin Darakananda Date: Mon, 26 Jan 2015 22:14:38 -0800 Subject: [PATCH] Add method for `searchsorted*` to handle Uint8 Add more types to `searchsorted` tests Testing with *Int128 types is triggering an compiler error, so they are commented out for now. We can add them back to the once #9947 is fixed Convert Unsigned to Signed before subtraction Fix Uint handling for `searchsortedlast` Re-enable `*Int128` tests for searchsorted` Previously blocked by #9947 Make `Float16`, `*Int128` promote to `Float32` --- base/float.jl | 2 +- base/sort.jl | 16 ++++++++++++++++ test/sorting.jl | 31 +++++++++++++++++++++---------- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/base/float.jl b/base/float.jl index 50012060a1a1e..aa648b3c9c9e6 100644 --- a/base/float.jl +++ b/base/float.jl @@ -1,6 +1,6 @@ ## conversions to floating-point ## convert(::Type{Float16}, x::Integer) = convert(Float16, convert(Float32,x)) -for t in (Int8,Int16,Int32,Int64,UInt8,UInt16,UInt32,UInt64) +for t in (Int8,Int16,Int32,Int64,Int128,UInt8,UInt16,UInt32,UInt64,UInt128) @eval promote_rule(::Type{Float16}, ::Type{$t}) = Float32 end promote_rule(::Type{Float16}, ::Type{Bool}) = Float16 diff --git a/base/sort.jl b/base/sort.jl index e8f8b7480e2bc..bb2b56b5f41a8 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -210,6 +210,22 @@ function searchsortedfirst{T<:Integer}(a::Range{T}, x::Real, o::DirectOrdering) end end +function searchsortedfirst{T<:Integer}(a::Range{T}, x::Unsigned, o::DirectOrdering) + if step(a) == 0 + lt(o, first(a), x) ? length(a)+1 : 1 + else + max(min(-fld(first(a)-signed(x),step(a))+1,length(a)+1),1) + end +end + +function searchsortedlast{T<:Integer}(a::Range{T}, x::Unsigned, o::DirectOrdering) + if step(a) == 0 + lt(o, x, first(a)) ? 0 : length(a) + else + max(min(fld(signed(x)-first(a),step(a))+1,length(a)),0) + end +end + searchsorted{T<:Real}(a::Range{T}, x::Real, o::DirectOrdering) = searchsortedfirst(a,x,o):searchsortedlast(a,x,o) diff --git a/test/sorting.jl b/test/sorting.jl index e170c6215ea23..0a77c2d20eabb 100644 --- a/test/sorting.jl +++ b/test/sorting.jl @@ -18,16 +18,27 @@ end @test sum(randperm(6)) == 21 @test nthperm([0,1,2],3) == [1,0,2] -@test searchsorted([1, 1, 2, 2, 3, 3], 0) == 1:0 -@test searchsorted([1, 1, 2, 2, 3, 3], 1) == 1:2 -@test searchsorted([1, 1, 2, 2, 3, 3], 2) == 3:4 -@test searchsorted([1, 1, 2, 2, 3, 3], 4) == 7:6 -@test searchsorted([1.0, 1, 2, 2, 3, 3], 2.5) == 5:4 - -@test searchsorted([1:10], 1, by=(x -> x >= 5)) == 1:4 -@test searchsorted([1:10], 10, by=(x -> x >= 5)) == 5:10 -@test searchsorted([1:5, 1:5, 1:5], 1, 6, 10, Base.Order.Forward) == 6:6 -@test searchsorted(ones(15), 1, 6, 10, Base.Order.Forward) == 6:10 +numTypes = [ Int8, Int16, Int32, Int64, Int128, + UInt8, UInt16, UInt32, UInt64, UInt128, + Float16, Float32, Float64, BigInt, BigFloat] + +for R in numTypes, T in numTypes + @test searchsorted(R[1, 1, 2, 2, 3, 3], T(0)) == 1:0 + @test searchsorted(R[1, 1, 2, 2, 3, 3], T(1)) == 1:2 + @test searchsorted(R[1, 1, 2, 2, 3, 3], T(2)) == 3:4 + @test searchsorted(R[1, 1, 2, 2, 3, 3], T(4)) == 7:6 + @test searchsorted(R[1, 1, 2, 2, 3, 3], 2.5) == 5:4 + + @test searchsorted(1:3, T(0)) == 1:0 + @test searchsorted(1:3, T(1)) == 1:1 + @test searchsorted(1:3, T(2)) == 2:2 + @test searchsorted(1:3, T(4)) == 4:3 + + @test searchsorted(R[1:10], T(1), by=(x -> x >= 5)) == 1:4 + @test searchsorted(R[1:10], T(10), by=(x -> x >= 5)) == 5:10 + @test searchsorted(R[1:5, 1:5, 1:5], T(1), 6, 10, Base.Order.Forward) == 6:6 + @test searchsorted(ones(R, 15), T(1), 6, 10, Base.Order.Forward) == 6:10 +end for (rg,I) in [(49:57,47:59), (1:2:17,-1:19), (-3:0.5:2,-5:.5:4)] rg_r = reverse(rg)