Skip to content

Commit c7ce342

Browse files
committed
Add function nestedview and example
1 parent eea8d53 commit c7ce342

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/array_of_similar_arrays.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ The following type aliases are defined:
5151
5252
`VectorOfSimilarArrays` supports `push!()`, etc., provided the underlying
5353
array supports resizing of it's last dimension (e.g. an `ElasticArray`).
54+
55+
The nested array can also be created using the function [`nestedview`](@doc)
56+
and the wrapped flat array can be accessed using [`flatview`](@ref)
57+
afterwards:
58+
59+
```julia
60+
A_flat = rand(2,3,4,5,6)
61+
A_nested = nestedview(A_flat, 2)
62+
A_nested isa AbstractArray{<:AbstractArray{T,2},3} where T
63+
flatview(A_nested) === A_flat
64+
```
5465
"""
5566
struct ArrayOfSimilarArrays{
5667
T, M, N, L,

src/functions.jl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,25 @@ will fall back to `Base.Iterators.flatten`.
4242
function flatview end
4343
export flatview
4444

45-
flatview(A::AbstractArray) = A
45+
@inline flatview(A::AbstractArray) = A
4646

47-
flatview(A::AbstractArray{<:AbstractArray}) = Base.Iterators.flatten(A)
47+
@inline flatview(A::AbstractArray{<:AbstractArray}) = Base.Iterators.flatten(A)
4848

4949

50+
"""
51+
nestedview(A::AbstractArray{T,M+N}, M::Integer)
52+
53+
AbstractArray{<:AbstractArray{T,M},N}
54+
55+
View array `A` in as an `M`-dimensional array of `N`-dimensional arrays by
56+
wrapping it into an [`ArrayOfSimilarArrays`](@ref).
57+
"""
58+
function nestedview end
59+
export nestedview
60+
61+
@inline nestedview(A::AbstractArray{T,L}, M::Integer) where {T,L} =
62+
ArrayOfSimilarArrays{T,M}(A)
63+
5064

5165
"""
5266
innersize(A:AbstractArray{<:AbstractArray}, [dim])

test/array_of_similar_arrays.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,11 @@ using UnsafeArrays
109109
test_from_nested(VectorOfSimilarVectors{Float32}, VectorOfSimilarVectors{Float32,Array{Float32,2}}, Val(1), Val(1))
110110
test_from_nested(VectorOfSimilarVectors, VectorOfSimilarVectors{Float64,Array{Float64,2}}, Val(1), Val(1))
111111
end
112+
113+
@testset "examples" begin
114+
A_flat = rand(2,3,4,5,6)
115+
A_nested = nestedview(A_flat, 2)
116+
A_nested isa AbstractArray{<:AbstractArray{T,2},3} where T
117+
flatview(A_nested) === A_flat
118+
end
112119
end

0 commit comments

Comments
 (0)