Skip to content

Commit 6d70cd1

Browse files
committed
Optimize operations for SubArrays with OffsetArray indices
1 parent c9aa4e3 commit 6d70cd1

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/OffsetArrays.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,24 @@ end
152152
@inline unsafe_getindex(a::AbstractArray, I...) = (@inbounds ret = a[I...]; ret)
153153
@inline unsafe_setindex!(a::AbstractArray, val, I...) = (@inbounds a[I...] = val; val)
154154

155-
@inline unsafe_getindex(a::OffsetArray, I::Int...) = (@inbounds ret = parent(a)[offset(a.offsets, I)...]; ret)
156-
@inline unsafe_setindex!(a::OffsetArray, val, I::Int...) = (@inbounds parent(a)[offset(a.offsets, I)...] = val; val)
155+
@inline unsafe_getindex(a::OffsetArray, I::Int...) = unsafe_getindex(parent(a), offset(a.offsets, I)...)
156+
@inline unsafe_setindex!(a::OffsetArray, val, I::Int...) = unsafe_setindex!(parent(a), val, offset(a.offsets, I)...)
157157
@inline unsafe_getindex(a::OffsetArray, I...) = unsafe_getindex(a, Base.IteratorsMD.flatten(I)...)
158158
@inline unsafe_setindex!(a::OffsetArray, val, I...) = unsafe_setindex!(a, val, Base.IteratorsMD.flatten(I)...)
159159

160+
# Indexing a SubArray which has OffsetArray indices
161+
typealias OffsetSubArray{T,N,P,I<:Tuple{OffsetArray,Vararg{OffsetArray}}} SubArray{T,N,P,I,false}
162+
@inline function unsafe_getindex{T,N}(a::OffsetSubArray{T,N}, I::Vararg{Int,N})
163+
J = map(unsafe_getindex, a.indexes, I)
164+
unsafe_getindex(parent(a), J...)
165+
end
166+
@inline function unsafe_setindex!{T,N}(a::OffsetSubArray{T,N}, val, I::Vararg{Int,N})
167+
J = map(unsafe_getindex, a.indexes, I)
168+
unsafe_setindex!(parent(a), val, J...)
169+
end
170+
@inline unsafe_getindex(a::OffsetSubArray, I::Union{Integer,CartesianIndex}...) = unsafe_getindex(a, Base.IteratorsMD.flatten(I)...)
171+
@inline unsafe_setindex!(a::OffsetSubArray, val, I::Union{Integer,CartesianIndex}...) = unsafe_setindex!(a, val, Base.IteratorsMD.flatten(I)...)
172+
160173
# Deprecations
161174
import Base: zeros, ones
162175
@deprecate zeros(T::Type, inds::UnitRange...) fill!(OffsetArray{T}(inds), zero(T))

0 commit comments

Comments
 (0)