Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ext/SparseArraysExt.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module SparseArraysExt

using StructArrays: StructArray, components, createinstance
import StructArrays
import SparseArrays: sparse, issparse

function sparse(S::StructArray{T}) where {T}
sparse_components = map(sparse, components(S))
sparse_components = StructArrays._mapcols(sparse, components(S))
return createinstance.(T, sparse_components...)
end

Expand Down
2 changes: 1 addition & 1 deletion ext/StaticArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ let shape = isdefined(Base, :AbstractOneTo) && SOneTo <: Base.AbstractOneTo ?
@eval Base.similar(s::StructArray, ::Type{T}, sz::$shape) where T = StructArrays._similar(s, T, sz)
@eval Base.similar(::Type{<:StructArray{T, N, C}}, sz::$shape) where {T, N, C} = StructArrays.buildfromschema(typ -> similar(typ, sz), T, C)
end
Base.reshape(s::StructArray{T}, d::Tuple{SOneTo, Vararg{SOneTo}}) where {T} = StructArray{T}(map(x -> reshape(x, d), StructArrays.components(s)))
Base.reshape(s::StructArray{T}, d::Tuple{SOneTo, Vararg{SOneTo}}) where {T} = StructArray{T}(StructArrays._mapcols(x -> reshape(x, d), StructArrays.components(s)))

# Broadcast overload
using StaticArrays: StaticArrayStyle, similar_type, Size, SOneTo
Expand Down
4 changes: 2 additions & 2 deletions src/StructArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ include("tables.jl")
import DataAPI: refarray, refvalue
using DataAPI: defaultarray

refarray(s::StructArray) = StructArray(map(refarray, components(s)))
refarray(s::StructArray) = StructArray(_mapcols(refarray, components(s)))

function refvalue(s::StructArray{T}, v::Tup) where {T}
createinstance(T, map(refvalue, components(s), v)...)
Expand All @@ -36,7 +36,7 @@ colmetadatasupport(::Type{T}) where {T<:StructArray} = (
)
colmetadata(sa::StructArray, col::Symbol) = metadata(getproperty(sa, col))
colmetadata(sa::StructArray) =
map(Tables.columns(sa)) do col
_mapcols(Tables.columns(sa)) do col
metadatasupport(typeof(col)).read ? metadata(col) : nothing
end

