Skip to content

Commit 8d2830f

Browse files
authored
Simplify lazy_axes (#333)
Previously we used a generated function for part of the implementation. This PR resolves that.
1 parent cb39428 commit 8d2830f

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "6.0.19"
3+
version = "6.0.20"
44

55
[deps]
66
ArrayInterfaceCore = "30b0a656-2188-435a-8636-2ec0e6a096e2"

src/axes.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,14 @@ Base.show(io::IO, x::LazyAxis{N}) where {N} = print(io, "LazyAxis{$N}($(parent(x
237237
Produces a tuple of axes where each axis is constructed lazily. If an axis of `x` is already
238238
constructed or it is simply retrieved.
239239
"""
240-
@generated function lazy_axes(x::X) where {X}
241-
Expr(:block, Expr(:meta, :inline), Expr(:tuple, [:(LazyAxis{$dim}(x)) for dim in 1:ndims(X)]...))
242-
end
240+
@inline lazy_axes(x) = lazy_axes(x, ntuple(static, StaticInt(ndims(x))))
243241
lazy_axes(x::Union{LinearIndices,CartesianIndices,AbstractRange}) = axes(x)
244-
@inline lazy_axes(x::VecAdjTrans) = (SOneTo{1}(), first(lazy_axes(parent(x))))
245-
@inline function lazy_axes(x::Union{PermutedDimsArray,MatAdjTrans})
246-
map(GetIndex{false}(lazy_axes(parent(x))), to_parent_dims(x))
247-
end
242+
@inline function lazy_axes(x::PermutedDimsArray, ::StaticInt{N}) where {N}
243+
N <= ndims(x) ? lazy_axes(parent(x), getfield(to_parent_dims(x), N)) : SOneTo{1}()
244+
end
245+
lazy_axes(x::Union{Adjoint,Transpose}, ::StaticInt{1}) = lazy_axes(parent(x), StaticInt(2))
246+
lazy_axes(x::Union{Adjoint,Transpose}, ::StaticInt{2}) = lazy_axes(parent(x), StaticInt(1))
247+
lazy_axes(x::AbstractRange, ::StaticInt{1}) = Base.axes1(x)
248+
lazy_axes(x, ::Colon) = LazyAxis{:}(x)
249+
lazy_axes(x, ::StaticInt{dim}) where {dim} = ndims(x) < dim ? SOneTo{1}() : LazyAxis{dim}(x)
250+
@inline lazy_axes(x, dims::Tuple) = map(Base.Fix1(lazy_axes, x), dims)

src/indexing.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ struct IndexedMappedArray{A}
8888
a::A
8989
end
9090
@inline (ima::IndexedMappedArray{A})(idx::I, ::StaticInt{0}) where {A,I} = to_index(StaticInt(1):StaticInt(1), idx)
91-
@inline (ima::IndexedMappedArray{A})(idx::I, ::Colon) where {A,I} = to_index(LazyAxis{:}(ima.a), idx)
92-
@inline (ima::IndexedMappedArray{A})(idx::I, ::StaticInt{dim}) where {A,I,dim} = to_index(LazyAxis{dim}(ima.a), idx)
91+
@inline (ima::IndexedMappedArray{A})(idx::I, ::Colon) where {A,I} = to_index(lazy_axes(ima.a, :), idx)
92+
@inline (ima::IndexedMappedArray{A})(idx::I, d::StaticInt{D}) where {A,I,D} = to_index(lazy_axes(ima.a, d), idx)
9393
@inline function (ima::IndexedMappedArray{A})(idx::AbstractArray{Bool}, dims::Tuple) where {A}
9494
if (last(dims) == ndims(A)) && (IndexStyle(A) isa IndexLinear)
9595
return LogicalIndex{Int}(idx)
@@ -99,9 +99,8 @@ end
9999
end
100100
@inline (ima::IndexedMappedArray{A})(idx::CartesianIndex, ::Tuple) where {A} = getfield(idx, 1)
101101
@inline function (ima::IndexedMappedArray{A})(idx::I, dims::Tuple) where {A,I}
102-
to_index(CartesianIndices(map(Base.Fix1(_to_lazy_axes, ima.a), dims)), idx)
102+
to_index(CartesianIndices(lazy_axes(ima.a, dims)), idx)
103103
end
104-
@inline _to_lazy_axes(a::A, ::StaticInt{dim}) where {A,dim} = LazyAxis{dim}(a)
105104

106105
"""
107106
ArrayInterface.to_index([::IndexStyle, ]axis, arg) -> index

0 commit comments

Comments
 (0)