Skip to content

Commit

Permalink
Un-deprecate setindex with multidimensional indices
Browse files Browse the repository at this point in the history
Back in #10525, I decided to deprecate `setindex!(A, x, I::Union{Real, Int, AbstractArray}...)` for symmetry since `getindex` only allows vector indices when there's more than one index.

But looking forward, I would really like to work towards APL semantics in 0.5 wherein the sum of the dimensionality of the indices is the dimensionality of the output array.  For example, indexing `A[[1 2; 3 4], 1]` would output a 2-dimensional `2x2` array: `[A[1,1] A[2, 1]; A[3,1] A[4,1]]`.  In which case, we'd add support back in for `setindex!` with array indices for symmetry.  This seems like needless churn - let's just leave things be until 0.5.
  • Loading branch information
mbauman committed Jul 24, 2015
1 parent c396349 commit e30a573
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
9 changes: 0 additions & 9 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -507,15 +507,6 @@ function chol(A::AbstractMatrix, uplo::Symbol)
chol(A, Val{uplo})
end

_ensure_vector(A::AbstractArray) = vec(A)
_ensure_vector(A) = A
_ensure_vectors() = ()
_ensure_vectors(A, As...) = (_ensure_vector(A), _ensure_vectors(As...)...)
function _unsafe_setindex!(l::LinearIndexing, A::AbstractArray, x, J::Union{Real,AbstractArray,Colon}...)
depwarn("multidimensional indexed assignment with multidimensional arrays is deprecated, use vec to convert indices to vectors", :_unsafe_setindex!)
_unsafe_setindex!(l, A, x, _ensure_vectors(J...)...)
end

# 11554

read!(from::AbstractIOBuffer, p::Ptr, nb::Integer) = read!(from, p, Int(nb))
Expand Down
15 changes: 6 additions & 9 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ index_lengths_dim(A, dim) = ()
index_lengths_dim(A, dim, ::Colon) = (trailingsize(A, dim),)
@inline index_lengths_dim(A, dim, ::Colon, i, I...) = (size(A, dim), index_lengths_dim(A, dim+1, i, I...)...)
@inline index_lengths_dim(A, dim, ::Real, I...) = (1, index_lengths_dim(A, dim+1, I...)...)
@inline index_lengths_dim(A, dim, i::AbstractVector{Bool}, I...) = (sum(i), index_lengths_dim(A, dim+1, I...)...)
@inline index_lengths_dim(A, dim, i::AbstractVector, I...) = (length(i), index_lengths_dim(A, dim+1, I...)...)
@inline index_lengths_dim(A, dim, i::AbstractArray{Bool}, I...) = (sum(i), index_lengths_dim(A, dim+1, I...)...)
@inline index_lengths_dim(A, dim, i::AbstractArray, I...) = (length(i), index_lengths_dim(A, dim+1, I...)...)

# shape of array to create for getindex() with indexes I, dropping trailing scalars
index_shape(A::AbstractArray, I::AbstractArray) = size(I) # Linear index reshape
Expand Down Expand Up @@ -290,15 +290,12 @@ _iterable(v) = repeated(v)
checkbounds(A, J...)
_unsafe_setindex!(l, A, x, J...)
end
@inline function _unsafe_setindex!(l::LinearIndexing, A::AbstractArray, x, J::Union{Real,AbstractVector,Colon}...)
@inline function _unsafe_setindex!(l::LinearIndexing, A::AbstractArray, x, J::Union{Real,AbstractArray,Colon}...)
_unsafe_batchsetindex!(l, A, _iterable(x), to_indexes(J...)...)
end

# While setindex! with one array argument doesn't mean anything special, it is
# still supported for symmetry with getindex.
_unsafe_setindex!(l::LinearIndexing, A::AbstractArray, x, I::AbstractArray) = _unsafe_setindex!(l, A, x, vec(I))
# 1-d logical indexing: override the above to avoid calling find (in to_index)
function _unsafe_setindex!(::LinearIndexing, A::AbstractArray, x, I::AbstractVector{Bool})
function _unsafe_setindex!(::LinearIndexing, A::AbstractArray, x, I::AbstractArray{Bool})
X = _iterable(x)
Xs = start(X)
i = 0
Expand All @@ -317,7 +314,7 @@ function _unsafe_setindex!(::LinearIndexing, A::AbstractArray, x, I::AbstractVec
end

# Use iteration over X so we don't need to worry about its storage
@generated function _unsafe_batchsetindex!(::LinearFast, A::AbstractArray, X, I::Union{Real,AbstractVector,Colon}...)
@generated function _unsafe_batchsetindex!(::LinearFast, A::AbstractArray, X, I::Union{Real,AbstractArray,Colon}...)
N = length(I)
quote
@nexprs $N d->(I_d = I[d])
Expand All @@ -334,7 +331,7 @@ end
A
end
end
@generated function _unsafe_batchsetindex!(::LinearSlow, A::AbstractArray, X, I::Union{Real,AbstractVector,Colon}...)
@generated function _unsafe_batchsetindex!(::LinearSlow, A::AbstractArray, X, I::Union{Real,AbstractArray,Colon}...)
N = length(I)
quote
@nexprs $N d->(I_d = I[d])
Expand Down
8 changes: 8 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ a[2,2] = 4
b = a'
@test a[1,1] == 1. && a[1,2] == 2. && a[2,1] == 3. && a[2,2] == 4.
@test b[1,1] == 1. && b[2,1] == 2. && b[1,2] == 3. && b[2,2] == 4.
a[[1 2 3 4]] = 0
@test a == zeros(2,2)
a[[1 2], [1 2]] = 1
@test a == ones(2,2)
a[[1 2], 1] = 0
@test a[1,1] == 0. && a[1,2] == 1. && a[2,1] == 0. && a[2,2] == 1.
a[:, [1 2]] = 2
@test a == 2ones(2,2)

a = Array(Float64, 2, 2, 2, 2, 2)
a[1,1,1,1,1] = 10
Expand Down

0 comments on commit e30a573

Please sign in to comment.