Skip to content

Commit 7f8bcfe

Browse files
author
Pietro Vertechi
authored
Merge pull request JuliaArrays#28 from piever/pv/fieldarrays
rename to fieldarrays
2 parents 8563a51 + e455cd9 commit 7f8bcfe

File tree

4 files changed

+88
-95
lines changed

4 files changed

+88
-95
lines changed

src/collect.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,29 @@ ArrayInitializer() = ArrayInitializer(t -> false)
1717
_reshape(v, itr, ::Base.HasShape) = reshape(v, axes(itr))
1818
_reshape(v, itr, ::Union{Base.HasLength, Base.SizeUnknown}) = v
1919

20-
function collect_columns(itr; initializer = default_initializer)
20+
function collect_fieldarrays(itr; initializer = default_initializer)
2121
sz = Base.IteratorSize(itr)
22-
v = collect_columns(itr, sz, initializer = initializer)
22+
v = collect_fieldarrays(itr, sz, initializer = initializer)
2323
_reshape(v, itr, sz)
2424
end
2525

26-
function collect_empty_columns(itr::T; initializer = default_initializer) where {T}
26+
function collect_empty_fieldarrays(itr::T; initializer = default_initializer) where {T}
2727
S = Core.Compiler.return_type(first, Tuple{T})
2828
initializer(S, (0,))
2929
end
3030

