Skip to content

Commit

Permalink
Use compact parametric syntax in base/s*.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloferz committed Feb 8, 2017
1 parent 6eb0406 commit 30feeb7
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 59 deletions.
4 changes: 2 additions & 2 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ delete!(s::Set, x) = (delete!(s.dict, x); s)
copy(s::Set) = union!(similar(s), s)

sizehint!(s::Set, newsz) = (sizehint!(s.dict, newsz); s)
empty!{T}(s::Set{T}) = (empty!(s.dict); s)
empty!(s::Set) = (empty!(s.dict); s)
rehash!(s::Set) = (rehash!(s.dict); s)

start(s::Set) = start(s.dict)
Expand Down Expand Up @@ -250,4 +250,4 @@ function hash(s::Set, h::UInt)
end

convert{T}(::Type{Set{T}}, s::Set{T}) = s
convert{T,S}(::Type{Set{T}}, x::Set{S}) = Set{T}(x)
convert{T}(::Type{Set{T}}, x::Set) = Set{T}(x)
4 changes: 2 additions & 2 deletions base/sharedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ typealias SharedMatrix{T} SharedArray{T,2}
length(S::SharedArray) = prod(S.dims)
size(S::SharedArray) = S.dims
ndims(S::SharedArray) = length(S.dims)
linearindexing{S<:SharedArray}(::Type{S}) = LinearFast()
linearindexing(::Type{<:SharedArray}) = LinearFast()

function reshape{T,N}(a::SharedArray{T}, dims::NTuple{N,Int})
if length(a) != prod(dims)
Expand Down Expand Up @@ -420,7 +420,7 @@ function serialize(s::AbstractSerializer, S::SharedArray)
end
end

function deserialize{T,N}(s::AbstractSerializer, t::Type{SharedArray{T,N}})
function deserialize(s::AbstractSerializer, t::Type{<:SharedArray})
S = invoke(deserialize, Tuple{AbstractSerializer,DataType}, s, t)
init_loc_flds(S, true)
S
Expand Down
2 changes: 1 addition & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ show(io::IO, n::Signed) = (write(io, dec(n)); nothing)
show(io::IO, n::Unsigned) = print(io, "0x", hex(n,sizeof(n)<<1))
print(io::IO, n::Unsigned) = print(io, dec(n))

show{T}(io::IO, p::Ptr{T}) = print(io, typeof(p), " @0x$(hex(UInt(p), Sys.WORD_SIZE>>2))")
show(io::IO, p::Ptr) = print(io, typeof(p), " @0x$(hex(UInt(p), Sys.WORD_SIZE>>2))")

function show(io::IO, p::Pair)
if typeof(p.first) != typeof(p).parameters[1] ||
Expand Down
2 changes: 1 addition & 1 deletion base/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
abstract IPAddr

Base.isless{T<:IPAddr}(a::T, b::T) = isless(a.host, b.host)
Base.convert{T<:Integer}(dt::Type{T}, ip::IPAddr) = dt(ip.host)
Base.convert(dt::Type{<:Integer}, ip::IPAddr) = dt(ip.host)

immutable IPv4 <: IPAddr
host::UInt32
Expand Down
58 changes: 29 additions & 29 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function searchsorted(v::AbstractVector, x, ilo::Int, ihi::Int, o::Ordering)
return (lo + 1) : (hi - 1)
end

function searchsortedlast{T<:Real}(a::Range{T}, x::Real, o::DirectOrdering)
function searchsortedlast(a::Range{<:Real}, x::Real, o::DirectOrdering)
if step(a) == 0
lt(o, x, first(a)) ? 0 : length(a)
else
Expand All @@ -144,7 +144,7 @@ function searchsortedlast{T<:Real}(a::Range{T}, x::Real, o::DirectOrdering)
end
end

