@@ -26,6 +26,19 @@ Returns the mapping from parent dimensions to child dimensions.
26
26
from_parent_dims (:: Type{T} ) where {T} = nstatic (Val (ndims (T)))
27
27
from_parent_dims (:: Type{T} ) where {T<: Union{Transpose,Adjoint} } = (StaticInt (2 ), One ())
28
28
from_parent_dims (:: Type{<:SubArray{T,N,A,I}} ) where {T,N,A,I} = _from_sub_dims (A, I)
29
+ @generated function _from_sub_dims (:: Type{A} , :: Type{I} ) where {A,N,I<: Tuple{Vararg{Any,N}} }
30
+ out = Expr (:tuple )
31
+ n = 1
32
+ for p in I. parameters
33
+ if argdims (A, p) > 0
34
+ push! (out. args, :(StaticInt ($ n)))
35
+ n += 1
36
+ else
37
+ push! (out. args, :(StaticInt (0 )))
38
+ end
39
+ end
40
+ out
41
+ end
29
42
function from_parent_dims (:: Type{<:PermutedDimsArray{T,N,<:Any,I}} ) where {T,N,I}
30
43
return _val_to_static (Val (I))
31
44
end
@@ -40,6 +53,17 @@ to_parent_dims(::Type{T}) where {T} = nstatic(Val(ndims(T)))
40
53
to_parent_dims (:: Type{T} ) where {T<: Union{Transpose,Adjoint} } = (StaticInt (2 ), One ())
41
54
to_parent_dims (:: Type{<:PermutedDimsArray{T,N,I}} ) where {T,N,I} = _val_to_static (Val (I))
42
55
to_parent_dims (:: Type{<:SubArray{T,N,A,I}} ) where {T,N,A,I} = _to_sub_dims (A, I)
56
+ @generated function _to_sub_dims (:: Type{A} , :: Type{I} ) where {A,N,I<: Tuple{Vararg{Any,N}} }
57
+ out = Expr (:tuple )
58
+ n = 1
59
+ for p in I. parameters
60
+ if argdims (A, p) > 0
61
+ push! (out. args, :(StaticInt ($ n)))
62
+ end
63
+ n += 1
64
+ end
65
+ out
66
+ end
43
67
44
68
"""
45
69
has_dimnames(::Type{T}) -> Bool
@@ -222,19 +246,72 @@ end
222
246
@inline function axes_types (:: Type{T} ) where {P,I,T<: SubArray{<:Any,<:Any,P,I} }
223
247
return _sub_axes_types (Val (ArrayStyle (T)), I, axes_types (P))
224
248
end
225
-
226
249
@inline function axes_types (:: Type{T} ) where {T<: Base.ReinterpretArray }
227
250
return _reinterpret_axes_types (
228
251
axes_types (parent_type (T)),
229
252
eltype (T),
230
253
eltype (parent_type (T)),
231
254
)
232
255
end
233
-
234
256
function axes_types (:: Type{T} ) where {N,T<: Base.ReshapedArray{<:Any,N} }
235
257
return Tuple{Vararg{OptionallyStaticUnitRange{One,Int},N}}
236
258
end
237
259
260
+ # These methods help handle identifying axes that don't directly propagate from the
261
+ # parent array axes. They may be worth making a formal part of the API, as they provide
262
+ # a low traffic spot to change what axes_types produces.
263
+ @inline function sub_axis_type (:: Type{A} , :: Type{I} ) where {A,I}
264
+ if known_length (I) === nothing
265
+ return OptionallyStaticUnitRange{One,Int}
266
+ else
267
+ return OptionallyStaticUnitRange{One,StaticInt{known_length (I)}}
268
+ end
269
+ end
270
+ @generated function _sub_axes_types (
271
+ :: Val{S} ,
272
+ :: Type{I} ,
273
+ :: Type{PI} ,
274
+ ) where {S,I<: Tuple ,PI<: Tuple }
275
+ out = Expr (:curly , :Tuple )
276
+ d = 1
277
+ for i in I. parameters
278
+ ad = argdims (S, i)
279
+ if ad > 0
280
+ push! (out. args, :(sub_axis_type ($ (PI. parameters[d]), $ i)))
281
+ d += ad
282
+ else
283
+ d += 1
284
+ end
285
+ end
286
+ Expr (:block , Expr (:meta , :inline ), out)
287
+ end
288
+ @inline function reinterpret_axis_type (:: Type{A} , :: Type{T} , :: Type{S} ) where {A,T,S}
289
+ if known_length (A) === nothing
290
+ return OptionallyStaticUnitRange{One,Int}
291
+ else
292
+ return OptionallyStaticUnitRange{
293
+ One,
294
+ StaticInt{Int (known_length (A) / (sizeof (T) / sizeof (S)))},
295
+ }
296
+ end
297
+ end
298
+ @generated function _reinterpret_axes_types (
299
+ :: Type{I} ,
300
+ :: Type{T} ,
301
+ :: Type{S} ,
302
+ ) where {I<: Tuple ,T,S}
303
+ out = Expr (:curly , :Tuple )
304
+ for i = 1 : length (I. parameters)
305
+ if i === 1
306
+ push! (out. args, reinterpret_axis_type (I. parameters[1 ], T, S))
307
+ else
308
+ push! (out. args, I. parameters[i])
309
+ end
310
+ end
311
+ Expr (:block , Expr (:meta , :inline ), out)
312
+ end
313
+
314
+
238
315
239
316
"""
240
317
size(A)
266
343
function strides (B:: S ) where {N,NP,T,A<: AbstractArray{T,NP} ,I,S<: SubArray{T,N,A,I} }
267
344
return _strides (strides (parent (B)), B. indices)
268
345
end
346
+ @generated function _size (A:: Tuple{Vararg{Any,N}} , inds:: I , l:: L ) where {N,I<: Tuple ,L}
347
+ t = Expr (:tuple )
348
+ for n = 1 : N
349
+ if (I. parameters[n] <: Base.Slice )
350
+ push! (t. args, :(@inbounds (_try_static (A[$ n], l[$ n]))))
351
+ elseif I. parameters[n] <: Number
352
+ nothing
353
+ else
354
+ push! (t. args, Expr (:ref , :l , n))
355
+ end
356
+ end
357
+ Expr (:block , Expr (:meta , :inline ), t)
358
+ end
269
359
@inline size (v:: AbstractVector ) = (static_length (v),)
270
360
@inline size (B:: MatAdjTrans ) = permute (size (parent (B)), Val {(2, 1)} ())
271
361
@inline function size (B:: PermutedDimsArray{T,N,I1,I2,A} ) where {T,N,I1,I2,A}
0 commit comments