Skip to content

Commit

Permalink
Use compact parametric syntax in base/t*.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloferz committed Feb 8, 2017
1 parent 30feeb7 commit be21a97
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
10 changes: 5 additions & 5 deletions base/traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ immutable HasOrder <: TypeOrder end
immutable Unordered <: TypeOrder end

(::Type{TypeOrder})(instance) = TypeOrder(typeof(instance))
(::Type{TypeOrder}){T<:Real}(::Type{T}) = HasOrder()
(::Type{TypeOrder}){T}(::Type{T}) = Unordered()
(::Type{TypeOrder})(::Type{<:Real}) = HasOrder()
(::Type{TypeOrder})(::Type{<:Any}) = Unordered()

# trait for objects that support arithmetic
abstract TypeArithmetic
Expand All @@ -17,6 +17,6 @@ immutable ArithmeticOverflows <: TypeArithmetic end # most significant bits ca
immutable ArithmeticUnknown <: TypeArithmetic end

(::Type{TypeArithmetic})(instance) = TypeArithmetic(typeof(instance))
(::Type{TypeArithmetic}){T<:AbstractFloat}(::Type{T}) = ArithmeticRounds()
(::Type{TypeArithmetic}){T<:Integer}(::Type{T}) = ArithmeticOverflows()
(::Type{TypeArithmetic}){T}(::Type{T}) = ArithmeticUnknown()
(::Type{TypeArithmetic})(::Type{<:AbstractFloat}) = ArithmeticRounds()
(::Type{TypeArithmetic})(::Type{<:Integer}) = ArithmeticOverflows()
(::Type{TypeArithmetic})(::Type{<:Any}) = ArithmeticUnknown()
4 changes: 2 additions & 2 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ endof(t::Tuple) = length(t)
size(t::Tuple, d) = d==1 ? length(t) : throw(ArgumentError("invalid tuple dimension $d"))
getindex(t::Tuple, i::Int) = getfield(t, i)
getindex(t::Tuple, i::Real) = getfield(t, convert(Int, i))
getindex{T}(t::Tuple, r::AbstractArray{T,1}) = tuple([t[ri] for ri in r]...)
getindex(t::Tuple, r::AbstractArray{<:Any,1}) = tuple([t[ri] for ri in r]...)
getindex(t::Tuple, b::AbstractArray{Bool,1}) = length(b) == length(t) ? getindex(t,find(b)) : throw(BoundsError(t, b))

# returns new tuple; N.B.: becomes no-op if i is out-of-bounds
Expand Down Expand Up @@ -62,7 +62,7 @@ first(t::Tuple) = t[1]
# eltype

eltype(::Type{Tuple{}}) = Bottom
eltype{E, T <: Tuple{Vararg{E}}}(::Type{T}) = E
eltype(::Type{<:Tuple{Vararg{E}}}) where {E} = E

# version of tail that doesn't throw on empty tuples (used in array indexing)
safe_tail(t::Tuple) = tail(t)
Expand Down
24 changes: 12 additions & 12 deletions base/twiceprecision.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ convert{T}(::Type{TwicePrecision{T}}, x::TwicePrecision) =
convert{T<:Number}(::Type{T}, x::TwicePrecision{T}) = convert(T, x.hi + x.lo)
convert{T}(::Type{TwicePrecision{T}}, x::Number) = TwicePrecision{T}(convert(T, x), zero(T))

float{T<:AbstractFloat}(x::TwicePrecision{T}) = x
float(x::TwicePrecision{<:AbstractFloat}) = x
float(x::TwicePrecision) = TwicePrecision(float(x.hi), float(x.lo))

big(x::TwicePrecision) = big(x.hi) + big(x.lo)
Expand Down Expand Up @@ -172,15 +172,15 @@ end

step{T,R,S<:TwicePrecision}(r::StepRangeLen{T,R,S}) = convert(eltype(S), r.step)

start{T,R<:TwicePrecision,S<:TwicePrecision}(r::StepRangeLen{T,R,S}) = 1
done{T,R<:TwicePrecision,S<:TwicePrecision}(r::StepRangeLen{T,R,S}, i::Int) = length(r) < i
function next{T,R<:TwicePrecision,S<:TwicePrecision}(r::StepRangeLen{T,R,S}, i::Int)
start(r::StepRangeLen{<:Any,<:TwicePrecision,<:TwicePrecision}) = 1
done(r::StepRangeLen{<:Any,<:TwicePrecision,<:TwicePrecision}, i::Int) = length(r) < i
function next(r::StepRangeLen{<:Any,<:TwicePrecision,<:TwicePrecision}, i::Int)
@_inline_meta
unsafe_getindex(r, i), i+1
end

