11"""
22A type that stores an array of structures as a structure of arrays.
33# Fields:
4- - `columns`: a tuple of arrays. Also `columns(x)`
4+ - `columns`: a named tuple of arrays. Also `columns(x)`
55"""
6- struct StructArray{T, N, C<: Tup } <: AbstractArray{T, N}
6+ struct StructArray{T, N, C<: NamedTuple } <: AbstractArray{T, N}
77 columns:: C
88
9- function StructArray {T, N, C} (c) where {T, N, C<: Tup }
9+ function StructArray {T, N, C} (c) where {T, N, C<: NamedTuple }
1010 length (c) > 0 || error (" must have at least one column" )
1111 n = size (c[1 ])
1212 length (n) == N || error (" wrong number of dimensions" )
@@ -21,6 +21,7 @@ StructArray{T}(c::C) where {T, C<:Tuple} = StructArray{T}(NamedTuple{fields(T)}(
2121StructArray {T} (c:: C ) where {T, C<: NamedTuple } =
2222 StructArray {createtype(T, eltypes(C)), length(size(c[1])), C} (c)
2323StructArray (c:: C ) where {C<: NamedTuple } = StructArray {C} (c)
24+ StructArray (c:: C ) where {C<: Tuple } = StructArray {eltypes(C)} (c)
2425
2526StructArray {T} (; kwargs... ) where {T} = StructArray {T} (values (kwargs))
2627StructArray (; kwargs... ) = StructArray (values (kwargs))
@@ -73,6 +74,10 @@ fields(::Type{<:StructArray{T}}) where {T} = fields(T)
7374@generated function fields (t:: Type{T} ) where {T}
7475 return :($ (Expr (:tuple , [QuoteNode (f) for f in fieldnames (T)]. .. )))
7576end
77+ @generated function fields (t:: Type{T} ) where {T<: Tuple }
78+ return :($ (Expr (:tuple , [QuoteNode (Symbol (" x$f " )) for f in fieldnames (T)]. .. )))
79+ end
80+
7681
7782@generated function Base. push! (s:: StructArray{T, 1} , vals) where {T}
7883 args = []
0 commit comments