function searchsortedfirst{T<:Real}(a::Range{T}, x::Real, o::DirectOrdering)
function searchsortedfirst(a::Range{<:Real}, x::Real, o::DirectOrdering)
if step(a) == 0
lt(o, first(a), x) ? length(a) + 1 : 1
else
Expand All @@ -153,23 +153,23 @@ function searchsortedfirst{T<:Real}(a::Range{T}, x::Real, o::DirectOrdering)
end
end

function searchsortedlast{T<:Integer}(a::Range{T}, x::Real, o::DirectOrdering)
function searchsortedlast(a::Range{<:Integer}, x::Real, o::DirectOrdering)
if step(a) == 0
lt(o, x, first(a)) ? 0 : length(a)
else
clamp( fld(floor(Integer, x) - first(a), step(a)) + 1, 0, length(a))
end
end

function searchsortedfirst{T<:Integer}(a::Range{T}, x::Real, o::DirectOrdering)
function searchsortedfirst(a::Range{<:Integer}, x::Real, o::DirectOrdering)
if step(a) == 0
lt(o, first(a), x) ? length(a)+1 : 1
else
clamp(-fld(floor(Integer, -x) + first(a), step(a)) + 1, 1, length(a) + 1)
end
end

function searchsortedfirst{T<:Integer}(a::Range{T}, x::Unsigned, o::DirectOrdering)
function searchsortedfirst(a::Range{<:Integer}, x::Unsigned, o::DirectOrdering)
if lt(o, first(a), x)
if step(a) == 0
length(a) + 1
Expand All @@ -181,7 +181,7 @@ function searchsortedfirst{T<:Integer}(a::Range{T}, x::Unsigned, o::DirectOrderi
end
end

function searchsortedlast{T<:Integer}(a::Range{T}, x::Unsigned, o::DirectOrdering)
function searchsortedlast(a::Range{<:Integer}, x::Unsigned, o::DirectOrdering)
if lt(o, x, first(a))
0
elseif step(a) == 0
Expand All @@ -191,7 +191,7 @@ function searchsortedlast{T<:Integer}(a::Range{T}, x::Unsigned, o::DirectOrderin
end
end

searchsorted{T<:Real}(a::Range{T}, x::Real, o::DirectOrdering) =
searchsorted(a::Range{<:Real}, x::Real, o::DirectOrdering) =
searchsortedfirst(a, x, o) : searchsortedlast(a, x, o)

for s in [:searchsortedfirst, :searchsortedlast, :searchsorted]
Expand Down Expand Up @@ -414,7 +414,7 @@ end
## generic sorting methods ##

defalg(v::AbstractArray) = DEFAULT_STABLE
defalg{T<:Number}(v::AbstractArray{T}) = DEFAULT_UNSTABLE
defalg(v::AbstractArray{<:Number}) = DEFAULT_UNSTABLE

function sort!(v::AbstractVector, alg::Algorithm, order::Ordering)
inds = indices(v,1)
Expand Down Expand Up @@ -455,7 +455,7 @@ function sort!(v::AbstractVector;
end

# sort! for vectors of few unique integers
function sort_int_range!{T<:Integer}(x::Vector{T}, rangelen, minval)
function sort_int_range!(x::Vector{<:Integer}, rangelen, minval)
offs = 1 - minval
n = length(x)

Expand Down Expand Up @@ -489,13 +489,13 @@ sort(v::AbstractVector; kws...) = sort!(copymutable(v); kws...)
selectperm(v::AbstractVector, k::Union{Integer,OrdinalRange}; kwargs...) =
selectperm!(similar(Vector{eltype(k)}, indices(v,1)), v, k; kwargs..., initialized=false)

function selectperm!{I<:Integer}(ix::AbstractVector{I}, v::AbstractVector,
k::Union{Int, OrdinalRange};
lt::Function=isless,
by::Function=identity,
rev::Bool=false,
order::Ordering=Forward,
initialized::Bool=false)
function selectperm!(ix::AbstractVector{<:Integer}, v::AbstractVector,
k::Union{Int, OrdinalRange};
lt::Function=isless,
by::Function=identity,
rev::Bool=false,
order::Ordering=Forward,
initialized::Bool=false)
if !initialized
@inbounds for i = indices(ix,1)
ix[i] = i
Expand Down Expand Up @@ -554,13 +554,13 @@ end
Like [`sortperm`](@ref), but accepts a preallocated index vector `ix`. If `initialized` is `false`
(the default), ix is initialized to contain the values `1:length(v)`.
"""
function sortperm!{I<:Integer}(x::AbstractVector{I}, v::AbstractVector;
alg::Algorithm=DEFAULT_UNSTABLE,
lt=isless,
by=identity,
rev::Bool=false,
order::Ordering=Forward,
initialized::Bool=false)
function sortperm!(x::AbstractVector{<:Integer}, v::AbstractVector;
alg::Algorithm=DEFAULT_UNSTABLE,
lt=isless,
by=identity,
rev::Bool=false,
order::Ordering=Forward,
initialized::Bool=false)
if indices(x,1) != indices(v,1)
throw(ArgumentError("index vector must have the same indices as the source vector, $(indices(x,1)) != $(indices(v,1))"))
end
Expand All @@ -573,7 +573,7 @@ function sortperm!{I<:Integer}(x::AbstractVector{I}, v::AbstractVector;
end

# sortperm for vectors of few unique integers
function sortperm_int_range{T<:Integer}(x::Vector{T}, rangelen, minval)
function sortperm_int_range(x::Vector{<:Integer}, rangelen, minval)
offs = 1 - minval
n = length(x)

Expand Down Expand Up @@ -670,7 +670,7 @@ function sortcols(A::AbstractMatrix; kws...)
A[:,p]
end

function slicetypeof{T,N}(A::AbstractArray{T,N}, i1, i2)
function slicetypeof{T}(A::AbstractArray{T}, i1, i2)
I = map(slice_dummy, to_indices(A, (i1, i2)))
fast = isa(linearindexing(viewindexing(I), linearindexing(A)), LinearFast)
SubArray{T,1,typeof(A),typeof(I),fast}
Expand Down Expand Up @@ -738,8 +738,8 @@ end

nans2end!(v::AbstractVector, o::ForwardOrdering) = nans2right!(v,o)
nans2end!(v::AbstractVector, o::ReverseOrdering) = nans2left!(v,o)
nans2end!{O<:ForwardOrdering}(v::AbstractVector{Int}, o::Perm{O}) = nans2right!(v,o)
nans2end!{O<:ReverseOrdering}(v::AbstractVector{Int}, o::Perm{O}) = nans2left!(v,o)
nans2end!(v::AbstractVector{Int}, o::Perm{<:ForwardOrdering}) = nans2right!(v,o)
nans2end!(v::AbstractVector{Int}, o::Perm{<:ReverseOrdering}) = nans2left!(v,o)

issignleft(o::ForwardOrdering, x::Floats) = lt(o, x, zero(x))
issignleft(o::ReverseOrdering, x::Floats) = lt(o, x, -zero(x))
Expand All @@ -763,8 +763,8 @@ end
fpsort!(v::AbstractVector, a::Sort.PartialQuickSort, o::Ordering) =
sort!(v, first(indices(v,1)), last(indices(v,1)), a, o)

sort!{T<:Floats}(v::AbstractVector{T}, a::Algorithm, o::DirectOrdering) = fpsort!(v,a,o)
sort!{O<:DirectOrdering,T<:Floats}(v::Vector{Int}, a::Algorithm, o::Perm{O,Vector{T}}) = fpsort!(v,a,o)
sort!(v::AbstractVector{<:Floats}, a::Algorithm, o::DirectOrdering) = fpsort!(v,a,o)
sort!(v::Vector{Int}, a::Algorithm, o::Perm{<:DirectOrdering,<:Vector{<:Floats}}) = fpsort!(v,a,o)

end # module Sort.Float

Expand Down
28 changes: 14 additions & 14 deletions base/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ mean(iterable) = mean(identity, iterable)
mean(f::Callable, A::AbstractArray) = sum(f, A) / _length(A)
mean(A::AbstractArray) = sum(A) / _length(A)

function mean!{T}(R::AbstractArray{T}, A::AbstractArray)
function mean!(R::AbstractArray, A::AbstractArray)
sum!(R, A; init=true)
scale!(R, _length(R) / _length(A))
return R
end

momenttype{T}(::Type{T}) = typeof((zero(T) + zero(T)) / 2)
momenttype(::Type{Float32}) = Float32
momenttype{T<:Union{Float64,Int32,Int64,UInt32,UInt64}}(::Type{T}) = Float64
momenttype(::Type{<:Union{Float64,Int32,Int64,UInt32,UInt64}}) = Float64

"""
mean(v[, region])
Expand Down Expand Up @@ -100,7 +100,7 @@ centralize_sumabs2(A::AbstractArray, m::Number) =
centralize_sumabs2(A::AbstractArray, m::Number, ifirst::Int, ilast::Int) =
mapreduce_impl(centralizedabs2fun(m), +, A, ifirst, ilast)

function centralize_sumabs2!{S,T,N}(R::AbstractArray{S}, A::AbstractArray{T,N}, means::AbstractArray)
function centralize_sumabs2!{S}(R::AbstractArray{S}, A::AbstractArray, means::AbstractArray)
# following the implementation of _mapreducedim! at base/reducedim.jl
lsiz = check_reducedims(R,A)
isempty(R) || fill!(R, zero(S))
Expand Down Expand Up @@ -279,7 +279,7 @@ stdm(iterable, m::Number; corrected::Bool=true) =

# auxiliary functions

_conj{T<:Real}(x::AbstractArray{T}) = x
_conj(x::AbstractArray{<:Real}) = x
_conj(x::AbstractArray) = conj(x)

_getnobs(x::AbstractVector, vardim::Int) = _length(x)
Expand Down Expand Up @@ -337,7 +337,7 @@ is scaled with `n-1`, whereas the sum is scaled with `n` if `corrected` is `fals
"""
cov(x::AbstractVector, corrected::Bool) = covm(x, Base.mean(x), corrected)
# This ugly hack is necessary to make the method below considered more specific than the deprecated method. When the old keyword version has been completely deprecated, these two methods can be merged
cov{T<:AbstractVector}(x::T) = covm(x, Base.mean(x), true)
cov(x::AbstractVector) = covm(x, Base.mean(x), true)

"""
cov(X[, vardim=1, corrected=true])
Expand All @@ -349,7 +349,7 @@ if `corrected` is `false` where `n = size(X, vardim)`.
cov(X::AbstractMatrix, vardim::Int, corrected::Bool=true) =
covm(X, _vmean(X, vardim), vardim, corrected)
# This ugly hack is necessary to make the method below considered more specific than the deprecated method. When the old keyword version has been completely deprecated, these two methods can be merged
cov{T<:AbstractMatrix}(X::T) = cov(X, 1, true)
cov(X::AbstractMatrix) = cov(X, 1, true)

"""
cov(x, y[, corrected=true])
Expand All @@ -361,7 +361,7 @@ where `n = length(x) = length(y)`.
cov(x::AbstractVector, y::AbstractVector, corrected::Bool) =
covm(x, Base.mean(x), y, Base.mean(y), corrected)
# This ugly hack is necessary to make the method below considered more specific than the deprecated method. When the old keyword version has been completely deprecated, these two methods can be merged
cov{T<:AbstractVector,S<:AbstractVector}(x::T, y::S) =
cov(x::AbstractVector, y::AbstractVector) =
covm(x, Base.mean(x), y, Base.mean(y), true)

"""
Expand Down Expand Up @@ -476,7 +476,7 @@ corm(x::AbstractVecOrMat, xmean, y::AbstractVecOrMat, ymean, vardim::Int=1) =
Return the number one.
"""
cor{T<:AbstractVector}(x::T) = one(real(eltype(x)))
cor(x::AbstractVector) = one(real(eltype(x)))
# This ugly hack is necessary to make the method below considered more specific than the deprecated method. When the old keyword version has been completely deprecated, these two methods can be merged

"""
Expand All @@ -486,14 +486,14 @@ Compute the Pearson correlation matrix of the matrix `X` along the dimension `va
"""
cor(X::AbstractMatrix, vardim::Int) = corm(X, _vmean(X, vardim), vardim)
# This ugly hack is necessary to make the method below considered more specific than the deprecated method. When the old keyword version has been completely deprecated, these two methods can be merged
cor{T<:AbstractMatrix}(X::T) = cor(X, 1)
cor(X::AbstractMatrix) = cor(X, 1)

"""
cor(x, y)
Compute the Pearson correlation between the vectors `x` and `y`.
"""
cor{T<:AbstractVector,S<:AbstractVector}(x::T, y::S) = corm(x, Base.mean(x), y, Base.mean(y))
cor(x::AbstractVector, y::AbstractVector) = corm(x, Base.mean(x), y, Base.mean(y))
# This ugly hack is necessary to make the method below considered more specific than the deprecated method. When the old keyword version has been completely deprecated, these two methods can be merged

"""
Expand Down Expand Up @@ -566,9 +566,9 @@ middle(a::AbstractArray) = ((v1, v2) = extrema(a); middle(v1, v2))
Like [`median`](@ref), but may overwrite the input vector.
"""
function median!{T}(v::AbstractVector{T})
function median!(v::AbstractVector)
isempty(v) && throw(ArgumentError("median of an empty array is undefined, $(repr(v))"))
if T<:AbstractFloat
if eltype(v)<:AbstractFloat
@inbounds for x in v
isnan(x) && return x
end
Expand All @@ -583,7 +583,7 @@ function median!{T}(v::AbstractVector{T})
return middle(m[1], m[2])
end
end
median!{T}(v::AbstractArray{T}) = median!(vec(v))
median!(v::AbstractArray) = median!(vec(v))
median{T}(v::AbstractArray{T}) = median!(copy!(Array{T,1}(_length(v)), v))

"""
Expand All @@ -598,7 +598,7 @@ equivalent to calculating mean of two median elements.
Julia does not ignore `NaN` values in the computation. For applications requiring the
handling of missing data, the `DataArrays.jl` package is recommended.
"""
median{T}(v::AbstractArray{T}, region) = mapslices(median!, v, region)
median(v::AbstractArray, region) = mapslices(median!, v, region)

# for now, use the R/S definition of quantile; may want variants later
# see ?quantile in R -- this is type 7
Expand Down
20 changes: 10 additions & 10 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ end
# `CartesianIndex`, and if so, we punt and keep two layers of indirection.
unsafe_view(V::SubArray, I::ViewIndex...) = (@_inline_meta; _maybe_reindex(V, I))
_maybe_reindex(V, I) = (@_inline_meta; _maybe_reindex(V, I, I))
_maybe_reindex{C<:AbstractCartesianIndex}(V, I, ::Tuple{AbstractArray{C}, Vararg{Any}}) =
_maybe_reindex(V, I, ::Tuple{AbstractArray{<:AbstractCartesianIndex}, Vararg{Any}}) =
(@_inline_meta; SubArray(V, I))
# But allow arrays of CartesianIndex{1}; they behave just like arrays of Ints
_maybe_reindex{C<:AbstractCartesianIndex{1}}(V, I, A::Tuple{AbstractArray{C}, Vararg{Any}}) =
_maybe_reindex(V, I, A::Tuple{AbstractArray{<:AbstractCartesianIndex{1}}, Vararg{Any}}) =
(@_inline_meta; _maybe_reindex(V, I, tail(A)))
_maybe_reindex(V, I, A::Tuple{Any, Vararg{Any}}) = (@_inline_meta; _maybe_reindex(V, I, tail(A)))
function _maybe_reindex(V, I, ::Tuple{})
Expand Down Expand Up @@ -221,12 +221,12 @@ function setindex!(V::FastContiguousSubArray, x, i::Int)
V
end

linearindexing{T<:FastSubArray}(::Type{T}) = LinearFast()
linearindexing{T<:SubArray}(::Type{T}) = LinearSlow()
linearindexing(::Type{<:FastSubArray}) = LinearFast()
linearindexing(::Type{<:SubArray}) = LinearSlow()

# Strides are the distance between adjacent elements in a given dimension,
# so they are well-defined even for non-linear memory layouts
strides{T,N,P,I}(V::SubArray{T,N,P,I}) = substrides(V.parent, V.indexes)
strides(V::SubArray) = substrides(V.parent, V.indexes)

substrides(parent, I::Tuple) = substrides(1, parent, 1, I)
substrides(s, parent, dim, ::Tuple{}) = ()
Expand All @@ -247,8 +247,8 @@ compute_stride1(s, inds, I::Tuple{Slice, Vararg{Any}}) = s
compute_stride1(s, inds, I::Tuple{Any, Vararg{Any}}) = throw(ArgumentError("invalid strided index type $(typeof(I[1]))"))

iscontiguous(A::SubArray) = iscontiguous(typeof(A))
iscontiguous{S<:SubArray}(::Type{S}) = false
iscontiguous{F<:FastContiguousSubArray}(::Type{F}) = true
iscontiguous(::Type{<:SubArray}) = false
iscontiguous(::Type{<:FastContiguousSubArray}) = true

first_index(V::FastSubArray) = V.offset1 + V.stride1 # cached for fast linear SubArrays
function first_index(V::SubArray)
Expand Down Expand Up @@ -294,16 +294,16 @@ _find_extended_dims(dims, inds, dim, ::ScalarIndex, I...) =
_find_extended_dims(dims, inds, dim, i1, I...) =
(@_inline_meta; _find_extended_dims((dims..., dim), (inds..., i1), dim+1, I...))

unsafe_convert{T,N,P,I<:Tuple{Vararg{RangeIndex}}}(::Type{Ptr{T}}, V::SubArray{T,N,P,I}) =
unsafe_convert{T,N,P}(::Type{Ptr{T}}, V::SubArray{T,N,P,<:Tuple{Vararg{RangeIndex}}}) =
unsafe_convert(Ptr{T}, V.parent) + (first_index(V)-1)*sizeof(T)

pointer(V::FastSubArray, i::Int) = pointer(V.parent, V.offset1 + V.stride1*i)
pointer(V::FastContiguousSubArray, i::Int) = pointer(V.parent, V.offset1 + i)
pointer(V::SubArray, i::Int) = _pointer(V, i)
_pointer{T}(V::SubArray{T,1}, i::Int) = pointer(V, (i,))
_pointer(V::SubArray{<:Any,1}, i::Int) = pointer(V, (i,))
_pointer(V::SubArray, i::Int) = pointer(V, ind2sub(indices(V), i))

function pointer{T,N,P<:Array,I<:Tuple{Vararg{RangeIndex}}}(V::SubArray{T,N,P,I}, is::Tuple{Vararg{Int}})
function pointer{T,N}(V::SubArray{T,N,<:Array,<:Tuple{Vararg{RangeIndex}}}, is::Tuple{Vararg{Int}})
index = first_index(V)
strds = strides(V)
for d = 1:length(is)
Expand Down

0 comments on commit 30feeb7

Please sign in to comment.