@@ -14,24 +14,23 @@ struct StructArray{T, N, C<:Tup, I} <: AbstractArray{T, N}
1414 axes (c[i]) == ax || error (" all field arrays must have same shape" )
1515 end
1616 end
17- new {T, N, C, _best_index(c... )} (c)
17+ new {T, N, C, index_type(C )} (c)
1818 end
1919end
2020
21- _best_index () = Int
22- _best_index (col:: AbstractArray , cols:: AbstractArray... ) = _best_index (IndexStyle (col, cols... ), col)
23- _best_index (:: IndexLinear , :: AbstractArray ) = Int
24- _best_index (:: IndexCartesian , :: AbstractArray{T, N} ) where {T, N} = CartesianIndex{N}
25- _best_index (:: Type{StructArray{T, N, C, I}} ) where {T, N, C, I} = I
26- _indexstyle (:: Type{Int} ) = IndexLinear ()
27- _indexstyle (:: Type{CartesianIndex{N}} ) where {N} = IndexCartesian ()
21+ index_type (:: Type{NamedTuple{names, types}} ) where {names, types} = index_type (types)
22+ index_type (:: Type{Tuple{}} ) = Int
23+ function index_type (:: Type{T} ) where {T<: Tuple }
24+ S, U = tuple_type_head (T), tuple_type_tail (T)
25+ IndexStyle (S) isa IndexCartesian ? CartesianIndex{ndims (S)} : index_type (U)
26+ end
2827
29- _dims (c:: Tup ) = length (axes (c[1 ]))
30- _dims (c:: EmptyTup ) = 1
28+ index_type (:: Type{StructArray{T, N, C, I}} ) where {T, N, C, I} = I
3129
3230function StructArray {T} (c:: C ) where {T, C<: Tup }
3331 cols = strip_params (staticschema (T))(c)
34- StructArray {T, _dims(cols), typeof(cols)} (cols)
32+ N = isempty (cols) ? 1 : ndims (cols[1 ])
33+ StructArray {T, N, typeof(cols)} (cols)
3534end
3635
3736StructArray (c:: C ) where {C<: NamedTuple } = StructArray {eltypes(C)} (c)
@@ -49,7 +48,9 @@ const StructVector{T, C<:Tup, I} = StructArray{T, 1, C, I}
4948StructVector {T} (args... ; kwargs... ) where {T} = StructArray {T} (args... ; kwargs... )
5049StructVector (args... ; kwargs... ) = StructArray (args... ; kwargs... )
5150
52- Base. IndexStyle (:: Type{S} ) where {S<: StructArray } = _indexstyle (_best_index (S))
51+ function Base. IndexStyle (:: Type{S} ) where {S<: StructArray }
52+ index_type (S) === Int ? IndexLinear () : IndexCartesian ()
53+ end
5354
5455function _undef_array (:: Type{T} , sz; unwrap = t -> false ) where {T}
5556 if unwrap (T)
0 commit comments