Skip to content

Commit f9d9a9c

Browse files
committed
Use new function flatview instead of Base.parent to access flattened view
1 parent 1b737cf commit f9d9a9c

File tree

4 files changed

+54
-24
lines changed

4 files changed

+54
-24
lines changed

src/array_of_similar_arrays.jl

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
66
An array that contains arrays that have the same size/axes. The array is
77
internally stored in flattened form as some kind of array of dimension
8-
`M + N`. The flattened form can be accessed via `parent(A)`.
8+
`M + N`. The flattened form can be accessed via `flatview(A)`.
99
1010
Subtypes must implement (in addition to typical array operations):
1111
12-
parent(A::SomeArrayOfSimilarArrays)::AbstractArray{T,M+N}
12+
flatview(A::SomeArrayOfSimilarArrays)::AbstractArray{T,M+N}
1313
1414
The following type aliases are defined:
1515
@@ -40,7 +40,7 @@ implicitly have equal size/axes.
4040
4141
Constructors:
4242
43-
ArrayOfSimilarArrays{N}(parent::AbstractArray)
43+
ArrayOfSimilarArrays{N}(flatview::AbstractArray)
4444
4545
The following type aliases are defined:
4646
@@ -57,10 +57,10 @@ struct ArrayOfSimilarArrays{
5757
} <: AbstractArrayOfSimilarArrays{T,M,N}
5858
data::P
5959

60-
function ArrayOfSimilarArrays{T,M,N}(parent::AbstractArray{U,L}) where {T,M,N,L,U}
61-
size_inner, size_outer = split_tuple(size(parent), Val{M}())
62-
require_ndims(parent, _add_vals(Val{M}(), Val{N}()))
63-
conv_parent = _convert_elype(T, parent)
60+
function ArrayOfSimilarArrays{T,M,N}(flatview::AbstractArray{U,L}) where {T,M,N,L,U}
61+
size_inner, size_outer = split_tuple(size(flatview), Val{M}())
62+
require_ndims(flatview, _add_vals(Val{M}(), Val{N}()))
63+
conv_parent = _convert_elype(T, flatview)
6464
P = typeof(conv_parent)
6565
new{T,M,N,L,P}(conv_parent)
6666
end
@@ -80,7 +80,7 @@ ArrayOfSimilarArrays(A::AbstractArray{<:AbstractArray{T,M},N}) where {T,M,N} =
8080
ArrayOfSimilarArrays{T,M,N}(A)
8181

8282

83-
Base.convert(R::Type{ArrayOfSimilarArrays{T,M,N}}, parent::AbstractArray{U,L}) where {T,M,N,L,U} = R(parent)
83+
Base.convert(R::Type{ArrayOfSimilarArrays{T,M,N}}, flatview::AbstractArray{U,L}) where {T,M,N,L,U} = R(flatview)
8484

8585
Base.convert(R::Type{ArrayOfSimilarArrays{T,M,N}}, A::AbstractArray{<:AbstractArray{U,M},N}) where {T,M,N,U} = R(A)
8686
Base.convert(R::Type{ArrayOfSimilarArrays{T}}, A::AbstractArray{<:AbstractArray{U,M},N}) where {T,M,N,U} = R(A)
@@ -114,7 +114,14 @@ import Base.==
114114
(A.data == B.data)
115115

116116

117-
Base.parent(A::ArrayOfSimilarArrays) = A.data
117+
"""
118+
flatview(A::ArrayOfSimilarArrays{T,M,N,L,P})::P
119+
120+
Returns the array of dimensionality `L = M + N` wrapped by `A`. The shape of
121+
the result may be freely changed without breaking the inner consistency of
122+
`A`.
123+
"""
124+
flatview(A::ArrayOfSimilarArrays) = A.data
118125

119126

