Skip to content

Commit

Permalink
add isassigned methods for reinterpretarray
Browse files Browse the repository at this point in the history
Fixes #52925
Refs #51760
  • Loading branch information
vtjnash committed Mar 8, 2024
1 parent e618369 commit a08cd7f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions base/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
20 changes: 20 additions & 0 deletions test/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a08cd7f

Please sign in to comment.