Skip to content

Commit 0793515

Browse files
author
Pietro Vertechi
authored
Merge pull request JuliaArrays#30 from piever/pv/rename
Rename and generalize foreachcolumn
2 parents a16ea18 + 019bcb2 commit 0793515

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

src/sort.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
isdiscrete(v) = false
22

33
function Base.permute!(c::StructVector, p::AbstractVector)
4-
foreachcolumn(c) do v
4+
foreachfield(c) do v
55
if isdiscrete(v) || v isa StructVector
66
permute!(v, p)
77
else

src/structarray.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ fieldarrays(s::StructArray) = getfield(s, :fieldarrays)
7474
Base.getproperty(s::StructArray, key::Symbol) = getfield(fieldarrays(s), key)
7575
Base.getproperty(s::StructArray, key::Int) = getfield(fieldarrays(s), key)
7676
Base.propertynames(s::StructArray) = fieldnames(typeof(fieldarrays(s)))
77+
staticschema(::Type{<:StructArray{T}}) where {T} = staticschema(T)
7778

7879
Base.size(s::StructArray) = size(fieldarrays(s)[1])
7980
Base.axes(s::StructArray) = axes(fieldarrays(s)[1])
@@ -97,24 +98,24 @@ end
9798

9899
function Base.setindex!(s::StructArray, vals, I::Int...)
99100
@boundscheck checkbounds(s, I...)
100-
@inbounds foreachcolumn((col, val) -> (col[I...] = val), s, vals)
101+
@inbounds foreachfield((col, val) -> (col[I...] = val), s, vals)
101102
s
102103
end
103104

104105
@inline getfieldindex(v::Tuple, field::Symbol, index::Integer) = getfield(v, index)
105106
@inline getfieldindex(v, field::Symbol, index::Integer) = getproperty(v, field)
106107

107108
function Base.push!(s::StructArray, vals)
108-
foreachcolumn(push!, s, vals)
109+
foreachfield(push!, s, vals)
109110
return s
110111
end
111112

112113
function Base.append!(s::StructArray, vals)
113-
foreachcolumn(append!, s, vals)
114+
foreachfield(append!, s, vals)
114115
return s
115116
end
116117

117-
Base.copyto!(I::StructArray, J::StructArray) = (foreachcolumn(copyto!, I, J); I)
118+
Base.copyto!(I::StructArray, J::StructArray) = (foreachfield(copyto!, I, J); I)
118119

119120
function Base.cat(args::StructArray...; dims)
120121
f = key -> cat((getproperty(t, key) for t in args)...; dims=dims)
@@ -130,7 +131,7 @@ function Base.resize!(s::StructArray, i::Integer)
130131
end
131132

132133
function Base.empty!(s::StructArray)
133-
foreachcolumn(empty!, s)
134+
foreachfield(empty!, s)
134135
end
135136

136137
for op in [:hcat, :vcat]

src/utils.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ end
3434

3535
Base.@pure SkipConstructor(::Type) = false
3636

37-
@generated function foreachcolumn(f, x::StructArray{T, N, NamedTuple{names, types}}, xs...) where {T, N, names, types}
37+
@generated function foreachfield(::Type{<:NamedTuple{names}}, f, xs...) where {names}
3838
exprs = Expr[]
3939
for (i, field) in enumerate(names)
4040
sym = QuoteNode(field)
4141
args = [Expr(:call, :getfieldindex, :(getfield(xs, $j)), sym, i) for j in 1:length(xs)]
42-
push!(exprs, Expr(:call, :f, Expr(:., :x, sym), args...))
42+
push!(exprs, Expr(:call, :f, args...))
4343
end
4444
push!(exprs, :(return nothing))
4545
Expr(:block, exprs...)
4646
end
47+
foreachfield(f, x::T, xs...) where {T} = foreachfield(staticschema(T), f, x, xs...)
4748

4849
function createinstance(::Type{T}, args...) where {T}
4950
SkipConstructor(T) ? unsafe_createinstance(T, args...) : T(args...)

0 commit comments

Comments
 (0)