Skip to content

Commit 15f13f7

Browse files
vtjnashKristofferC
authored andcommitted
add isassigned methods for reinterpretarray (#53663)
Fixes #52925 Refs #51760 (cherry picked from commit 60d4b7b)
1 parent ab37ee5 commit 15f13f7

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

base/reinterpretarray.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,10 @@ check_ptr_indexable(a::AbstractArray, sz) = false
386386

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

389+
@propagate_inbounds isassigned(a::ReinterpretArray, inds::Integer...) = checkbounds(Bool, a, inds...) && (check_ptr_indexable(a) || _isassigned_ra(a, inds...))
390+
@propagate_inbounds isassigned(a::ReinterpretArray, inds::SCartesianIndex2) = isassigned(a.parent, inds.j)
391+
@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
392+
389393
@propagate_inbounds function getindex(a::ReinterpretArray{T,N,S}, inds::Vararg{Int, N}) where {T,N,S}
390394
check_readable(a)
391395
check_ptr_indexable(a) && return _getindex_ptr(a, inds...)

test/reinterpretarray.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,3 +588,23 @@ end
588588

589589
@test_throws ArgumentError reinterpret(Tuple{Int32, Int64}, (Int16(1), Int64(4)))
590590
end
591+
592+
let R = reinterpret(Float32, ComplexF32[1.0f0+2.0f0*im, 4.0f0+3.0f0*im])
593+
@test !isassigned(R, 0)
594+
@test isassigned(R, 1)
595+
@test isassigned(R, 4)
596+
@test isassigned(R, Int8(2), Int16(1), Int32(1), Int64(1))
597+
@test !isassigned(R, 1, 2)
598+
@test !isassigned(R, 5)
599+
@test Array(R)::Vector{Float32} == [1.0f0, 2.0f0, 4.0f0, 3.0f0]
600+
end
601+
602+
let R = reinterpret(reshape, Float32, ComplexF32[1.0f0+2.0f0*im, 4.0f0+3.0f0*im])
603+
@test !isassigned(R, 0)
604+
@test isassigned(R, 1)
605+
@test isassigned(R, 4)
606+
@test isassigned(R, Int8(2), Int16(2), Int32(1), Int64(1))
607+
@test !isassigned(R, 1, 1, 2)
608+
@test !isassigned(R, 5)
609+
@test Array(R)::Matrix{Float32} == [1.0f0 4.0f0; 2.0f0 3.0f0]
610+
end

0 commit comments

Comments
 (0)