From e30a57399e586902a3101659a7b6f23b44243cba Mon Sep 17 00:00:00 2001 From: Matt Bauman Date: Thu, 23 Jul 2015 21:08:39 -0400 Subject: [PATCH] Un-deprecate setindex with multidimensional indices 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. --- base/deprecated.jl | 9 --------- base/multidimensional.jl | 15 ++++++--------- test/arrayops.jl | 8 ++++++++ 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index dad7aac7da3f9..2c7af050bf812 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -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)) diff --git a/base/multidimensional.jl b/base/multidimensional.jl index 39b0acab6cf81..80f069afc52ff 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -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 @@ -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 @@ -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]) @@ -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]) diff --git a/test/arrayops.jl b/test/arrayops.jl index 6b3f78130ff70..217e3c7589035 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -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