From 7714d1fab0bc563293fadaf6512102ce002835f9 Mon Sep 17 00:00:00 2001 From: Colin Summers Date: Wed, 26 Jun 2019 20:11:46 -0700 Subject: [PATCH 1/2] allows proper indexing for StructArrays of views --- src/interface.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/interface.jl b/src/interface.jl index 90422750..123dadcd 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -10,4 +10,5 @@ end staticschema(::Type{T}) where {T<:Tup} = T createinstance(::Type{T}, args...) where {T} = T(args...) -createinstance(::Type{T}, args...) where {T<:Union{Tuple, NamedTuple}} = T(args) +createinstance(::Type{<:Tuple}, args...) = args +createinstance(::Type{<:NamedTuple{names}}, args...) where {names} = NamedTuple{names}(args) From 862af3a843b3212b884a4ef5f327ee7199f193d4 Mon Sep 17 00:00:00 2001 From: Colin Summers Date: Wed, 26 Jun 2019 22:14:27 -0700 Subject: [PATCH 2/2] type stable get_ith --- src/structarray.jl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/structarray.jl b/src/structarray.jl index f9b43c0a..308a94a5 100644 --- a/src/structarray.jl +++ b/src/structarray.jl @@ -134,11 +134,13 @@ Base.axes(s::StructArray) = axes(fieldarrays(s)[1]) Base.axes(s::StructArray{<:Any, <:Any, <:EmptyTup}) = (1:0,) get_ith(cols::NamedTuple, I...) = get_ith(Tuple(cols), I...) -function get_ith(cols::NTuple{N, Any}, I...) where N - ntuple(N) do i - @inbounds res = getfield(cols, i)[I...] - return res +@generated get_ith(cols::NTuple{N, Any}, I...) where N = _get_ith(N) +function _get_ith(N::Integer) + ex = Expr(:tuple) + for i=1:N + push!(ex.args, :(getfield(cols, $i)[I...])) end + ex end Base.@propagate_inbounds function Base.getindex(x::StructArray{T, <:Any, <:Any, CartesianIndex{N}}, I::Vararg{Int, N}) where {T, N}