Skip to content

Commit 71565b6

Browse files
committed
dispatch on types, and add tests
1 parent c38d76e commit 71565b6

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/OffsetArrays.jl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,23 @@ Base.similar(A::OffsetArray, ::Type{T}, dims::Dims) where T =
319319
similar(parent(A), T, dims)
320320

321321
"""
322-
isonebased(ax::AbstractUnitRange{<:Integer})
322+
isonebased(::Type{T}) where {T<:AbstractUnitRange{<:Integer}}
323323
324-
Return whether `firstindex(ax)` is statically known to be `1`. Custom axis types may extend this method
325-
to ensure that the axis offset is ignored, for example in `similar`.
324+
Return whether `first(ax::T)` is statically known to be `1` for any `ax` of type `T`
325+
(`false` by default).
326+
Custom axis types may extend this method to ensure that the axis offset is ignored,
327+
for example in `similar`. This may strip `OffsetArray` wrappers on occasion, or dispatch
328+
to `Base` methods for 1-based axes.
329+
330+
For axes that are essentially wrappers around another `AbstractUnitRange`,
331+
and share their indexing with their parents, one may forward the type of the
332+
parent range to `isonebased`.
326333
"""
327-
isonebased(_) = false
328-
isonebased(::Integer) = true
329-
isonebased(::Base.OneTo) = true
330-
isonebased(r::IIUR) = isonebased(r.indices)
334+
isonebased(x) = isonebased(typeof(x))
335+
isonebased(::Type) = false
336+
isonebased(::Type{<:Integer}) = true
337+
isonebased(::Type{<:Base.OneTo}) = true
338+
isonebased(::Type{IdentityUnitRange{T}}) where {T} = isonebased(T)
331339

332340
# Since the following is committing type-piracy, we provide an opt-out mechanism to the users
333341
function Base.similar(A::AbstractArray, ::Type{T}, shape::Tuple{OffsetAxisKnownLength,Vararg{OffsetAxisKnownLength}}) where T

test/runtests.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ using EllipsisNotation
99
using FillArrays
1010
using LinearAlgebra
1111
using OffsetArrays
12-
using OffsetArrays: IdentityUnitRange, no_offset_view, IIUR, Origin, IdOffsetRange
12+
using OffsetArrays: IdentityUnitRange, no_offset_view, IIUR, Origin, IdOffsetRange, isonebased
1313
using StaticArrays
1414
using Test
1515

@@ -2262,6 +2262,14 @@ Base.size(x::PointlessWrapper) = size(parent(x))
22622262
Base.axes(x::PointlessWrapper) = axes(parent(x))
22632263
Base.getindex(x::PointlessWrapper, i...) = x.parent[i...]
22642264

2265+
@testset "isonebased" begin
2266+
@test isonebased(Base.OneTo(2))
2267+
@test !isonebased(1:2)
2268+
@test isonebased(IdentityUnitRange(Base.OneTo(2)))
2269+
@test !isonebased(IdentityUnitRange(1:2))
2270+
@test similar(zeros(1), Int, IdentityUnitRange(Base.OneTo(2))) isa Vector{Int}
2271+
end
2272+
22652273
@testset "no offset view" begin
22662274
# OffsetArray fallback
22672275
A = randn(3, 3)

0 commit comments

Comments
 (0)