Skip to content

Commit c840568

Browse files
jishnubjohanmon
authored andcommitted
indexing OneTo with IdentityUnitRange preserves axes (JuliaLang#40997)
1 parent 889ce9b commit c840568

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

base/indices.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ getindex(S::IdentityUnitRange, i::StepRange{<:Integer}) = (@_inline_meta; @bound
400400
show(io::IO, r::IdentityUnitRange) = print(io, "Base.IdentityUnitRange(", r.indices, ")")
401401
iterate(S::IdentityUnitRange, s...) = iterate(S.indices, s...)
402402

403+
# For OneTo, the values and indices of the values are identical, so this may be defined in Base.
404+
# In general such an indexing operation would produce offset ranges
405+
getindex(S::OneTo, I::IdentityUnitRange{<:AbstractUnitRange{<:Integer}}) = (@_inline_meta; @boundscheck checkbounds(S, I); I)
406+
403407
"""
404408
LinearIndices(A::AbstractArray)
405409

test/ranges.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,3 +1933,13 @@ end
19331933
@test (10:-1:1) * 0.1 == 1:-0.1:0.1
19341934
@test 0.2 * (-2:2:2) == [-0.4, 0, 0.4]
19351935
end
1936+
1937+
@testset "Indexing OneTo with IdentityUnitRange" begin
1938+
for endpt in Any[10, big(10), UInt(10)]
1939+
r = Base.OneTo(endpt)
1940+
inds = Base.IdentityUnitRange(3:5)
1941+
rs = r[inds]
1942+
@test rs === inds
1943+
@test_throws BoundsError r[Base.IdentityUnitRange(-1:100)]
1944+
end
1945+
end

0 commit comments

Comments
 (0)