@@ -23,34 +23,39 @@ StructArray{T}(c::C) where {T, C<:NamedTuple} =
23
23
StructArray (c:: C ) where {C<: NamedTuple } = StructArray {C} (c)
24
24
25
25
StructArray {T} (args... ) where {T} = StructArray {T} (NamedTuple {fields(T)} (args))
26
+ @generated function StructArray {T} (:: Base.UndefInitializer , d:: Integer... ) where {T}
27
+ ex = Expr (:tuple , [:(Array {$(fieldtype(T, i))} (undef, sz)) for i in 1 : fieldcount (T)]. .. )
28
+ return quote
29
+ sz = convert (Tuple{Vararg{Int}}, d)
30
+ StructArray {T} (NamedTuple {fields(T)} ($ ex))
31
+ end
32
+ end
26
33
27
34
columns (s:: StructArray ) = getfield (s, :columns )
28
- getproperty (s:: StructArray , key:: Symbol ) = getfield (columns (s), key)
29
- getproperty (s:: StructArray , key:: Int ) = getfield (columns (s), key)
35
+ Base. getproperty (s:: StructArray , key:: Symbol ) = getfield (columns (s), key)
36
+ Base. getproperty (s:: StructArray , key:: Int ) = getfield (columns (s), key)
37
+ Base. propertynames (s:: StructArray ) = fieldnames (typeof (columns (s)))
30
38
31
- size (s:: StructArray ) = size (columns (s)[1 ])
39
+ Base . size (s:: StructArray ) = size (columns (s)[1 ])
32
40
33
- getindex (s:: StructArray , I:: Int... ) = get_ith (s, I... )
34
- function getindex (s:: StructArray{T, N, C} , I:: Union{Int, AbstractArray, Colon} ...) where {T, N, C}
41
+ Base . getindex (s:: StructArray , I:: Int... ) = get_ith (s, I... )
42
+ function Base . getindex (s:: StructArray{T, N, C} , I:: Union{Int, AbstractArray, Colon} ...) where {T, N, C}
35
43
StructArray {T} (map (v -> getindex (v, I... ), columns (s)))
36
44
end
37
45
38
- function view (s:: StructArray{T, N, C} , I... ) where {T, N, C}
46
+ function Base . view (s:: StructArray{T, N, C} , I... ) where {T, N, C}
39
47
StructArray {T} (map (v -> view (v, I... ), columns (s)))
40
48
end
41
49
42
- setindex! (s:: StructArray , val, I:: Int... ) = set_ith! (s, val, I... )
50
+ Base . setindex! (s:: StructArray , val, I:: Int... ) = set_ith! (s, val, I... )
43
51
44
52
fields (:: Type{<:NamedTuple{K}} ) where {K} = K
45
53
fields (:: Type{<:StructArray{T}} ) where {T} = fields (T)
46
-
47
- Base. propertynames (s:: StructArray ) = fieldnames (typeof (columns (s)))
48
-
49
54
@generated function fields (t:: Type{T} ) where {T}
50
55
return :($ (Expr (:tuple , [QuoteNode (f) for f in fieldnames (T)]. .. )))
51
56
end
52
57
53
- @generated function push! (s:: StructArray{T, 1} , vals) where {T}
58
+ @generated function Base . push! (s:: StructArray{T, 1} , vals) where {T}
54
59
args = []
55
60
for key in fields (T)
56
61
field = Expr (:., :s , Expr (:quote , key))
61
66
Expr (:block , args... )
62
67
end
63
68
64
- @generated function append! (s:: StructArray{T, 1} , vals) where {T}
69
+ @generated function Base . append! (s:: StructArray{T, 1} , vals) where {T}
65
70
args = []
66
71
for key in fields (T)
67
72
field = Expr (:., :s , Expr (:quote , key))
72
77
Expr (:block , args... )
73
78
end
74
79
75
- function cat (dims, args:: StructArray... )
76
- f = key -> cat (dims, (getproperty (t, key) for t in args). .. )
80
+ function Base . cat (args:: StructArray... ; dims )
81
+ f = key -> cat ((getproperty (t, key) for t in args). .. ; dims = dims )
77
82
T = mapreduce (eltype, promote_type, args)
78
83
StructArray {T} (map (f, fields (eltype (args[1 ]))))
79
84
end
87
92
88
93
for op in [:hcat , :vcat ]
89
94
@eval begin
90
- function $op (args:: StructArray... )
95
+ function Base . $op (args:: StructArray... )
91
96
f = key -> $ op ((getproperty (t, key) for t in args). .. )
92
97
T = mapreduce (eltype, promote_type, args)
93
98
StructArray {T} (map (f, fields (eltype (args[1 ]))))
0 commit comments