# This assumes that r.step has already been split so that (0:len-1)*r.step.hi is exact
function unsafe_getindex{T,R<:TwicePrecision,S<:TwicePrecision}(r::StepRangeLen{T,R,S}, i::Integer)
function unsafe_getindex{T}(r::StepRangeLen{T,<:TwicePrecision,<:TwicePrecision}, i::Integer)
# Very similar to _getindex_hiprec, but optimized to avoid a 2nd call to add2
@_inline_meta
u = i - r.offset
Expand All @@ -189,15 +189,15 @@ function unsafe_getindex{T,R<:TwicePrecision,S<:TwicePrecision}(r::StepRangeLen{
T(x_hi + (x_lo + (shift_lo + r.ref.lo)))
end

function _getindex_hiprec{T,R<:TwicePrecision,S<:TwicePrecision}(r::StepRangeLen{T,R,S}, i::Integer)
function _getindex_hiprec(r::StepRangeLen{<:Any,<:TwicePrecision,<:TwicePrecision}, i::Integer)
u = i - r.offset
shift_hi, shift_lo = u*r.step.hi, u*r.step.lo
x_hi, x_lo = add2(r.ref.hi, shift_hi)
x_hi, x_lo = add2(x_hi, x_lo + (shift_lo + r.ref.lo))
TwicePrecision(x_hi, x_lo)
end

function getindex{T,R<:TwicePrecision,S<:TwicePrecision,I<:Integer}(r::StepRangeLen{T,R,S}, s::OrdinalRange{I})
function getindex{T}(r::StepRangeLen{T,<:TwicePrecision,<:TwicePrecision}, s::OrdinalRange{<:Integer})
@_inline_meta
@boundscheck checkbounds(r, s)
soffset = 1 + round(Int, (r.offset - first(s))/step(s))
Expand All @@ -215,10 +215,10 @@ function getindex{T,R<:TwicePrecision,S<:TwicePrecision,I<:Integer}(r::StepRange
end
end

*{T<:Real,R<:TwicePrecision}(x::Real, r::StepRangeLen{T,R}) =
*(x::Real, r::StepRangeLen{<:Real,<:TwicePrecision}) =
StepRangeLen(x*r.ref, twiceprecision(x*r.step, nbitslen(r)), length(r), r.offset)
*{T<:Real,R<:TwicePrecision}(r::StepRangeLen{T,R}, x::Real) = x*r
/{T<:Real,R<:TwicePrecision}(r::StepRangeLen{T,R}, x::Real) =
*(r::StepRangeLen{<:Real,<:TwicePrecision}, x::Real) = x*r
/(r::StepRangeLen{<:Real,<:TwicePrecision}, x::Real) =
StepRangeLen(r.ref/x, twiceprecision(r.step/x, nbitslen(r)), length(r), r.offset)

convert{T<:AbstractFloat,R<:TwicePrecision,S<:TwicePrecision}(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen{T,R,S}) = r
Expand All @@ -232,11 +232,11 @@ convert{T<:Union{Float16,Float32,Float64}}(::Type{StepRangeLen{T}}, r::StepRange
convert{T<:Union{Float16,Float32,Float64}}(::Type{StepRangeLen{T}}, r::Range) =
_convertSRL(StepRangeLen{T,TwicePrecision{T},TwicePrecision{T}}, r)

function _convertSRL{T,R,S,I<:Integer}(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen{I})
function _convertSRL{T,R,S}(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen{<:Integer})
StepRangeLen{T,R,S}(R(r.ref), S(r.step), length(r), r.offset)
end

function _convertSRL{T,R,S,I<:Integer}(::Type{StepRangeLen{T,R,S}}, r::Range{I})
function _convertSRL{T,R,S}(::Type{StepRangeLen{T,R,S}}, r::Range{<:Integer})
StepRangeLen{T,R,S}(R(first(r)), S(step(r)), length(r))
end

Expand Down

0 comments on commit be21a97

Please sign in to comment.