Skip to content

Commit

Permalink
fix ambiguity warning, small change for type stability in non-Int case
Browse files Browse the repository at this point in the history
  • Loading branch information
Jutho committed Feb 26, 2015
1 parent c00bdcc commit da80d63
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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))
Expand Down

0 comments on commit da80d63

Please sign in to comment.