Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support indexing A[I1, i, I2] for CartesianIndexes I1, I2 #10814

Merged
merged 2 commits into from
Apr 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 43 additions & 10 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,62 @@ length{I<:CartesianIndex}(::Type{I})=length(super(I))
getindex(index::CartesianIndex, i::Integer) = getfield(index, i)::Int

stagedfunction getindex{N}(A::Array, index::CartesianIndex{N})
N==0 ? :(Base.arrayref(A, 1)) : :(@ncall $N Base.arrayref A d->index[d])
:(Base.arrayref(A, $(cartindex_exprs((index,), (:index,))...)))
end
stagedfunction getindex{N}(A::Array, i::Integer, index::CartesianIndex{N})
N==0 ? :(Base.arrayref(A, i)) : :(@ncall $(N+1) Base.arrayref A d->(d == 1 ? i : index[d-1]))
:(Base.arrayref(A, $(cartindex_exprs((i, index), (:i, :index))...)))
end
stagedfunction getindex{M,N}(A::Array, index1::CartesianIndex{M}, i::Integer, index2::CartesianIndex{N})
:(Base.arrayref(A, $(cartindex_exprs((index1, i, index2), (:index1, :i, :index2))...)))
end
stagedfunction setindex!{T,N}(A::Array{T}, v, index::CartesianIndex{N})
N==0 ? :(Base.arrayset(A, convert($T,v), 1)) : :(@ncall $N Base.arrayset A convert($T,v) d->index[d])
:(Base.arrayset(A, convert($T,v), $(cartindex_exprs((index,), (:index,))...)))
end
stagedfunction setindex!{T,N}(A::Array{T}, v, i::Integer, index::CartesianIndex{N})
N==0 ? :(Base.arrayset(A, convert($T,v), i)) : :(@ncall $(N+1) Base.arrayset A convert($T,v) d->(d == 1 ? i : index[d-1]))
:(Base.arrayset(A, convert($T,v), $(cartindex_exprs((i, index), (:i, :index))...)))
end
stagedfunction setindex!{T,M,N}(A::Array{T}, v, index1::CartesianIndex{M}, i::Integer, index2::CartesianIndex{N})
:(Base.arrayset(A, convert($T,v), $(cartindex_exprs((index1, i, index2), (:index1, :i, :index2))...)))
end

stagedfunction getindex{N}(A::AbstractArray, index::CartesianIndex{N})
:(@nref $N A d->index[d])
:(getindex(A, $(cartindex_exprs((index,), (:index,))...)))
end
stagedfunction getindex{N}(A::AbstractArray, i::Integer, index::CartesianIndex{N})
:(@nref $(N+1) A d->(d == 1 ? i : index[d-1]))
:(getindex(A, $(cartindex_exprs((i, index), (:i, :index))...)))
end
stagedfunction setindex!{T,N}(A::AbstractArray{T}, v, index::CartesianIndex{N})
:(setindex!(A, v, $(cartindex_exprs((index,), (:index,))...)))
end
stagedfunction setindex!{T,N}(A::AbstractArray{T}, v, i::Integer, index::CartesianIndex{N})
:(setindex!(A, v, $(cartindex_exprs((i, index), (:i, :index))...)))
end
stagedfunction setindex!{N}(A::AbstractArray, v, index::CartesianIndex{N})
:((@nref $N A d->index[d]) = v)
for AT in (AbstractVector, AbstractMatrix, AbstractArray) # nix ambiguity warning
@eval begin
stagedfunction getindex{M,N}(A::AbstractArray, index1::CartesianIndex{M}, i::Integer, index2::CartesianIndex{N})
:(getindex(A, $(cartindex_exprs((index1, i, index2), (:index1, :i, :index2))...)))
end
stagedfunction setindex!{T,M,N}(A::AbstractArray{T}, v, index1::CartesianIndex{M}, i::Integer, index2::CartesianIndex{N})
:(setindex!(A, v, $(cartindex_exprs((index1, i, index2), (:index1, :i, :index2))...)))
end
end
end
stagedfunction setindex!{N}(A::AbstractArray, v, i::Integer, index::CartesianIndex{N})
:((@nref $(N+1) A d->(d == 1 ? i : index[d-1])) = v)

function cartindex_exprs(indexes, syms)
exprs = Any[]
for (i,ind) in enumerate(indexes)
if ind <: Number
push!(exprs, :($(syms[i])))
else
for j = 1:length(ind)
push!(exprs, :($(syms[i])[$j]))
end
end
end
if isempty(exprs)
push!(exprs, 1) # Handle the zero-dimensional case
end
exprs
end

# arithmetic, min/max
Expand Down
19 changes: 19 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,25 @@ a = ones(5,0)
b = sub(a, :, :)
@test mdsum(b) == 0

a = reshape(1:60, 3, 4, 5)
@test a[CartesianIndex{3}(2,3,4)] == 44
a[CartesianIndex{3}(2,3,3)] = -1
@test a[CartesianIndex{3}(2,3,3)] == -1
@test a[2,CartesianIndex{2}(3,4)] == 44
a[1,CartesianIndex{2}(3,4)] = -2
@test a[1,CartesianIndex{2}(3,4)] == -2
@test a[CartesianIndex{1}(2),3,CartesianIndex{1}(4)] == 44
a[CartesianIndex{1}(2),3,CartesianIndex{1}(3)] = -3
@test a[CartesianIndex{1}(2),3,CartesianIndex{1}(3)] == -3

a = sub(zeros(3, 4, 5), :, :, :)
a[CartesianIndex{3}(2,3,3)] = -1
@test a[CartesianIndex{3}(2,3,3)] == -1
a[1,CartesianIndex{2}(3,4)] = -2
@test a[1,CartesianIndex{2}(3,4)] == -2
a[CartesianIndex{1}(2),3,CartesianIndex{1}(3)] = -3
@test a[CartesianIndex{1}(2),3,CartesianIndex{1}(3)] == -3

I1 = CartesianIndex((2,3,0))
I2 = CartesianIndex((-1,5,2))
@test I1 + I2 == CartesianIndex((1,8,2))
Expand Down