Skip to content

Add _unsetindex! methods for SubArrays and CartesianIndexes #53383

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

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1472,7 +1472,10 @@ function _setindex!(::IndexCartesian, A::AbstractArray, v, I::Vararg{Int,M}) whe
r
end

_unsetindex!(A::AbstractArray, i::Integer) = _unsetindex!(A, to_index(i))
function _unsetindex!(A::AbstractArray, i::Integer...)
@_propagate_inbounds_meta
_unsetindex!(A, map(to_index, i)...)
end

"""
parent(A)
Expand Down
7 changes: 6 additions & 1 deletion base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,12 @@ function _unsetindex!(A::Array, i::Int)
@inbounds _unsetindex!(GenericMemoryRef(A.ref, i))
return A
end

function _unsetindex!(A::Array, i::Int...)
@inline
@boundscheck checkbounds(A, i...)
@inbounds _unsetindex!(A, _to_linear_index(A, i...))
return A
end

# TODO: deprecate this (aligned_sizeof and/or elsize and/or sizeof(Some{T}) are more correct)
elsize(::Type{A}) where {T,A<:Array{T}} = aligned_sizeof(T)
Expand Down
6 changes: 6 additions & 0 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,12 @@ end
end
end

# _unsetindex
@propagate_inbounds function Base._unsetindex!(A::AbstractArray, i::CartesianIndex)
Base._unsetindex!(A, to_indices(A, (i,))...)
return A
end

## permutedims

## Permute array dims ##
Expand Down
19 changes: 19 additions & 0 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,25 @@ function isassigned(V::FastSubArray{<:Any, 1}, i::Int)
r
end

function _unsetindex!(V::FastSubArray, i::Int)
@inline
@boundscheck checkbounds(Bool, V, i)
@inbounds _unsetindex!(V.parent, _reindexlinear(V, i))
return V
end
function _unsetindex!(V::FastSubArray{<:Any,1}, i::Int)
@inline
@boundscheck checkbounds(Bool, V, i)
@inbounds _unsetindex!(V.parent, _reindexlinear(V, i))
return V
end
function _unsetindex!(V::SubArray{T,N}, i::Vararg{Int,N}) where {T,N}
@inline
@boundscheck checkbounds(Bool, V, i...)
@inbounds _unsetindex!(V.parent, reindex(V.indices, i)...)
return V
end

IndexStyle(::Type{<:FastSubArray}) = IndexLinear()
IndexStyle(::Type{<:SubArray}) = IndexCartesian()

Expand Down
42 changes: 42 additions & 0 deletions test/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1055,4 +1055,46 @@ end
@test !isassigned(v, 1, 2) # inbounds but not assigned
@test !isassigned(v, 3, 3) # out-of-bounds
end

@testset "_unsetindex!" begin
function test_unsetindex(A, B)
copyto!(A, B)
for i in eachindex(A)
@test !isassigned(A, i)
end
end
@testset "dest IndexLinear, src IndexLinear" begin
for p in (fill(BigInt(2)), BigInt[1, 2], BigInt[1 2; 3 4])
A = view(copy(p), ntuple(_->:, ndims(p))...)
B = view(similar(A), ntuple(_->:, ndims(p))...)
test_unsetindex(A, B)
test_unsetindex(p, B)
end
end

@testset "dest IndexLinear, src IndexCartesian" begin
for p in (fill(BigInt(2)), BigInt[1, 2], BigInt[1 2; 3 4])
A = view(copy(p), ntuple(_->:, ndims(p))...)
B = view(similar(A), axes(A)...)
test_unsetindex(A, B)
test_unsetindex(p, B)
end
end

@testset "dest IndexCartesian, src IndexLinear" begin
for p in (fill(BigInt(2)), BigInt[1, 2], BigInt[1 2; 3 4])
A = view(p, axes(p)...)
B = similar(A)
test_unsetindex(A, B)
end
end

@testset "dest IndexCartesian, src IndexCartesian" begin
for p in (fill(BigInt(2)), BigInt[1, 2], BigInt[1 2; 3 4])
A = view(p, axes(p)...)
B = view(similar(A), axes(A)...)
test_unsetindex(A, B)
end
end
end
end