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

add isassigned methods for reinterpretarray #53663

Merged
merged 1 commit into from
Mar 11, 2024
Merged
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
add isassigned methods for reinterpretarray
Fixes #52925
Refs #51760
vtjnash committed Mar 8, 2024
commit a08cd7f3bf40e9738cf97db2e1cd84d765db380f
4 changes: 4 additions & 0 deletions base/reinterpretarray.jl
Original file line number Diff line number Diff line change
@@ -386,6 +386,10 @@ check_ptr_indexable(a::AbstractArray, sz) = false

@propagate_inbounds getindex(a::ReinterpretArray) = a[firstindex(a)]

@propagate_inbounds isassigned(a::ReinterpretArray, inds::Integer...) = checkbounds(Bool, a, inds...) && (check_ptr_indexable(a) || _isassigned_ra(a, inds...))
@propagate_inbounds isassigned(a::ReinterpretArray, inds::SCartesianIndex2) = isassigned(a.parent, inds.j)
@propagate_inbounds _isassigned_ra(a::ReinterpretArray, inds...) = true # that is not entirely true, but computing exactly which indexes will be accessed in the parent requires a lot of duplication from the _getindex_ra code

@propagate_inbounds function getindex(a::ReinterpretArray{T,N,S}, inds::Vararg{Int, N}) where {T,N,S}
check_readable(a)
check_ptr_indexable(a) && return _getindex_ptr(a, inds...)
20 changes: 20 additions & 0 deletions test/reinterpretarray.jl
Original file line number Diff line number Diff line change
@@ -588,3 +588,23 @@ end

@test_throws ArgumentError reinterpret(Tuple{Int32, Int64}, (Int16(1), Int64(4)))
end

let R = reinterpret(Float32, ComplexF32[1.0f0+2.0f0*im, 4.0f0+3.0f0*im])
@test !isassigned(R, 0)
@test isassigned(R, 1)
@test isassigned(R, 4)
@test isassigned(R, Int8(2), Int16(1), Int32(1), Int64(1))
@test !isassigned(R, 1, 2)
@test !isassigned(R, 5)
@test Array(R)::Vector{Float32} == [1.0f0, 2.0f0, 4.0f0, 3.0f0]
end

let R = reinterpret(reshape, Float32, ComplexF32[1.0f0+2.0f0*im, 4.0f0+3.0f0*im])
@test !isassigned(R, 0)
@test isassigned(R, 1)
@test isassigned(R, 4)
@test isassigned(R, Int8(2), Int16(2), Int32(1), Int64(1))
@test !isassigned(R, 1, 1, 2)
@test !isassigned(R, 5)
@test Array(R)::Matrix{Float32} == [1.0f0 4.0f0; 2.0f0 3.0f0]
end