Skip to content

Commit 79ff5bf

Browse files
Merge pull request #52 from Tokazama/master
Add `parent_type` method
2 parents 65fff23 + 24fc454 commit 79ff5bf

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/ArrayInterface.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ Base.@pure __parameterless_type(T) = Base.typename(T).wrapper
88
parameterless_type(x) = parameterless_type(typeof(x))
99
parameterless_type(x::Type) = __parameterless_type(x)
1010

11+
"""
12+
parent_type(x)
13+
14+
Returns the parent array that `x` wraps.
15+
"""
16+
parent_type(x) = parent_type(typeof(x))
17+
parent_type(::Type{<:SubArray{T,N,P}}) where {T,N,P} = P
18+
parent_type(::Type{<:Base.ReshapedArray{T,N,P}}) where {T,N,P} = P
19+
parent_type(::Type{Adjoint{T,S}}) where {T,S} = S
20+
parent_type(::Type{Transpose{T,S}}) where {T,S} = S
21+
parent_type(::Type{Symmetric{T,S}}) where {T,S} = S
22+
parent_type(::Type{<:LinearAlgebra.AbstractTriangular{T,S}}) where {T,S} = S
23+
parent_type(::Type{T}) where {T} = T
24+
1125
function ismutable end
1226

1327
"""

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,13 @@ end
162162

163163
using ArrayInterface: zeromatrix
164164
@test zeromatrix(rand(4,4,4)) == zeros(4*4*4,4*4*4)
165+
166+
using ArrayInterface: parent_type
167+
@testset "Parent Type" begin
168+
x = ones(4, 4)
169+
@test parent_type(view(x, 1:2, 1:2)) <: typeof(x)
170+
@test parent_type(reshape(x, 2, :)) <: typeof(x)
171+
@test parent_type(transpose(x)) <: typeof(x)
172+
@test parent_type(Symmetric(x)) <: typeof(x)
173+
@test parent_type(UpperTriangular(x)) <: typeof(x)
174+
end

0 commit comments

Comments
 (0)