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/StaticArraysExt.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module StaticArraysExt

using StructArrays
using StaticArrays: StaticArray, FieldArray, tuple_prod, SVector, MVector, SOneTo, HeterogeneousShapeTuple
using StaticArrays: StaticArray, FieldArray, tuple_prod, SVector, MVector, SOneTo, HeterogeneousShapeTuple, SArray, MArray, SizedArray

"""
StructArrays.staticschema(::Type{<:StaticArray{S, T}}) where {S, T}
Expand All @@ -22,6 +22,7 @@ which subtypes `FieldArray`.
end
end
StructArrays.createinstance(::Type{T}, args...) where {T<:StaticArray} = T(args)
StructArrays._createinstance(::Type{T}, row) where {T<:Union{SArray, MArray, SizedArray}} = T(row)
StructArrays.component(s::StaticArray, i::Integer) = getindex(s, i)

function StructArrays.component(s::StructArray{<:Union{SVector,MVector}}, key::Symbol)
Expand Down
5 changes: 5 additions & 0 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ end

createinstance(::Type{T}, args...) where {T<:Tup} = T(args)

# Build a `T` from a tuple of field values. Splatting more than 32 values allocates, so types whose
# `createinstance` belongs to this package (no user can overload it) are built from the tuple directly.
_createinstance(::Type{T}, row) where {T} = createinstance(T, row...)
_createinstance(::Type{T}, row) where {T<:Tup} = T(row)

struct Instantiator{T} end

Instantiator(::Type{T}) where {T} = Instantiator{T}()
Expand Down
4 changes: 2 additions & 2 deletions src/structarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ Base.@propagate_inbounds Base.getindex(x::StructArray, I...) = _getindex(x, to_i
Base.@propagate_inbounds function _getindex(x::StructArray{T}, I::Vararg{Int}) where {T}
cols = components(x)
@boundscheck checkbounds(x, I...)
return createinstance(T, get_ith(cols, I...)...)
return _createinstance(T, get_ith(cols, I...))
end

@inline function _getindex(s::StructArray{T}, I...) where {T}
Expand Down Expand Up @@ -408,7 +408,7 @@ end
for f in (:pop!, :popfirst!)
@eval function Base.$f(s::StructVector{T}) where T
t = _mapcols($f, components(s))
return createinstance(T, t...)
return _createinstance(T, t)
end
end

Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,10 @@ end
# 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)
# rows are stored rather than returned
allocs(f::F, args...) where {F} = (f(args...); @allocated f(args...))
storerow!(out, t, i) = (out[1] = t[i]; nothing)
storepop!(out, f::F, t) where {F} = (out[1] = f(t); nothing)
@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)
Expand All @@ -822,10 +826,17 @@ end
@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
out = Vector{eltype(t)}(undef, 1)
@test allocs(storerow!, out, t, 1) == 0
@test allocs(storepop!, out, pop!, copy(t)) == 0
@test allocs(storepop!, out, popfirst!, copy(t)) == 0
end

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

sv = StructArray{SVector{64, Float64}}(ntuple(_ -> [1.0, 2.0], 64))
@test allocs(storerow!, Vector{eltype(sv)}(undef, 1), sv, 1) == 0
end

@testset "propertynames" begin
Expand Down