Skip to content

Commit

Permalink
Where syntax for indices and cat function
Browse files Browse the repository at this point in the history
  • Loading branch information
musm committed Apr 19, 2017
1 parent 3b8a5f7 commit a961492
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 39 deletions.
50 changes: 25 additions & 25 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ julia> indices(A,2)
Base.OneTo(6)
```
"""
function indices{T,N}(A::AbstractArray{T,N}, d)
function indices(A::AbstractArray{T,N}, d) where {T,N}
@_inline_meta
d <= N ? indices(A)[d] : OneTo(1)
end
Expand Down Expand Up @@ -901,7 +901,7 @@ function _getindex(::IndexLinear, A::AbstractArray, I::Int...)
end
_to_linear_index(A::AbstractArray, i::Int) = i
_to_linear_index(A::AbstractVector, i::Int, I::Int...) = i # TODO: DEPRECATE FOR #14770
_to_linear_index{T,N}(A::AbstractArray{T,N}, I::Vararg{Int,N}) = (@_inline_meta; sub2ind(A, I...))
_to_linear_index(A::AbstractArray{T,N}, I::Vararg{Int,N}) where {T,N} = (@_inline_meta; sub2ind(A, I...))
_to_linear_index(A::AbstractArray) = 1 # TODO: DEPRECATE FOR #14770
_to_linear_index(A::AbstractArray, I::Int...) = (@_inline_meta; sub2ind(A, I...)) # TODO: DEPRECATE FOR #14770

Expand All @@ -916,16 +916,16 @@ function _getindex(::IndexCartesian, A::AbstractArray, I::Int...)
@inbounds r = getindex(A, _to_subscript_indices(A, I...)...)
r
end
function _getindex{T,N}(::IndexCartesian, A::AbstractArray{T,N}, I::Vararg{Int, N})
function _getindex(::IndexCartesian, A::AbstractArray{T,N}, I::Vararg{Int, N}) where {T,N}
@_propagate_inbounds_meta
getindex(A, I...)
end
_to_subscript_indices(A::AbstractArray, i::Int) = (@_inline_meta; _unsafe_ind2sub(A, i))
_to_subscript_indices{T,N}(A::AbstractArray{T,N}) = (@_inline_meta; fill_to_length((), 1, Val{N})) # TODO: DEPRECATE FOR #14770
_to_subscript_indices{T}(A::AbstractArray{T,0}) = () # TODO: REMOVE FOR #14770
_to_subscript_indices{T}(A::AbstractArray{T,0}, i::Int) = () # TODO: REMOVE FOR #14770
_to_subscript_indices{T}(A::AbstractArray{T,0}, I::Int...) = () # TODO: DEPRECATE FOR #14770
function _to_subscript_indices{T,N}(A::AbstractArray{T,N}, I::Int...) # TODO: DEPRECATE FOR #14770
_to_subscript_indices(A::AbstractArray{T,N}) where {T,N} = (@_inline_meta; fill_to_length((), 1, Val{N})) # TODO: DEPRECATE FOR #14770
_to_subscript_indices(A::AbstractArray{T,0}) where {T} = () # TODO: REMOVE FOR #14770
_to_subscript_indices(A::AbstractArray{T,0}, i::Int) where {T} = () # TODO: REMOVE FOR #14770
_to_subscript_indices(A::AbstractArray{T,0}, I::Int...) where {T} = () # TODO: DEPRECATE FOR #14770
function _to_subscript_indices(A::AbstractArray{T,N}, I::Int...) where {T,N} # TODO: DEPRECATE FOR #14770
@_inline_meta
J, Jrem = IteratorsMD.split(I, Val{N})
_to_subscript_indices(A, J, Jrem)
Expand All @@ -946,7 +946,7 @@ function __to_subscript_indices(A::AbstractArray,
(J..., map(first, tail(_remaining_size(J, indices(A))))...)
end
_to_subscript_indices(A, J::Tuple, Jrem::Tuple) = J # already bounds-checked, safe to drop
_to_subscript_indices{T,N}(A::AbstractArray{T,N}, I::Vararg{Int,N}) = I
_to_subscript_indices(A::AbstractArray{T,N}, I::Vararg{Int,N}) where {T,N} = I
_remaining_size(::Tuple{Any}, t::Tuple) = t
_remaining_size(h::Tuple, t::Tuple) = (@_inline_meta; _remaining_size(tail(h), tail(t)))
_unsafe_ind2sub(::Tuple{}, i) = () # ind2sub may throw(BoundsError()) in this case
Expand Down Expand Up @@ -979,7 +979,7 @@ function _setindex!(::IndexLinear, A::AbstractArray, v, I::Int...)
end

# IndexCartesian Scalar indexing
function _setindex!{T,N}(::IndexCartesian, A::AbstractArray{T,N}, v, I::Vararg{Int, N})
function _setindex!(::IndexCartesian, A::AbstractArray{T,N}, v, I::Vararg{Int, N}) where {T,N}
@_propagate_inbounds_meta
setindex!(A, v, I...)
end
Expand Down Expand Up @@ -1051,8 +1051,8 @@ promote_eltype(v1, vs...) = promote_type(eltype(v1), promote_eltype(vs...))
#TODO: ERROR CHECK
cat(catdim::Integer) = Array{Any,1}(0)

typed_vcat{T}(::Type{T}) = Array{T,1}(0)
typed_hcat{T}(::Type{T}) = Array{T,1}(0)
typed_vcat(::Type{T}) where {T} = Array{T,1}(0)
typed_hcat(::Type{T}) where {T} = Array{T,1}(0)

## cat: special cases
vcat(X::T...) where {T} = T[ X[i] for i=1:length(X) ]
Expand All @@ -1062,13 +1062,13 @@ hcat(X::T...) where {T<:Number} = T[ X[j] for i=1:1, j=1:length(X) ]

vcat(X::Number...) = hvcat_fill(Array{promote_typeof(X...)}(length(X)), X)
hcat(X::Number...) = hvcat_fill(Array{promote_typeof(X...)}(1,length(X)), X)
typed_vcat{T}(::Type{T}, X::Number...) = hvcat_fill(Array{T,1}(length(X)), X)
typed_hcat{T}(::Type{T}, X::Number...) = hvcat_fill(Array{T,2}(1,length(X)), X)
typed_vcat(::Type{T}, X::Number...) where {T} = hvcat_fill(Array{T,1}(length(X)), X)
typed_hcat(::Type{T}, X::Number...) where {T} = hvcat_fill(Array{T,2}(1,length(X)), X)

vcat(V::AbstractVector...) = typed_vcat(promote_eltype(V...), V...)
vcat(V::AbstractVector{T}...) where {T} = typed_vcat(T, V...)

function typed_vcat{T}(::Type{T}, V::AbstractVector...)
function typed_vcat(::Type{T}, V::AbstractVector...) where T
n::Int = 0
for Vk in V
n += length(Vk)
Expand All @@ -1087,7 +1087,7 @@ end
hcat(A::AbstractVecOrMat...) = typed_hcat(promote_eltype(A...), A...)
hcat(A::AbstractVecOrMat{T}...) where {T} = typed_hcat(T, A...)

function typed_hcat{T}(::Type{T}, A::AbstractVecOrMat...)
function typed_hcat(::Type{T}, A::AbstractVecOrMat...) where T
nargs = length(A)
nrows = size(A[1], 1)
ncols = 0
Expand Down Expand Up @@ -1124,7 +1124,7 @@ end
vcat(A::AbstractMatrix...) = typed_vcat(promote_eltype(A...), A...)
vcat(A::AbstractMatrix{T}...) where {T} = typed_vcat(T, A...)

function typed_vcat{T}(::Type{T}, A::AbstractMatrix...)
function typed_vcat(::Type{T}, A::AbstractMatrix...) where T
nargs = length(A)
nrows = sum(a->size(a, 1), A)::Int
ncols = size(A[1], 2)
Expand Down Expand Up @@ -1200,7 +1200,7 @@ function cat_t(dims, T::Type, X...)
return _cat(A, shape, catdims, X...)
end

function _cat{N}(A, shape::NTuple{N}, catdims, X...)
function _cat(A, shape::NTuple{N}, catdims, X...) where N
offsets = zeros(Int, N)
inds = Vector{UnitRange{Int}}(N)
concat = copy!(zeros(Bool, N), catdims)
Expand Down Expand Up @@ -1295,7 +1295,7 @@ hcat(X...) = cat(Val{2}, X...)
typed_vcat(T::Type, X...) = cat_t(Val{1}, T, X...)
typed_hcat(T::Type, X...) = cat_t(Val{2}, T, X...)

cat{T}(catdims, A::AbstractArray{T}...) = cat_t(catdims, T, A...)
cat(catdims, A::AbstractArray{T}...) where {T} = cat_t(catdims, T, A...)

# The specializations for 1 and 2 inputs are important
# especially when running with --inline=no, see #11158
Expand Down Expand Up @@ -1362,9 +1362,9 @@ If the first argument is a single integer `n`, then all block rows are assumed t
block columns.
"""
hvcat(rows::Tuple{Vararg{Int}}, xs::AbstractVecOrMat...) = typed_hvcat(promote_eltype(xs...), rows, xs...)
hvcat{T}(rows::Tuple{Vararg{Int}}, xs::AbstractVecOrMat{T}...) = typed_hvcat(T, rows, xs...)
hvcat(rows::Tuple{Vararg{Int}}, xs::AbstractVecOrMat{T}...) where {T} = typed_hvcat(T, rows, xs...)

function typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}, as::AbstractVecOrMat...)
function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, as::AbstractVecOrMat...) where T
nbr = length(rows) # number of block rows

nc = 0
Expand Down Expand Up @@ -1408,9 +1408,9 @@ function typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}, as::AbstractVecOrMa
end

hvcat(rows::Tuple{Vararg{Int}}) = []
typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}) = Array{T,1}(0)
typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}) where {T} = Array{T,1}(0)

function hvcat{T<:Number}(rows::Tuple{Vararg{Int}}, xs::T...)
function hvcat(rows::Tuple{Vararg{Int}}, xs::T...) where T<:Number
nr = length(rows)
nc = rows[1]

Expand Down Expand Up @@ -1445,7 +1445,7 @@ end

hvcat(rows::Tuple{Vararg{Int}}, xs::Number...) = typed_hvcat(promote_typeof(xs...), rows, xs...)

function typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}, xs::Number...)
function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, xs::Number...) where T
nr = length(rows)
nc = rows[1]
for i = 2:nr
Expand All @@ -1472,7 +1472,7 @@ function hvcat(rows::Tuple{Vararg{Int}}, as...)
vcat(rs...)
end

function typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}, as...)
function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, as...) where T
nbr = length(rows) # number of block rows
rs = Array{Any,1}(nbr)
a = 1
Expand Down
2 changes: 1 addition & 1 deletion base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ function getindex(r::Union{StepRangeLen,LinSpace}, i::Integer)
end

# This is separate to make it useful even when running with --check-bounds=yes
function unsafe_getindex{T}(r::StepRangeLen{T}, i::Integer)
function unsafe_getindex(r::StepRangeLen{T}, i::Integer) where T
u = i - r.offset
T(r.ref + u*r.step)
end
Expand Down
4 changes: 2 additions & 2 deletions base/reducedim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ reduced_indices(a::AbstractArray, region) = reduced_indices(indices(a), region)
# for reductions that keep 0 dims as 0
reduced_indices0(a::AbstractArray, region) = reduced_indices0(indices(a), region)

function reduced_indices{N}(inds::Indices{N}, d::Int, rd::AbstractUnitRange)
function reduced_indices(inds::Indices{N}, d::Int, rd::AbstractUnitRange) where N
d < 1 && throw(ArgumentError("dimension must be ≥ 1, got $d"))
if d == 1
return (oftype(inds[1], rd), tail(inds)...)
Expand All @@ -29,7 +29,7 @@ function reduced_indices0{N}(inds::Indices{N}, d::Int)
end
end

function reduced_indices{N}(inds::Indices{N}, region)
function reduced_indices(inds::Indices{N}, region) where N
rinds = [inds...]
for i in region
isa(i, Integer) || throw(ArgumentError("reduced dimension(s) must be integers"))
Expand Down
4 changes: 2 additions & 2 deletions base/sparse/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ function norm_sparse{Tv<:VTypes}(A::Sparse{Tv}, norm::Integer)
get(A.p), norm, common())
end

function horzcat{Tv<:VRealTypes}(A::Sparse{Tv}, B::Sparse{Tv}, values::Bool)
function horzcat(A::Sparse{Tv}, B::Sparse{Tv}, values::Bool) where Tv<:VRealTypes
s = Sparse(ccall((@cholmod_name("horzcat", SuiteSparse_long), :libcholmod),
Ptr{C_Sparse{Tv}},
(Ptr{C_Sparse{Tv}}, Ptr{C_Sparse{Tv}}, Cint, Ptr{UInt8}),
Expand Down Expand Up @@ -724,7 +724,7 @@ function sdmult!{Tv<:VTypes}(A::Sparse{Tv}, transpose::Bool,
Y
end

function vertcat{Tv<:VRealTypes}(A::Sparse{Tv}, B::Sparse{Tv}, values::Bool)
function vertcat(A::Sparse{Tv}, B::Sparse{Tv}, values::Bool) where Tv<:VRealTypes
s = Sparse(ccall((@cholmod_name("vertcat", SuiteSparse_long), :libcholmod),
Ptr{C_Sparse{Tv}},
(Ptr{C_Sparse{Tv}}, Ptr{C_Sparse{Tv}}, Cint, Ptr{UInt8}),
Expand Down
16 changes: 8 additions & 8 deletions base/sparse/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ end
# Logical and linear indexing into SparseMatrices
getindex(A::SparseMatrixCSC, I::AbstractVector{Bool}) = _logical_index(A, I) # Ambiguities
getindex(A::SparseMatrixCSC, I::AbstractArray{Bool}) = _logical_index(A, I)
function _logical_index{Tv}(A::SparseMatrixCSC{Tv}, I::AbstractArray{Bool})
function _logical_index(A::SparseMatrixCSC{Tv}, I::AbstractArray{Bool}) where Tv
checkbounds(A, I)
n = sum(I)
nnzB = min(n, nnz(A))
Expand Down Expand Up @@ -674,7 +674,7 @@ end

### getindex

function _spgetindex{Tv,Ti}(m::Int, nzind::AbstractVector{Ti}, nzval::AbstractVector{Tv}, i::Integer)
function _spgetindex(m::Int, nzind::AbstractVector{Ti}, nzval::AbstractVector{Tv}, i::Integer) where {Tv,Ti}
ii = searchsortedfirst(nzind, convert(Ti, i))
(ii <= m && nzind[ii] == i) ? nzval[ii] : zero(Tv)
end
Expand Down Expand Up @@ -840,7 +840,7 @@ complex(x::AbstractSparseVector) =
# of _absspvec_hcat below. The <:Integer qualifications are necessary for correct dispatch.
hcat(X::SparseVector{Tv,Ti}...) where {Tv,Ti<:Integer} = _absspvec_hcat(X...)
hcat(X::AbstractSparseVector{Tv,Ti}...) where {Tv,Ti<:Integer} = _absspvec_hcat(X...)
function _absspvec_hcat{Tv,Ti}(X::AbstractSparseVector{Tv,Ti}...)
function _absspvec_hcat(X::AbstractSparseVector{Tv,Ti}...) where {Tv,Ti}
# check sizes
n = length(X)
m = length(X[1])
Expand Down Expand Up @@ -875,7 +875,7 @@ end
# of _absspvec_vcat below. The <:Integer qualifications are necessary for correct dispatch.
vcat(X::SparseVector{Tv,Ti}...) where {Tv,Ti<:Integer} = _absspvec_vcat(X...)
vcat(X::AbstractSparseVector{Tv,Ti}...) where {Tv,Ti<:Integer} = _absspvec_vcat(X...)
function _absspvec_vcat{Tv,Ti}(X::AbstractSparseVector{Tv,Ti}...)
function _absspvec_vcat(X::AbstractSparseVector{Tv,Ti}...) where {Tv,Ti}
# check sizes
n = length(X)
tnnz = 0
Expand Down Expand Up @@ -973,10 +973,10 @@ hcat(A::Vector...) = Base.typed_hcat(promote_eltype(A...), A...)
hcat(A::_DenseConcatGroup...) = Base.typed_hcat(promote_eltype(A...), A...)
hvcat(rows::Tuple{Vararg{Int}}, xs::_DenseConcatGroup...) = Base.typed_hvcat(promote_eltype(xs...), rows, xs...)
# For performance, specially handle the case where the matrices/vectors have homogeneous eltype
cat{T}(catdims, xs::_TypedDenseConcatGroup{T}...) = Base.cat_t(catdims, T, xs...)
cat(catdims, xs::_TypedDenseConcatGroup{T}...) where {T} = Base.cat_t(catdims, T, xs...)
vcat(A::_TypedDenseConcatGroup{T}...) where {T} = Base.typed_vcat(T, A...)
hcat(A::_TypedDenseConcatGroup{T}...) where {T} = Base.typed_hcat(T, A...)
hvcat{T}(rows::Tuple{Vararg{Int}}, xs::_TypedDenseConcatGroup{T}...) = Base.typed_hvcat(T, rows, xs...)
hvcat(rows::Tuple{Vararg{Int}}, xs::_TypedDenseConcatGroup{T}...) where {T} = Base.typed_hvcat(T, rows, xs...)


### math functions
Expand All @@ -993,7 +993,7 @@ conj(x::SparseVector) = SparseVector(length(x), copy(nonzeroinds(x)), conj(nonze
#
macro unarymap_nz2z_z2z(op, TF)
esc(quote
function $(op){Tv<:$(TF),Ti<:Integer}(x::AbstractSparseVector{Tv,Ti})
function $(op)(x::AbstractSparseVector{Tv,Ti}) where Tv<:$(TF) where Ti<:Integer
R = typeof($(op)(zero(Tv)))
xnzind = nonzeroinds(x)
xnzval = nonzeros(x)
Expand Down Expand Up @@ -1039,7 +1039,7 @@ end

macro unarymap_z2nz(op, TF)
esc(quote
function $(op){Tv<:$(TF)}(x::AbstractSparseVector{Tv,<:Integer})
function $(op)(x::AbstractSparseVector{Tv,<:Integer}) where Tv<:$(TF)
v0 = $(op)(zero(Tv))
R = typeof(v0)
xnzind = nonzeroinds(x)
Expand Down
2 changes: 1 addition & 1 deletion base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ compute_offset1(parent, stride1::Integer, dims::Tuple{Int}, inds::Tuple{Slice},
compute_offset1(parent, stride1::Integer, dims, inds, I::Tuple) =
(@_inline_meta; compute_linindex(parent, I) - stride1) # linear indexing starts with 1

function compute_linindex{N}(parent, I::NTuple{N,Any})
function compute_linindex(parent, I::NTuple{N,Any}) where N
@_inline_meta
IP = fill_to_length(indices(parent), OneTo(1), Val{N})
compute_linindex(1, 1, IP, I)
Expand Down

0 comments on commit a961492

Please sign in to comment.