19
19
20
20
StructArray {T} (c:: C ) where {T, C<: Tuple } = StructArray {T} (NamedTuple {fields(T)} (c))
21
21
StructArray {T} (c:: C ) where {T, C<: NamedTuple } = StructArray {T, length(size(c[1])), C} (c)
22
+ StructArray {T} (c:: C ) where {T, C<: Pair } = StructArray {T} (Tuple (c))
22
23
StructArray (c:: C ) where {C<: NamedTuple } = StructArray {eltypes(C)} (c)
23
24
StructArray (c:: C ) where {C<: Tuple } = StructArray {eltypes(C)} (c)
25
+ StructArray (c:: Pair{P, Q} ) where {P, Q} = StructArray {Pair{eltype(P), eltype(Q)}} (c)
24
26
25
27
StructArray {T} (; kwargs... ) where {T} = StructArray {T} (values (kwargs))
26
28
StructArray (; kwargs... ) = StructArray (values (kwargs))
27
29
28
30
StructArray {T} (args... ) where {T} = StructArray {T} (NamedTuple {fields(T)} (args))
29
31
32
+ const StructVector{T, C<: NamedTuple } = StructArray{T, 1 , C}
33
+ StructVector {T} (args... ; kwargs... ) where {T} = StructArray {T} (args... ; kwargs... )
34
+ StructVector (args... ; kwargs... ) = StructArray (args... ; kwargs... )
35
+
36
+ Base. IndexStyle (:: Type{StructArray{T, N, C}} ) where {T, N, C} = Base. IndexStyle (gettypes (C). parameters[1 ])
37
+
30
38
_undef_array (:: Type{T} , sz; unwrap = t -> false ) where {T} = unwrap (T) ? StructArray {T} (undef, sz; unwrap = unwrap) : Array {T} (undef, sz)
31
39
32
40
_similar (v:: AbstractArray , :: Type{Z} ; unwrap = t -> false ) where {Z} =
@@ -53,6 +61,12 @@ StructArray(s::StructArray) = copy(s)
53
61
Base. convert (:: Type{StructArray} , v:: AbstractArray ) = StructArray (v)
54
62
55
63
columns (s:: StructArray ) = getfield (s, :columns )
64
+ columns (v:: AbstractVector ) = v
65
+ ncols (v:: AbstractVector ) = 1
66
+ ncols (v:: StructArray{T, N, C} ) where {T, N, C} = length (getnames (C))
67
+ colnames (v:: AbstractVector ) = (1 ,)
68
+ colnames (v:: StructArray{T, N, C} ) where {T, N, C} = getnames (C)
69
+
56
70
Base. getproperty (s:: StructArray , key:: Symbol ) = getfield (columns (s), key)
57
71
Base. getproperty (s:: StructArray , key:: Int ) = getfield (columns (s), key)
58
72
Base. propertynames (s:: StructArray ) = fieldnames (typeof (columns (s)))
@@ -106,6 +120,10 @@ function Base.resize!(s::StructArray, i::Integer)
106
120
return s
107
121
end
108
122
123
+ function Base. empty! (s:: StructArray )
124
+ foreachcolumn (empty!, s)
125
+ end
126
+
109
127
for op in [:hcat , :vcat ]
110
128
@eval begin
111
129
function Base. $op (args:: StructArray... )
0 commit comments