From 15f13f7c575d159d088ab73163e7b54bb226aa7d Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Mon, 11 Mar 2024 08:28:24 -0400 Subject: [PATCH] add isassigned methods for reinterpretarray (#53663) Fixes #52925 Refs #51760 (cherry picked from commit 60d4b7be63a477071a0640a3bc87d5db3ff73d93) --- base/reinterpretarray.jl | 4 ++++ test/reinterpretarray.jl | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/base/reinterpretarray.jl b/base/reinterpretarray.jl index ff674690a5c66..8b4025e6903cd 100644 --- a/base/reinterpretarray.jl +++ b/base/reinterpretarray.jl @@ -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...) diff --git a/test/reinterpretarray.jl b/test/reinterpretarray.jl index 05a40895b7934..46ecbf6d06723 100644 --- a/test/reinterpretarray.jl +++ b/test/reinterpretarray.jl @@ -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