Skip to content

Commit c91e285

Browse files
jlchanpiever
andauthored
Adding support for components/broadcasting/views with StructArrays of StaticArrays (#186)
* supporting StructArrays.components for StaticArrays * adding StaticArrays as a dependency * adding StaticArray tests for StructArrays.components and broadcasting * consolidating StaticArrays modifications into single file * adding general staticarray support * adding tests for different types of arrays * Update src/staticarrays_support.jl Co-authored-by: Pietro Vertechi <pietro.vertechi@protonmail.com> * Update src/staticarrays_support.jl Co-authored-by: Pietro Vertechi <pietro.vertechi@protonmail.com> * Update src/staticarrays_support.jl Co-authored-by: Pietro Vertechi <pietro.vertechi@protonmail.com> * added inference tests * adding docs for StaticArray staticschema * fixed hanging @inferred which caused tests to fail * adding staticschema tests and documentation for StaticArrays * update order pkgs are listed in Project.toml * adding @inline for Base.view enables non-allocating views for StaticArray types * Update src/staticarrays_support.jl Co-authored-by: Pietro Vertechi <pietro.vertechi@protonmail.com> * Update src/staticarrays_support.jl Co-authored-by: Pietro Vertechi <pietro.vertechi@protonmail.com> * Update src/staticarrays_support.jl Co-authored-by: Pietro Vertechi <pietro.vertechi@protonmail.com> Co-authored-by: Jesse Chan <jesse.chan@rice.edu> Co-authored-by: Pietro Vertechi <pietro.vertechi@protonmail.com>
1 parent ce6da29 commit c91e285

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ version = "0.5.1"
55
[deps]
66
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
77
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
8+
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
89
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
910

1011
[compat]
1112
Adapt = "1, 2, 3"
1213
DataAPI = "1"
14+
StaticArrays = "1"
1315
Tables = "1"
1416
julia = "1"
1517

src/StructArrays.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ include("collect.jl")
1313
include("sort.jl")
1414
include("lazy.jl")
1515
include("tables.jl")
16+
include("staticarrays_support.jl")
1617

1718
# Implement refarray and refvalue to deal with pooled arrays and weakrefstrings effectively
1819
import DataAPI: refarray, refvalue

src/staticarrays_support.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import StaticArrays: StaticArray, tuple_prod
2+
3+
"""
4+
StructArrays.staticschema(::Type{<:StaticArray{S, T}}) where {S, T}
5+
6+
The `staticschema` of a `StaticArray` element type is the `staticschema` of the underlying `Tuple`.
7+
```julia
8+
julia> StructArrays.staticschema(SVector{2, Float64})
9+
Tuple{Float64, Float64}
10+
```
11+
"""
12+
@generated function StructArrays.staticschema(::Type{<:StaticArray{S, T}}) where {S, T}
13+
return quote
14+
Base.@_inline_meta
15+
return NTuple{$(tuple_prod(S)), T}
16+
end
17+
end
18+
StructArrays.createinstance(::Type{T}, args...) where {T<:StaticArray} = T(args)
19+
StructArrays.component(s::StaticArray, i) = getindex(s, i)

src/structarray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ Base.@propagate_inbounds function Base.getindex(x::StructArray{T, <:Any, <:Any,
344344
return createinstance(T, get_ith(cols, I)...)
345345
end
346346

347-
function Base.view(s::StructArray{T, N, C}, I...) where {T, N, C}
347+
@inline function Base.view(s::StructArray{T, N, C}, I...) where {T, N, C}
348348
StructArray{T}(map(v -> view(v, I...), components(s)))
349349
end
350350

test/runtests.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using StructArrays
22
using StructArrays: staticschema, iscompatible, _promote_typejoin, append!!
33
using OffsetArrays: OffsetArray
4+
using StaticArrays
45
import Tables, PooledArrays, WeakRefStrings
56
using TypedTables: Table
67
using DataAPI: refarray, refvalue
@@ -809,3 +810,26 @@ Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{MyArray}}, ::Type{El
809810
s = StructArray{ComplexF64}((MyArray(rand(2,2)), MyArray(rand(2,2))))
810811
@test_throws MethodError s .+ s
811812
end
813+
814+
@testset "staticarrays" begin
815+
816+
# test that staticschema returns the right things
817+
for StaticVectorType = [SVector, MVector, SizedVector]
818+
@test StructArrays.staticschema(StaticVectorType{2,Float64}) == Tuple{Float64,Float64}
819+
end
820+
821+
# test broadcast + components for vectors
822+
for StaticVectorType = [SVector, MVector, SizedVector]
823+
x = @inferred StructArray([StaticVectorType{2}(Float64[i;i+1]) for i = 1:2])
824+
y = @inferred StructArray([StaticVectorType{2}(Float64[i+1;i+2]) for i = 1:2])
825+
@test StructArrays.components(x) == ([1.0,2.0], [2.0,3.0])
826+
@test x .+ y == StructArray([StaticVectorType{2}(Float64[2*i+1;2*i+3]) for i = 1:2])
827+
end
828+
# test broadcast + components for general arrays
829+
for StaticArrayType = [SArray, MArray, SizedArray]
830+
x = @inferred StructArray([StaticArrayType{Tuple{1,2}}(ones(1,2) .+ i) for i = 0:1])
831+
y = @inferred StructArray([StaticArrayType{Tuple{1,2}}(2*ones(1,2) .+ i) for i = 0:1])
832+
@test StructArrays.components(x) == ([1., 2.], [1., 2.])
833+
@test x .+ y == StructArray([StaticArrayType{Tuple{1,2}}(3*ones(1,2) .+ 2*i) for i = 0:1])
834+
end
835+
end

0 commit comments

Comments
 (0)