@@ -167,22 +167,26 @@ function Base.IndexStyle(::Type{S}) where {S<:StructArray}
167
167
index_type (S) === Int ? IndexLinear () : IndexCartesian ()
168
168
end
169
169
170
- function _undef_array (:: Type{T} , sz; unwrap:: F = alwaysfalse) where {T, F}
170
+ function undef_array (:: Type{T} , sz; unwrap:: F = alwaysfalse) where {T, F}
171
171
if unwrap (T)
172
172
return StructArray {T} (undef, sz; unwrap = unwrap)
173
173
else
174
174
return Array {T} (undef, sz)
175
175
end
176
176
end
177
177
178
- function _similar (v:: AbstractArray , :: Type{Z} ; unwrap:: F = alwaysfalse) where {Z, F}
178
+ function similar_array (v:: AbstractArray , :: Type{Z} ; unwrap:: F = alwaysfalse) where {Z, F}
179
179
if unwrap (Z)
180
- return buildfromschema (typ -> _similar (v, typ; unwrap = unwrap), Z)
180
+ return buildfromschema (typ -> similar_array (v, typ; unwrap = unwrap), Z)
181
181
else
182
182
return similar (v, Z)
183
183
end
184
184
end
185
185
186
+ function similar_structarray (v:: AbstractArray , :: Type{Z} ; unwrap:: F = alwaysfalse) where {Z, F}
187
+ buildfromschema (typ -> similar_array (v, typ; unwrap = unwrap), Z)
188
+ end
189
+
186
190
"""
187
191
StructArray{T}(undef, dims; unwrap=T->false)
188
192
@@ -204,14 +208,10 @@ julia> StructArray{ComplexF64}(undef, (2,3))
204
208
StructArray (:: Base.UndefInitializer , sz:: Dims )
205
209
206
210
function StructArray {T} (:: Base.UndefInitializer , sz:: Dims ; unwrap:: F = alwaysfalse) where {T, F}
207
- buildfromschema (typ -> _undef_array (typ, sz; unwrap = unwrap), T)
211
+ buildfromschema (typ -> undef_array (typ, sz; unwrap = unwrap), T)
208
212
end
209
213
StructArray {T} (u:: Base.UndefInitializer , d:: Integer... ; unwrap:: F = alwaysfalse) where {T, F} = StructArray {T} (u, convert (Dims, d); unwrap = unwrap)
210
214
211
- function similar_structarray (v:: AbstractArray , :: Type{Z} ; unwrap:: F = alwaysfalse) where {Z, F}
212
- buildfromschema (typ -> _similar (v, typ; unwrap = unwrap), Z)
213
- end
214
-
215
215
"""
216
216
StructArray(A; unwrap = T->false)
217
217
@@ -276,14 +276,34 @@ Base.convert(::Type{StructArray}, v::StructArray) = v
276
276
Base. convert (:: Type{StructVector} , v:: AbstractVector ) = StructVector (v)
277
277
Base. convert (:: Type{StructVector} , v:: StructVector ) = v
278
278
279
- function Base. similar (:: Type{<:StructArray{T, <:Any, C}} , sz:: Dims ) where {T, C}
280
- buildfromschema (typ -> similar (typ, sz), T, C)
279
+ # Mimic OffsetArrays signatures
280
+ const OffsetAxisKnownLength = Union{Integer, AbstractUnitRange}
281
+ const OffsetAxis = Union{OffsetAxisKnownLength, Colon}
282
+
283
+ const OffsetShapeKnownLength = Tuple{OffsetAxisKnownLength,Vararg{OffsetAxisKnownLength}}
284
+ const OffsetShape = Tuple{OffsetAxis,Vararg{OffsetAxis}}
285
+
286
+ # Helper function to avoid adding too many dispatches to `Base.similar`
287
+ function _similar (s:: StructArray{T} , :: Type{T} , sz) where {T}
288
+ return StructArray {T} (map (typ -> similar (typ, sz), components (s)))
281
289
end
282
290
283
- Base. similar (s:: StructArray , sz:: Base.DimOrInd... ) = similar (s, Base. to_shape (sz))
284
- Base. similar (s:: StructArray ) = similar (s, Base. to_shape (axes (s)))
285
- function Base. similar (s:: StructArray{T} , sz:: Tuple ) where {T}
286
- StructArray {T} (map (typ -> similar (typ, sz), components (s)))
291
+ function _similar (s:: StructArray{T} , S:: Type , sz) where {T}
292
+ # If not specified, we don't really know what kind of array to use for each
293
+ # interior type, so we just pick the first one arbitrarily. If users need
294
+ # something else, they need to be more specific.
295
+ c1 = first (components (s))
296
+ return isnonemptystructtype (S) ? buildfromschema (typ -> similar (c1, typ, sz), S) : similar (c1, S, sz)
297
+ end
298
+
299
+ for type in (:Dims , :OffsetShapeKnownLength )
300
+ @eval function Base. similar (:: Type{<:StructArray{T, N, C}} , sz:: $ (type)) where {T, N, C}
301
+ return buildfromschema (typ -> similar (typ, sz), T, C)
302
+ end
303
+
304
+ @eval function Base. similar (s:: StructArray , S:: Type , sz:: $ (type))
305
+ return _similar (s, S, sz)
306
+ end
287
307
end
288
308
289
309
@deprecate fieldarrays (x) StructArrays. components (x)
437
457
438
458
Base. copy (s:: StructArray{T} ) where {T} = StructArray {T} (map (copy, components (s)))
439
459
440
- function Base. reshape (s:: StructArray{T} , d:: Dims ) where {T}
441
- StructArray {T} (map (x -> reshape (x, d), components (s)))
460
+ for type in (:Dims , :OffsetShape )
461
+ @eval function Base. reshape (s:: StructArray{T} , d:: $ (type)) where {T}
462
+ StructArray {T} (map (x -> reshape (x, d), components (s)))
463
+ end
442
464
end
443
465
444
466
function showfields (io:: IO , fields:: NTuple{N, Any} ) where N
0 commit comments