Skip to content

Commit e30a573

Browse files
committed
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.
1 parent c396349 commit e30a573

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

base/deprecated.jl

-9
Original file line numberDiff line numberDiff line change
@@ -507,15 +507,6 @@ function chol(A::AbstractMatrix, uplo::Symbol)
507507
chol(A, Val{uplo})
508508
end
509509

510-
_ensure_vector(A::AbstractArray) = vec(A)
511-
_ensure_vector(A) = A
512-
_ensure_vectors() = ()
513-
_ensure_vectors(A, As...) = (_ensure_vector(A), _ensure_vectors(As...)...)
514-
function _unsafe_setindex!(l::LinearIndexing, A::AbstractArray, x, J::Union{Real,AbstractArray,Colon}...)
515-
depwarn("multidimensional indexed assignment with multidimensional arrays is deprecated, use vec to convert indices to vectors", :_unsafe_setindex!)
516-
_unsafe_setindex!(l, A, x, _ensure_vectors(J...)...)
517-
end
518-
519510
# 11554
520511

521512
read!(from::AbstractIOBuffer, p::Ptr, nb::Integer) = read!(from, p, Int(nb))

base/multidimensional.jl

+6-9
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ index_lengths_dim(A, dim) = ()
161161
index_lengths_dim(A, dim, ::Colon) = (trailingsize(A, dim),)
162162
@inline index_lengths_dim(A, dim, ::Colon, i, I...) = (size(A, dim), index_lengths_dim(A, dim+1, i, I...)...)
163163
@inline index_lengths_dim(A, dim, ::Real, I...) = (1, index_lengths_dim(A, dim+1, I...)...)
164-
@inline index_lengths_dim(A, dim, i::AbstractVector{Bool}, I...) = (sum(i), index_lengths_dim(A, dim+1, I...)...)
165-
@inline index_lengths_dim(A, dim, i::AbstractVector, I...) = (length(i), index_lengths_dim(A, dim+1, I...)...)
164+
@inline index_lengths_dim(A, dim, i::AbstractArray{Bool}, I...) = (sum(i), index_lengths_dim(A, dim+1, I...)...)
165+
@inline index_lengths_dim(A, dim, i::AbstractArray, I...) = (length(i), index_lengths_dim(A, dim+1, I...)...)
166166

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

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

319316
# Use iteration over X so we don't need to worry about its storage
320-
@generated function _unsafe_batchsetindex!(::LinearFast, A::AbstractArray, X, I::Union{Real,AbstractVector,Colon}...)
317+
@generated function _unsafe_batchsetindex!(::LinearFast, A::AbstractArray, X, I::Union{Real,AbstractArray,Colon}...)
321318
N = length(I)
322319
quote
323320
@nexprs $N d->(I_d = I[d])
@@ -334,7 +331,7 @@ end
334331
A
335332
end
336333
end
337-
@generated function _unsafe_batchsetindex!(::LinearSlow, A::AbstractArray, X, I::Union{Real,AbstractVector,Colon}...)
334+
@generated function _unsafe_batchsetindex!(::LinearSlow, A::AbstractArray, X, I::Union{Real,AbstractArray,Colon}...)
338335
N = length(I)
339336
quote
340337
@nexprs $N d->(I_d = I[d])

test/arrayops.jl

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ a[2,2] = 4
4040
b = a'
4141
@test a[1,1] == 1. && a[1,2] == 2. && a[2,1] == 3. && a[2,2] == 4.
4242
@test b[1,1] == 1. && b[2,1] == 2. && b[1,2] == 3. && b[2,2] == 4.
43+
a[[1 2 3 4]] = 0
44+
@test a == zeros(2,2)
45+
a[[1 2], [1 2]] = 1
46+
@test a == ones(2,2)
47+
a[[1 2], 1] = 0
48+
@test a[1,1] == 0. && a[1,2] == 1. && a[2,1] == 0. && a[2,2] == 1.
49+
a[:, [1 2]] = 2
50+
@test a == 2ones(2,2)
4351

4452
a = Array(Float64, 2, 2, 2, 2, 2)
4553
a[1,1,1,1,1] = 10

0 commit comments

Comments
 (0)