Skip to content

Commit 9e5e287

Browse files
authored
axes for a Slice of an AbstractOneTo axis (#59384)
Currently, we have specialized `axes` methods for `Slice{<:OneTo}` to unwrap the `Slice`. The same logic may be extended to `Base.AbstractOneTo`, so that subtypes of `AbstractOneTo` inherit the behavior.
1 parent a776445 commit 9e5e287

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

base/indices.jl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,9 @@ end
385385
Slice(S::Slice) = S
386386
Slice{T}(S::Slice) where {T<:AbstractUnitRange} = Slice{T}(T(S.indices))
387387

388-
axes(S::Slice) = (IdentityUnitRange(S.indices),)
388+
axes(S::Slice) = (axes1(S),)
389389
axes1(S::Slice) = IdentityUnitRange(S.indices)
390-
axes(S::Slice{<:OneTo}) = (S.indices,)
391-
axes1(S::Slice{<:OneTo}) = S.indices
390+
axes1(S::Slice{<:AbstractOneTo{<:Integer}}) = S.indices
392391

393392
first(S::Slice) = first(S.indices)
394393
last(S::Slice) = last(S.indices)
@@ -414,10 +413,9 @@ IdentityUnitRange(S::IdentityUnitRange) = S
414413
IdentityUnitRange{T}(S::IdentityUnitRange) where {T<:AbstractUnitRange} = IdentityUnitRange{T}(T(S.indices))
415414

416415
# IdentityUnitRanges are offset and thus have offset axes, so they are their own axes
417-
axes(S::IdentityUnitRange) = (S,)
416+
axes(S::IdentityUnitRange) = (axes1(S),)
418417
axes1(S::IdentityUnitRange) = S
419-
axes(S::IdentityUnitRange{<:OneTo}) = (S.indices,)
420-
axes1(S::IdentityUnitRange{<:OneTo}) = S.indices
418+
axes1(S::IdentityUnitRange{<:AbstractOneTo{<:Integer}}) = S.indices
421419

422420
first(S::IdentityUnitRange) = first(S.indices)
423421
last(S::IdentityUnitRange) = last(S.indices)
@@ -465,11 +463,11 @@ end
465463
show(io::IO, r::IdentityUnitRange) = print(io, "Base.IdentityUnitRange(", r.indices, ")")
466464
iterate(S::IdentityUnitRange, s...) = iterate(S.indices, s...)
467465

468-
# For OneTo, the values and indices of the values are identical, so this may be defined in Base.
466+
# For AbstractOneTo, the values and indices of the values are identical, so this may be defined in Base.
469467
# In general such an indexing operation would produce offset ranges
470468
# This should also ideally return an AbstractUnitRange{eltype(S)}, but currently
471469
# we're restricted to eltype(::IdentityUnitRange) == Int by definition
472-
function getindex(S::OneTo, I::IdentityUnitRange{<:AbstractUnitRange{<:Integer}})
470+
function getindex(S::AbstractOneTo{<:Integer}, I::IdentityUnitRange{<:AbstractUnitRange{<:Integer}})
473471
@inline
474472
@boundscheck checkbounds(S, I)
475473
return I

test/abstractarray.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,21 @@ end
22932293
end
22942294
end
22952295

2296+
@testset "AbstractOneTo" begin
2297+
s = SizedArrays.SizedArray{(2,2)}(ones(2,2))
2298+
v = view(s, :, 1)
2299+
@test axes(v,1) isa SizedArrays.SOneTo{2}
2300+
@test eachindex(v) isa SizedArrays.SOneTo{2}
2301+
2302+
ax = axes(v,1)
2303+
@test ax[Base.IdentityUnitRange(ax)] == ax
2304+
@test ax[Base.IdentityUnitRange(2:2)] == Base.IdentityUnitRange(2:2)
2305+
2306+
# check that IdentityUnitRange behaves like Slice
2307+
@test axes(Base.IdentityUnitRange(ax), 1) === ax
2308+
@test eachindex(Base.IdentityUnitRange(ax)) === ax
2309+
end
2310+
22962311
@testset "effect inference for `iterate` for `Array` and for `Memory`" begin
22972312
for El (Float32, Real, Any)
22982313
for Arr (Memory{El}, Array{El, 0}, Vector{El}, Matrix{El}, Array{El, 3})

0 commit comments

Comments
 (0)