Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Depend on ArrayInterface #268

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ version = "1.10.7"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
Static = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"

[compat]
Adapt = "2, 3"
Aqua = "0.5"
ArrayInterface = "3"
CatIndices = "0.2"
DistributedArrays = "0.6"
Documenter = "0.27"
EllipsisNotation = "1"
FillArrays = "0.11"
Static = "0.3"
StaticArrays = "1"
julia = "0.7, 1"

Expand Down
18 changes: 16 additions & 2 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module OffsetArrays

using ArrayInterface
using Base: tail, @propagate_inbounds
@static if !isdefined(Base, :IdentityUnitRange)
const IdentityUnitRange = Base.Slice
else
using Base: IdentityUnitRange
end
using Static

export OffsetArray, OffsetMatrix, OffsetVector

Expand Down Expand Up @@ -119,6 +121,8 @@ struct OffsetArray{T,N,AA<:AbstractArray{T,N}} <: AbstractArray{T,N}
end
end

ArrayInterface.parent_type(::Type{O}) where {T,N,A<:AbstractArray{T,N},O<:OffsetArrays.OffsetArray{T,N,A}} = A

"""
OffsetVector(v, index)

Expand Down Expand Up @@ -284,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a trivial question, but what's the benefit of this?

Copy link
Member Author

@Tokazama Tokazama Oct 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The biggest benefit of ArrayInterface.axes is that it facilitates statically sized axes. This helps with arrays like ReshapedReinterpretArray where Base uses Int instead of StaticInt to define the axis length. I should clean up and document that code in ArrayInterface before completing this PR.

to_dims is described in the docs https://juliaarrays.github.io/ArrayInterface.jl/dev/#Dimensions. If there's anything there that is unclear I'd be happy to improve the docs.

EDIT: latest PR to ArrayInterface adds a bunch of examples the docs and cleanup related to axes. Once that goes through I'll make relevant changes here.

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