Expand Down
2 changes: 1 addition & 1 deletion src/constructionbase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ConstructionBase.constructorof(::Type{<:StructArray{<:Union{Tuple, NamedTuple}}}
ConstructionBase.constructorof(::Type{<:StructArray{T}}) where {T} = function(comps::CT) where {CT}
# the resulting eltype is like T, but potentially with different type parameters, eg Complex{Int} -> Complex{Float64}
# probe its constructorof to get the right concrete type
ET = Base.promote_op(constructorof(T), map(eltype, fieldtypes(CT))...)
ET = Base.promote_op(constructorof(T), _mapcols(eltype, comps)...)
StructArray{ET}(comps)
end

Expand Down
24 changes: 12 additions & 12 deletions src/structarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ Base.convert(::Type{StructVector}, v::StructVector) = v

# Helper function to avoid adding too many dispatches to `Base.similar`
function _similar(s::StructArray{T}, ::Type{T}, sz) where {T}
return StructArray{T}(map(typ -> similar(typ, sz), components(s)))
return StructArray{T}(_mapcols(typ -> similar(typ, sz), components(s)))
end

function _similar(s::StructArray{T}, S::Type, sz) where {T}
Expand Down Expand Up @@ -336,13 +336,13 @@ Base.axes(s::StructArray) = axes(components(s)[1])
"""
StructArrays.get_ith(cols::Union{Tuple,NamedTuple}, I...)

Form a `Tuple` of the `I`th index of each element of `cols`, i.e. is equivalent
to
Form a `Tuple` or `NamedTuple` (matching `cols`) of the `I`th index of each element
of `cols`, i.e. is equivalent to
```julia
map(c -> c[I...], Tuple(cols))
map(c -> c[I...], cols)
```
"""
@inline @generated get_ith(cols::Tup, I...) = :(Base.Cartesian.@ntuple $(fieldcount(cols)) i -> @inbounds cols[i][I...])
@inline get_ith(cols::Tup, I...) = _mapcols(c -> @inbounds(c[I...]), cols)

Base.@propagate_inbounds Base.getindex(x::StructArray, I...) = _getindex(x, to_indices(x, I)...)

Expand All @@ -354,12 +354,12 @@ end

@inline function _getindex(s::StructArray{T}, I...) where {T}
@boundscheck checkbounds(s, I...)
StructArray{T}(map(v -> @inbounds(getindex(v, I...)), components(s)))
StructArray{T}(_mapcols(v -> @inbounds(getindex(v, I...)), components(s)))
end

@inline function Base.view(s::StructArray{T, N, C}, I...) where {T, N, C}
@boundscheck checkbounds(s, I...)
StructArray{T}(map(v -> @inbounds(view(v, I...)), components(s)))
StructArray{T}(_mapcols(v -> @inbounds(view(v, I...)), components(s)))
end

function Base.parentindices(s::StructArray)
Expand Down Expand Up @@ -407,18 +407,18 @@ end

for f in (:pop!, :popfirst!)
@eval function Base.$f(s::StructVector{T}) where T
t = map($f, components(s))
t = _mapcols($f, components(s))
return createinstance(T, t...)
end
end

function Base.deleteat!(s::StructVector{T}, idxs) where T
t = map(Base.Fix2(deleteat!, idxs), components(s))
t = _mapcols(Base.Fix2(deleteat!, idxs), components(s))
return StructVector{T}(t)
end

function Base.keepat!(s::StructVector{T}, idxs) where T
t = map(Base.Fix2(keepat!, idxs), components(s))
t = _mapcols(Base.Fix2(keepat!, idxs), components(s))
return StructVector{T}(t)
end

Expand Down Expand Up @@ -484,7 +484,7 @@ end
Base.reduce(::typeof(vcat), A::AbstractVector{<:StructArray}) = _reducecat_structarray(vcat, A)
Base.reduce(::typeof(hcat), A::AbstractVector{<:StructArray}) = _reducecat_structarray(hcat, A)

Base.copy(s::StructArray{T}) where {T} = StructArray{T}(map(copy, components(s)))
Base.copy(s::StructArray{T}) where {T} = StructArray{T}(_mapcols(copy, components(s)))

for type in (
:Dims,
Expand All @@ -500,7 +500,7 @@ for type in (
:(Tuple{Colon}),
)
@eval function Base.reshape(s::StructArray{T}, d::$(type)) where {T}
StructArray{T}(map(x -> reshape(x, d), components(s)))
StructArray{T}(_mapcols(x -> reshape(x, d), components(s)))
end
end

Expand Down
15 changes: 12 additions & 3 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ argtail(_, args...) = args
split_tuple_type(T) = fieldtype(T, 1), Tuple{argtail(T.parameters...)...}

eltypes(nt::NamedTuple{names}) where {names} = NamedTuple{names, eltypes(values(nt))}
eltypes(t::Tuple) = Tuple{map(eltype, t)...}
eltypes(t::Tuple) = Tuple{_mapcols(eltype, t)...}

alwaysfalse(t) = false

Expand Down Expand Up @@ -32,6 +32,15 @@ end

map_params_as_tuple_fallback(f, ::Type{T}) where {T<:Tup} = map(f, fieldtypes(T))

"""
StructArrays._mapcols(f, cols::Union{Tuple,NamedTuple})

Apply `f` to each element of `cols`, returning a `Tuple` or `NamedTuple` with the same names.
Unlike `map`, stays inferrable for any number of elements: `map` gives up on tuples with 32 or more.
"""
@inline @generated _mapcols(f::F, cols::Tup) where {F} =
:($(strip_params(cols))(Base.Cartesian.@ntuple $(fieldcount(cols)) i -> f(cols[i])))

buildfromschema(initializer::F, ::Type{T}) where {F, T} = buildfromschema(initializer, T, staticschema(T))

"""
Expand Down Expand Up @@ -151,7 +160,7 @@ julia> s_pooled = StructArrays.replace_storage(s) do v
"""
function replace_storage(f, s::StructArray{T}) where T
cols = components(s)
newcols = map(t -> replace_storage(f, t), cols)
newcols = _mapcols(t -> replace_storage(f, t), cols)
StructArray{T}(newcols)
end

Expand Down Expand Up @@ -201,6 +210,6 @@ If not all values are equal, return `nothing`. Otherwise, return the unique valu
"""
function findconsistentvalue(f::F, cols::Tup) where F
val = f(first(cols))
isconsistent = all(map(isequal(val) ∘ f, values(cols)))
isconsistent = all(_mapcols(isequal(val) ∘ f, cols))
return ifelse(isconsistent, val, nothing)
end
38 changes: 38 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,44 @@ end
@inferred map_params_infer()
end

@eval struct Wide64
$([:($(Symbol(:x, i))::$(iseven(i) ? Float64 : Int)) for i in 1:64]...)
end

@testset "wide components" begin
# Base.map loses inference on tuples with 32 or more elements,
# so componentwise operations must not rely on it
widecols(n, het) = ntuple(i -> het && iseven(i) ? [1.0, 2.0] : [1, 2], n)
@testset "$n columns, $(het ? "mixed" : "same") types, $(named ? "NamedTuple" : "Tuple")" for n in (32, 64), het in (false, true), named in (false, true)
cols = named ? NamedTuple{ntuple(i -> Symbol(:x, i), n)}(widecols(n, het)) : widecols(n, het)
t = StructArray(cols)
row = t[1]

@test (@inferred StructArray(cols)) == t
@test (@inferred StructArrays.eltypes(cols)) == eltype(t)

@test (@inferred t[1]) == row
@test (@inferred t[1:2]) == t
@test (@inferred view(t, 1:2)) == t

@test (@inferred similar(t)) isa typeof(t)
@test (@inferred similar(t, 3)) isa typeof(t)
@test (@inferred copy(t)) == t
@test (@inferred reshape(t, 1, 2)) == reshape(collect(t), 1, 2)
@test (@inferred deleteat!(copy(t), 1)) == t[2:2]
@test (@inferred keepat!(copy(t), [1])) == t[1:1]
@test (@inferred pop!(copy(t))) == t[2]
@test (@inferred popfirst!(copy(t))) == row
@test (@inferred refarray(t)) == t
@test (@inferred StructArrays.replace_storage(identity, t)) == t
@test (@inferred StructArrays.colmetadata(t)) == map(_ -> nothing, Tables.columns(t))
@test (@inferred reshape(t, (SOneTo(2),))) == t
end

s = StructArray{Wide64}(widecols(64, true))
@test (@inferred constructorof(typeof(s))(components(s))) == s
end

@testset "propertynames" begin
a = StructArray{ComplexF64}((Float64[], Float64[]))
@test sort(collect(propertynames(a))) == [:im, :re]
Expand Down
Loading