120127
Base.size(A::ArrayOfSimilarArrays{T,M,N}) where {T,M,N} = split_tuple(size(A.data), Val{M}())[2]
@@ -196,8 +203,8 @@ const VectorOfSimilarArrays{
196203

197204
export VectorOfSimilarArrays
198205

199-
VectorOfSimilarArrays{T}(parent::AbstractArray{U,L}) where {T,U,L} =
200-
ArrayOfSimilarArrays{T,length(Base.front(size(parent))),1}(parent)
206+
VectorOfSimilarArrays{T}(flatview::AbstractArray{U,L}) where {T,U,L} =
207+
ArrayOfSimilarArrays{T,length(Base.front(size(flatview))),1}(flatview)
201208

202209
VectorOfSimilarArrays{T}(A::AbstractVector{<:AbstractArray{U,M}}) where {T,M,U} =
203210
VectorOfSimilarArrays{T,M}(A)
@@ -206,7 +213,7 @@ VectorOfSimilarArrays(A::AbstractVector{<:AbstractArray{T,M}}) where {T,M} =
206213
VectorOfSimilarArrays{T,M}(A)
207214

208215

209-
Base.convert(R::Type{VectorOfSimilarArrays{T}}, parent::AbstractArray{U,L}) where {T,U,L} = R(parent)
216+
Base.convert(R::Type{VectorOfSimilarArrays{T}}, flatview::AbstractArray{U,L}) where {T,U,L} = R(flatview)
210217
Base.convert(R::Type{VectorOfSimilarArrays{T}}, A::AbstractVector{<:AbstractArray{U,M}}) where {T,M,U} = R(A)
211218
Base.convert(R::Type{VectorOfSimilarArrays}, A::AbstractVector{<:AbstractArray{T,M}}) where {T,M} = R(A)
212219

@@ -245,8 +252,8 @@ const ArrayOfSimilarVectors{
245252

246253
export ArrayOfSimilarVectors
247254

248-
ArrayOfSimilarVectors{T}(parent::AbstractArray{U,L}) where {T,U,L} =
249-
ArrayOfSimilarArrays{T,1,length(Base.front(size(parent)))}(parent)
255+
ArrayOfSimilarVectors{T}(flatview::AbstractArray{U,L}) where {T,U,L} =
256+
ArrayOfSimilarArrays{T,1,length(Base.front(size(flatview)))}(flatview)
250257

251258
ArrayOfSimilarVectors{T}(A::AbstractArray{<:AbstractVector{U},N}) where {T,N,U} =
252259
ArrayOfSimilarVectors{T,N}(A)
@@ -255,7 +262,7 @@ ArrayOfSimilarVectors(A::AbstractArray{<:AbstractVector{T},N}) where {T,N} =
255262
ArrayOfSimilarVectors{T,N}(A)
256263

257264

258-
Base.convert(R::Type{ArrayOfSimilarVectors{T}}, parent::AbstractArray{U,L}) where {T,U,L} = R(parent)
265+
Base.convert(R::Type{ArrayOfSimilarVectors{T}}, flatview::AbstractArray{U,L}) where {T,U,L} = R(flatview)
259266
Base.convert(R::Type{ArrayOfSimilarVectors{T}}, A::AbstractArray{<:AbstractVector{U},N}) where {T,N,U} = R(A)
260267
Base.convert(R::Type{ArrayOfSimilarVectors}, A::AbstractArray{<:AbstractVector{T},N}) where {T,N} = R(A)
261268

@@ -299,20 +306,20 @@ const VectorOfSimilarVectors{
299306

300307
export VectorOfSimilarVectors
301308

302-
VectorOfSimilarVectors{T}(parent::AbstractArray{U,2}) where {T,U} =
303-
ArrayOfSimilarArrays{T,1,1}(parent)
309+
VectorOfSimilarVectors{T}(flatview::AbstractArray{U,2}) where {T,U} =
310+
ArrayOfSimilarArrays{T,1,1}(flatview)
304311

305-
VectorOfSimilarVectors(parent::AbstractArray{T,2}) where {T} =
306-
VectorOfSimilarVectors{T}(parent)
312+
VectorOfSimilarVectors(flatview::AbstractArray{T,2}) where {T} =
313+
VectorOfSimilarVectors{T}(flatview)
307314

308315
VectorOfSimilarVectors{T}(A::AbstractVector{<:AbstractVector{U}}) where {T,U} =
309316
ArrayOfSimilarArrays{T,1}(A)
310317

311318
VectorOfSimilarVectors(A::AbstractVector{<:AbstractVector{T}}) where {T} =
312319
VectorOfSimilarVectors{T}(A)
313320

314-
Base.convert(R::Type{VectorOfSimilarVectors{T}}, parent::AbstractArray{U,2}) where {T,U} = R(parent)
315-
Base.convert(R::Type{VectorOfSimilarVectors}, parent::AbstractArray{T,2}) where {T} = R(parent)
321+
Base.convert(R::Type{VectorOfSimilarVectors{T}}, flatview::AbstractArray{U,2}) where {T,U} = R(flatview)
322+
Base.convert(R::Type{VectorOfSimilarVectors}, flatview::AbstractArray{T,2}) where {T} = R(flatview)
316323
Base.convert(R::Type{VectorOfSimilarVectors{T}}, A::AbstractVector{<:AbstractVector{U}}) where {T,U} = R(A)
317324
Base.convert(R::Type{VectorOfSimilarVectors}, A::AbstractVector{<:AbstractVector{T}}) where {T} = R(A)
318325

src/functions.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,20 @@ deepmap(f::Base.Callable, x::Any) = map(f, x)
2727

2828
deepmap(f::Base.Callable, A::AbstractArray{<:AbstractArray}) =
2929
map(X -> deepmap(f, X), A)
30+
31+
32+
"""
33+
flatview(A::AbstractArray)
34+
flatview(A::AbstractArray{<:AbstractArray{<:...}})
35+
36+
View array `A` in a suitable flattened form. The shape of the flattened form
37+
will depend on the type of `A`. If the `A` is not a nested array, the return
38+
value is `A` itself. When no type-specific method is available, `flatview`
39+
will fall back to `Base.Iterators.flatten`.
40+
"""
41+
function flatview end
42+
export flatview
43+
44+
flatview(A::AbstractArray) = A
45+
46+
flatview(A::AbstractArray{<:AbstractArray}) = Base.Iterators.flatten(A)

src/vector_of_arrays.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,14 @@ import Base.==
137137
(==)(A::VectorOfArrays, B::VectorOfArrays) =
138138
A.data == B.data && A.elem_ptr == B.elem_ptr && A.kernel_size == B.kernel_size
139139

140+
"""
141+
flatview(A::VectorOfArrays{T})::Vector{T}
140142
141-
Base.parent(A::VectorOfArrays) = A.data
143+
Returns the internal serialized representation of all element arrays of `A` as
144+
a single vector. Do *not* change the length of the returned vector, as it
145+
would break the inner consistency of `A`.
146+
"""
147+
flatview(A::VectorOfArrays) = A.data
142148

143149
Base.size(A::VectorOfArrays) = size(A.kernel_size)
144150

test/array_of_similar_arrays.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ using UnsafeArrays
5050
A = rand_nested_similar_arrays(Val_M, Val_N)
5151

5252
A2_ctor = @inferred TT(A)
53-
U = eltype(parent(A2_ctor))
53+
U = eltype(flatview(A2_ctor))
5454

5555
A_U = Array{Array{U,M},N}(A)
5656

@@ -61,7 +61,7 @@ using UnsafeArrays
6161
@test typeof(A2_conv) == RT
6262
@test A2_conv == A2_ctor
6363

64-
U = eltype(parent(A2_ctor))
64+
U = eltype(flatview(A2_ctor))
6565
A3 = @inferred Array(A2_ctor)
6666
@test typeof(A3) == Array{Array{U,M},N}
6767
@test A3 == A_U

0 commit comments

Comments
 (0)