Skip to content

Commit

Permalink
SubArray: default to linear indexing when it's fast
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Nov 24, 2014
1 parent 3ccaf3c commit 63b2ae9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 11 additions & 3 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,21 @@ function tailsize(P, d)
s
end

stagedfunction linearindexing{T,N,P,I,LD}(A::SubArray{T,N,P,I,LD})
length(I) == LD ? (:(LinearFast())) : (:(LinearSlow()))
end
stagedfunction linearindexing{A<:SubArray}(::Type{A})
T,N,P,I,LD = A.parameters
length(I) == LD ? (:(LinearFast())) : (:(LinearSlow()))
end

getindex(::Colon, ::Colon) = Colon()
getindex{T}(v::AbstractArray{T,1}, ::Colon) = v
getindex(::Colon, i) = i

step(::Colon) = 1
first(::Colon) = 1

## Strides
stagedfunction strides{T,N,P,I}(V::SubArray{T,N,P,I})
all(map(x->x<:Union(RangeIndex,Colon), I)) || error("strides valid only for RangeIndex indexing")
Expand Down Expand Up @@ -299,9 +310,6 @@ function stride1expr(Atype::Type, Itypes::Tuple, Aexpr, Inewsym, LD)
ex
end

step(::Colon) = 1
first(::Colon) = 1

first_index(V::SubArray) = first_index(V.parent, V.indexes)
function first_index(P::AbstractArray, indexes::Tuple)
f = 1
Expand Down
8 changes: 6 additions & 2 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -859,13 +859,17 @@ end
a = [1:5]
@test isa(Base.linearindexing(a), Base.LinearFast)
b = sub(a, :)
@test isa(Base.linearindexing(b), Base.IteratorsMD.LinearSlow)
@test isa(Base.linearindexing(b), Base.IteratorsMD.LinearFast)
aa = fill(99, 10)
aa[1:2:9] = a
shp = [5]
for i = 1:10
A = reshape(a, tuple(shp...))
@test mdsum(A) == 15
@test mdsum2(A) == 15
B = sub(A, ntuple(i, i->Colon())...)
AA = reshape(aa, tuple(2, shp...))
B = sub(AA, 1:1, ntuple(i, i->Colon())...)
@test isa(Base.linearindexing(B), Base.IteratorsMD.LinearSlow)
@test mdsum(B) == 15
@test mdsum2(B) == 15
unshift!(shp, 1)
Expand Down

0 comments on commit 63b2ae9

Please sign in to comment.