Skip to content

Commit

Permalink
to_dims methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Tokazama committed Oct 26, 2021
1 parent b63cc5e commit fc2fda4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,6 @@ struct OffsetArray{T,N,AA<:AbstractArray{T,N}} <: AbstractArray{T,N}
end

ArrayInterface.parent_type(::Type{O}) where {T,N,A<:AbstractArray{T,N},O<:OffsetArrays.OffsetArray{T,N,A}} = A
function _offset_axis_type(::Type{T}, dim::StaticInt{D}) where {T,D}
OffsetArrays.IdOffsetRange{Int,ArrayInterface.axes_types(T, dim)}
end
function ArrayInterface.axes_types(::Type{T}) where {T<:OffsetArrays.OffsetArray}
ArrayInterface.Static.eachop_tuple(_offset_axis_type, Static.nstatic(Val(ndims(T))), ArrayInterface.parent_type(T))
end
@inline ArrayInterface.axes(A::OffsetArrays.OffsetArray) = Base.axes(A)
@inline _axes(A::OffsetArrays.OffsetArray, dim::Integer) = Base.axes(A, dim)
@inline function ArrayInterface.axes(A::OffsetArrays.OffsetArray{T,N}, ::StaticInt{M}) where {T,M,N}
_axes(A, StaticInt{M}(), gt(StaticInt{M}(),StaticInt{N}()))
end

"""
OffsetVector(v, index)
Expand Down Expand Up @@ -299,10 +288,20 @@ Base.parent(A::OffsetArray) = A.parent
# Base.Broadcast.BroadcastStyle(::Type{<:OffsetArray{<:Any, <:Any, AA}}) where AA = Base.Broadcast.BroadcastStyle(AA)

@inline Base.size(A::OffsetArray) = size(parent(A))
@inline Base.size(A::OffsetArray, dim) = size(parent(A), ArrayInterface.to_dims(A, dim))

@inline Base.axes(A::OffsetArray) = map(IdOffsetRange, axes(parent(A)), A.offsets)
@inline Base.axes(A::OffsetArray, d) = d <= ndims(A) ? IdOffsetRange(axes(parent(A), d), A.offsets[d]) : IdOffsetRange(axes(parent(A), d))
@inline Base.axes(A::OffsetArray) = map(IdOffsetRange, ArrayInterface.axes(parent(A)), A.offsets)
Base.axes(A::OffsetArray, d) = axes(A, ArrayInterface.to_dims(A, d))
@inline function Base.axes(A::OffsetArray, d::Union{Int,StaticInt})
d <= ndims(A) ? IdOffsetRange(axes(parent(A), d), A.offsets[d]) : IdOffsetRange(axes(parent(A), d))
end
@inline Base.axes1(A::OffsetArray{T,0}) where {T} = IdOffsetRange(axes(parent(A), 1)) # we only need to specialize this one
function _offset_axis_type(::Type{T}, dim::StaticInt{D}) where {T,D}
OffsetArrays.IdOffsetRange{Int,ArrayInterface.axes_types(T, dim)}
end
function ArrayInterface.axes_types(::Type{T}) where {T<:OffsetArrays.OffsetArray}
Static.eachop_tuple(_offset_axis_type, Static.nstatic(Val(ndims(T))), ArrayInterface.parent_type(T))
end

# Issue 128
# See https://github.com/JuliaLang/julia/issues/37274 for the issue reported in Base
Expand Down
1 change: 1 addition & 0 deletions src/axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ end
offset_coerce(::Type{I}, r::AbstractUnitRange) where I<:AbstractUnitRange =
convert(I, r)::I, 0

ArrayInterface.parent_type(::Type{<:IdOffsetRange{<:Any,I}}) where {I} = I
@inline Base.parent(r::IdOffsetRange) = r.parent
@inline Base.axes(r::IdOffsetRange) = (Base.axes1(r),)
@inline Base.axes1(r::IdOffsetRange) = IdOffsetRange(Base.axes1(r.parent), r.offset)
Expand Down

0 comments on commit fc2fda4

Please sign in to comment.