diff --git a/base/abstractarray.jl b/base/abstractarray.jl index afdd39720081c..c4a197664d507 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1030,7 +1030,7 @@ function repmat(a::AbstractVector, m::Int) return b end -sub2ind(dims) = 1 +sub2ind(dims::(Integer...,)) = 1 stagedfunction sub2ind{N}(dims::NTuple{N,Integer},I::Integer...) length(I) == N || throw(ArgumentError("number of subscripts should match length of dims")) ex=:(I[$N]) @@ -1052,24 +1052,25 @@ function sub2ind{T<:Integer}(dims::Array{T}, sub::Array{T}) return ind end -sub2ind{T<:Integer}(dims, I::AbstractVector{T}...) = +sub2ind{T<:Integer}(dims::(Integer...,), I::AbstractVector{T}...) = [ sub2ind(dims, map(X->X[i], I)...)::Int for i=1:length(I[1]) ] ind2sub(dims::(), ind::Integer) = ind==1 ? () : throw(BoundsError()) -stagedfunction ind2sub{N}(dims::NTuple{N,Integer}, ind::Int) +stagedfunction ind2sub{N}(dims::NTuple{N,Integer}, ind::Integer) subsyms = [symbol("i$k") for k=1:N] - subex = Expr(:block,[:($(subsyms[k])=rem(ind,dims[$k])+1;ind=div(ind,dims[$k])) for k=1:N-1]...) + subex = Expr(:block,[:($(subsyms[k])=rem(rest,dims[$k])+1;rest=div(rest,dims[$k])) for k=1:N-1]...) quote - ind-=1 + $(Expr(:meta,:inline)) + rest::Int=ind-1 $(subex) - $(subsyms[N]) = ind+1 + $(subsyms[N]) = rest+1 $(Expr(:tuple,subsyms...)) end end -ind2sub(a::AbstractArray, ind::Integer) = ind2sub(size(a), Int(ind)) +ind2sub(a::AbstractArray, ind::Integer) = ind2sub(size(a), ind) -function ind2sub{T<:Integer}(dims::(Integer,Integer...), ind::AbstractVector{T}) +function ind2sub{T<:Integer}(dims::(Integer...), ind::AbstractVector{T}) n = length(dims) l = length(ind) t = ntuple(n, x->Array(Int, l))