31-
function collect_columns(@nospecialize(itr), ::Union{Base.HasShape, Base.HasLength};
31+
function collect_fieldarrays(@nospecialize(itr), ::Union{Base.HasShape, Base.HasLength};
3232
initializer = default_initializer)
3333

3434
st = iterate(itr)
35-
st === nothing && return collect_empty_columns(itr, initializer = initializer)
35+
st === nothing && return collect_empty_fieldarrays(itr, initializer = initializer)
3636
el, i = st
3737
dest = initializer(typeof(el), (length(itr),))
3838
dest[1] = el
39-
collect_to_columns!(dest, itr, 2, i)
39+
collect_to_fieldarrays!(dest, itr, 2, i)
4040
end
4141

42-
function collect_to_columns!(dest::AbstractArray{T}, itr, offs, st) where {T}
42+
function collect_to_fieldarrays!(dest::AbstractArray{T}, itr, offs, st) where {T}
4343
# collect to dest array, checking the type of each result. if a result does not
4444
# match, widen the result type and re-dispatch.
4545
i = offs
@@ -51,24 +51,24 @@ function collect_to_columns!(dest::AbstractArray{T}, itr, offs, st) where {T}
5151
@inbounds dest[i] = el
5252
i += 1
5353
else
54-
new = widencolumns(dest, i, el)
54+
new = widenfieldarrays(dest, i, el)
5555
@inbounds new[i] = el
56-
return collect_to_columns!(new, itr, i+1, st)
56+
return collect_to_fieldarrays!(new, itr, i+1, st)
5757
end
5858
end
5959
return dest
6060
end
6161

62-
function collect_columns(itr, ::Base.SizeUnknown; initializer = default_initializer)
62+
function collect_fieldarrays(itr, ::Base.SizeUnknown; initializer = default_initializer)
6363
elem = iterate(itr)
64-
elem === nothing && return collect_empty_columns(itr; initializer = initializer)
64+
elem === nothing && return collect_empty_fieldarrays(itr; initializer = initializer)
6565
el, st = elem
6666
dest = initializer(typeof(el), (1,))
6767
dest[1] = el
68-
grow_to_columns!(dest, itr, iterate(itr, st))
68+
grow_to_fieldarrays!(dest, itr, iterate(itr, st))
6969
end
7070

71-
function grow_to_columns!(dest::AbstractArray{T}, itr, elem = iterate(itr)) where {T}
71+
function grow_to_fieldarrays!(dest::AbstractArray{T}, itr, elem = iterate(itr)) where {T}
7272
# collect to dest array, checking the type of each result. if a result does not
7373
# match, widen the result type and re-dispatch.
7474
i = length(dest)+1
@@ -79,9 +79,9 @@ function grow_to_columns!(dest::AbstractArray{T}, itr, elem = iterate(itr)) wher
7979
elem = iterate(itr, st)
8080
i += 1
8181
else
82-
new = widencolumns(dest, i, el)
82+
new = widenfieldarrays(dest, i, el)
8383
push!(new, el)
84-
return grow_to_columns!(new, itr, iterate(itr, st))
84+
return grow_to_fieldarrays!(new, itr, iterate(itr, st))
8585
end
8686
end
8787
return dest
@@ -92,18 +92,18 @@ function to_structarray(::Type{T}, nt::C) where {T, C}
9292
StructArray{S}(nt)
9393
end
9494

95-
function widencolumns(dest::StructArray{T}, i, el::S) where {T, S}
95+
function widenfieldarrays(dest::StructArray{T}, i, el::S) where {T, S}
9696
fs = fields(S)
9797
if fs === fields(T)
98-
new_cols = (widencolumns(columns(dest)[ind], i, getfieldindex(el, f, ind)) for (ind, f) in enumerate(fs))
98+
new_cols = (widenfieldarrays(fieldarrays(dest)[ind], i, getfieldindex(el, f, ind)) for (ind, f) in enumerate(fs))
9999
nt = NamedTuple{fs}(Tuple(new_cols))
100100
v = to_structarray(T, nt)
101101
else
102102
widenarray(dest, i, el)
103103
end
104104
end
105105

106-
widencolumns(dest::AbstractArray, i, el) = widenarray(dest, i, el)
106+
widenfieldarrays(dest::AbstractArray, i, el) = widenarray(dest, i, el)
107107

108108
function widenarray(dest::AbstractArray{T}, i, el::S) where {S, T}
109109
S <: T && return dest

src/structarray.jl

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
"""
22
A type that stores an array of structures as a structure of arrays.
33
# Fields:
4-
- `columns`: a named tuple of arrays. Also `columns(x)`
4+
- `fieldarrays`: a named tuple of arrays. Also `fieldarrays(x)`
55
"""
66
struct StructArray{T, N, C<:NamedTuple} <: AbstractArray{T, N}
7-
columns::C
7+
fieldarrays::C
88

99
function StructArray{T, N, C}(c) where {T, N, C<:NamedTuple}
1010
length(c) > 0 || error("must have at least one column")
1111
ax = axes(c[1])
1212
length(ax) == N || error("wrong number of dimensions")
1313
for i = 2:length(c)
14-
axes(c[i]) == ax || error("all columns must have same size")
14+
axes(c[i]) == ax || error("all field arrays must have same shape")
1515
end
1616
new{T, N, C}(c)
1717
end
@@ -69,35 +69,30 @@ Base.similar(s::S, sz::Tuple) where {S<:StructArray} = similar(S, Base.to_shape(
6969
Base.similar(s::S, sz::Base.DimOrInd...) where {S<:StructArray} = similar(S, Base.to_shape(sz))
7070
Base.similar(s::S) where {S<:StructArray} = similar(S, Base.to_shape(axes(s)))
7171

72-
columns(s::StructArray) = getfield(s, :columns)
73-
columns(v::AbstractVector) = v
74-
ncols(v::AbstractVector) = 1
75-
ncols(v::StructArray{T, N, C}) where {T, N, C} = length(getnames(C))
76-
colnames(v::AbstractVector) = (1,)
77-
colnames(v::StructArray{T, N, C}) where {T, N, C} = getnames(C)
72+
fieldarrays(s::StructArray) = getfield(s, :fieldarrays)
7873

79-
Base.getproperty(s::StructArray, key::Symbol) = getfield(columns(s), key)
80-
Base.getproperty(s::StructArray, key::Int) = getfield(columns(s), key)
81-
Base.propertynames(s::StructArray) = fieldnames(typeof(columns(s)))
74+
Base.getproperty(s::StructArray, key::Symbol) = getfield(fieldarrays(s), key)
75+
Base.getproperty(s::StructArray, key::Int) = getfield(fieldarrays(s), key)
76+
Base.propertynames(s::StructArray) = fieldnames(typeof(fieldarrays(s)))
8277

83-
Base.size(s::StructArray) = size(columns(s)[1])
84-
Base.axes(s::StructArray) = axes(columns(s)[1])
78+
Base.size(s::StructArray) = size(fieldarrays(s)[1])
79+
Base.axes(s::StructArray) = axes(fieldarrays(s)[1])
8580

8681
@generated function Base.getindex(x::StructArray{T, N, NamedTuple{names, types}}, I::Int...) where {T, N, names, types}
8782
args = [:(getfield(cols, $i)[I...]) for i in 1:length(names)]
8883
return quote
89-
cols = columns(x)
84+
cols = fieldarrays(x)
9085
@boundscheck checkbounds(x, I...)
9186
@inbounds $(Expr(:call, :createinstance, :T, args...))
9287
end
9388
end
9489

9590
function Base.getindex(s::StructArray{T, N, C}, I::Union{Int, AbstractArray, Colon}...) where {T, N, C}
96-
StructArray{T}(map(v -> getindex(v, I...), columns(s)))
91+
StructArray{T}(map(v -> getindex(v, I...), fieldarrays(s)))
9792
end
9893

9994
function Base.view(s::StructArray{T, N, C}, I...) where {T, N, C}
100-
StructArray{T}(map(v -> view(v, I...), columns(s)))
95+
StructArray{T}(map(v -> view(v, I...), fieldarrays(s)))
10196
end
10297

10398
function Base.setindex!(s::StructArray, vals, I::Int...)
@@ -124,7 +119,7 @@ function Base.cat(args::StructArray...; dims)
124119
end
125120

126121
function Base.resize!(s::StructArray, i::Integer)
127-
for a in columns(s)
122+
for a in fieldarrays(s)
128123
resize!(a, i)
129124
end
130125
return s
@@ -144,8 +139,8 @@ for op in [:hcat, :vcat]
144139
end
145140
end
146141

147-
Base.copy(s::StructArray{T,N,C}) where {T,N,C} = StructArray{T,N,C}(C(copy(x) for x in columns(s)))
142+
Base.copy(s::StructArray{T,N,C}) where {T,N,C} = StructArray{T,N,C}(C(copy(x) for x in fieldarrays(s)))
148143

149144
function Base.reshape(s::StructArray{T}, d::Dims) where {T}
150-
StructArray{T}(map(x -> reshape(x, d), columns(s)))
145+
StructArray{T}(map(x -> reshape(x, d), fieldarrays(s)))
151146
end

src/tables.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Tables.rowaccess(::Type{<:StructArray}) = true
33
Tables.columnaccess(::Type{<:StructArray}) = true
44

55
Tables.rows(s::StructArray) = s
6-
Tables.columns(s::StructArray) = columns(s)
6+
Tables.columns(s::StructArray) = fieldarrays(s)
77

88
function Tables.schema(s::StructArray{T}) where {T}
99
NT = staticschema(T)

0 commit comments

Comments
 (0)