Skip to content

Commit 44b06f2

Browse files
committed
Improve organization / reduce organizational change vs earlier.
1 parent 39f0bfd commit 44b06f2

File tree

4 files changed

+100
-105
lines changed

4 files changed

+100
-105
lines changed

src/ArrayInterface.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,10 +744,9 @@ end
744744

745745
include("static.jl")
746746
include("ranges.jl")
747-
include("dimensions.jl")
748747
include("indexing.jl")
748+
include("dimensions.jl")
749749
include("stridelayout.jl")
750-
include("generated_funcs.jl")
751750

752751
function __init__()
753752

src/dimensions.jl

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ Returns the mapping from parent dimensions to child dimensions.
2626
from_parent_dims(::Type{T}) where {T} = nstatic(Val(ndims(T)))
2727
from_parent_dims(::Type{T}) where {T<:Union{Transpose,Adjoint}} = (StaticInt(2), One())
2828
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
2942
function from_parent_dims(::Type{<:PermutedDimsArray{T,N,<:Any,I}}) where {T,N,I}
3043
return _val_to_static(Val(I))
3144
end
@@ -40,6 +53,17 @@ to_parent_dims(::Type{T}) where {T} = nstatic(Val(ndims(T)))
4053
to_parent_dims(::Type{T}) where {T<:Union{Transpose,Adjoint}} = (StaticInt(2), One())
4154
to_parent_dims(::Type{<:PermutedDimsArray{T,N,I}}) where {T,N,I} = _val_to_static(Val(I))
4255
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
4367

4468
"""
4569
has_dimnames(::Type{T}) -> Bool
@@ -222,19 +246,72 @@ end
222246
@inline function axes_types(::Type{T}) where {P,I,T<:SubArray{<:Any,<:Any,P,I}}
223247
return _sub_axes_types(Val(ArrayStyle(T)), I, axes_types(P))
224248
end
225-
226249
@inline function axes_types(::Type{T}) where {T<:Base.ReinterpretArray}
227250
return _reinterpret_axes_types(
228251
axes_types(parent_type(T)),
229252
eltype(T),
230253
eltype(parent_type(T)),
231254
)
232255
end
233-
234256
function axes_types(::Type{T}) where {N,T<:Base.ReshapedArray{<:Any,N}}
235257
return Tuple{Vararg{OptionallyStaticUnitRange{One,Int},N}}
236258
end
237259

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+
238315

239316
"""
240317
size(A)
@@ -266,6 +343,19 @@ end
266343
function strides(B::S) where {N,NP,T,A<:AbstractArray{T,NP},I,S<:SubArray{T,N,A,I}}
267344
return _strides(strides(parent(B)), B.indices)
268345
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
269359
@inline size(v::AbstractVector) = (static_length(v),)
270360
@inline size(B::MatAdjTrans) = permute(size(parent(B)), Val{(2, 1)}())
271361
@inline function size(B::PermutedDimsArray{T,N,I1,I2,A}) where {T,N,I1,I2,A}

src/generated_funcs.jl

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/stridelayout.jl

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -313,27 +313,7 @@ end
313313
_known_length(::Nothing, _, __) = nothing
314314
@inline _known_length(L::Integer, ::Type{T}, ::Type{P}) where {T,P} = L * sizeof(P) ÷ sizeof(T)
315315

316-
# These methods help handle identifying axes that dont' directly propagate from the
317-
# parent array axes. They may be worth making a formal part of the API, as they provide
318-
# a low traffic spot to change what axes_types produces.
319-
@inline function sub_axis_type(::Type{A}, ::Type{I}) where {A,I}
320-
if known_length(I) === nothing
321-
return OptionallyStaticUnitRange{One,Int}
322-
else
323-
return OptionallyStaticUnitRange{One,StaticInt{known_length(I)}}
324-
end
325-
end
326316

327-
@inline function reinterpret_axis_type(::Type{A}, ::Type{T}, ::Type{S}) where {A,T,S}
328-
if known_length(A) === nothing
329-
return OptionallyStaticUnitRange{One,Int}
330-
else
331-
return OptionallyStaticUnitRange{
332-
One,
333-
StaticInt{Int(known_length(A) / (sizeof(T) / sizeof(S)))},
334-
}
335-
end
336-
end
337317

338318
"""
339319
known_offsets(::Type{T}[, d]) -> Tuple
@@ -343,6 +323,13 @@ not known at compile time `nothing` is returned its position.
343323
"""
344324
@inline known_offsets(x, d) = known_offsets(x)[to_dims(x, d)]
345325
known_offsets(x) = known_offsets(typeof(x))
326+
@generated function known_offsets(::Type{T}) where {T}
327+
out = Expr(:tuple)
328+
for p in axes_types(T).parameters
329+
push!(out.args, known_first(p))
330+
end
331+
return out
332+
end
346333

347334
"""
348335
known_size(::Type{T}[, d]) -> Tuple

0 commit comments

Comments
 (0)