Skip to content

Commit 4513c5f

Browse files
committed
Add function abstract_nestedarray_type
1 parent 4896a94 commit 4513c5f

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

docs/src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This package also defines and exports the following new functions applicable to
88
* [`innersize`](@ref) returns the size of the elements of an array, provided they all have equal size.
99
* [`deepgetindex`](@ref), [`deepsetindex!`](@ref) and [`deepview`](@ref) provide index-based access across multiple layers of nested arrays
1010
* [`innermap`](@ref) and [`deepmap`](@ref) apply a function to the elements of the inner (resp. innermost) arrays.
11+
* [`abstract_nestedarray_type`](@ref) returns the type of nested `AbstractArray`s for a given innermost element type with multiple layers of nesting.
1112

1213

1314
## [ArrayOfSimilarArrays](@id section_ArrayOfSimilarArrays)

src/functions.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,29 @@ Base.@propagate_inbounds _deepview_impl(A::AbstractArray{<:AbstractArray}, idxs_
198198
_deepview_tupled.(view(A, idxs_outer...), (idxs_inner,))
199199

200200
Base.@propagate_inbounds _deepview_tupled(A::AbstractArray, idxs::Tuple) = deepview(A, idxs...)
201+
202+
203+
"""
204+
abstract_nestedarray_type(T_inner::Type, ::Val{ndims_tuple})
205+
206+
Return the type of nested `AbstractArray`s. `T_inner` specifies the element
207+
type of the innermost layer of arrays, `ndims_tuple` specifies the
208+
dimensionality of each nesting layer (outer arrays first).
209+
210+
If `ndims_tuple` is empty, the returns is the (typically scalar) type
211+
`T_inner` itself.
212+
"""
213+
function abstract_nestedarray_type end
214+
export abstract_nestedarray_type
215+
216+
217+
Base.@pure function abstract_nestedarray_type(::Type{T_inner}, outer::Val{ndims_tuple}) where {T_inner,ndims_tuple}
218+
_abstract_nestedarray_type_impl(T_inner, ndims_tuple...)
219+
end
220+
221+
Base.@pure _abstract_nestedarray_type_impl(::Type{T_inner}) where {T_inner} = T_inner
222+
223+
Base.@pure _abstract_nestedarray_type_impl(::Type{T_inner}, N) where {T_inner} = AbstractArray{T_inner, N}
224+
225+
Base.@pure _abstract_nestedarray_type_impl(::Type{T_inner}, N, M, ndims_tuple...) where {T_inner} =
226+
AbstractArray{<:_abstract_nestedarray_type_impl(T_inner, M, ndims_tuple...), N}

test/functions.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,12 @@ using Test
6262
@test_throws MethodError deepview(B, 6)
6363
@test_throws MethodError deepview(B, 2, 3)
6464
end
65+
66+
67+
@testset "abstract_nestedarray_type" begin
68+
@test @inferred(abstract_nestedarray_type(Int, Val(()))) == Int
69+
@test @inferred(abstract_nestedarray_type(Int, Val((2,)))) == AbstractArray{Int, 2}
70+
@test @inferred(abstract_nestedarray_type(Float32, Val((2,3,4)))) ==
71+
AbstractArray{<:AbstractArray{<:AbstractArray{Float32, 4}, 3}, 2}
72+
end
6573
end

0 commit comments

Comments
 (0)