Skip to content

Commit

Permalink
indexing OneTo with IdentityUnitRange preserves axes (JuliaLang#40997)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub authored and Amit Shirodkar committed Jun 9, 2021
1 parent df16c4c commit ee3e8f3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions base/indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,10 @@ getindex(S::IdentityUnitRange, i::StepRange{<:Integer}) = (@_inline_meta; @bound
show(io::IO, r::IdentityUnitRange) = print(io, "Base.IdentityUnitRange(", r.indices, ")")
iterate(S::IdentityUnitRange, s...) = iterate(S.indices, s...)

# For OneTo, the values and indices of the values are identical, so this may be defined in Base.
# In general such an indexing operation would produce offset ranges
getindex(S::OneTo, I::IdentityUnitRange{<:AbstractUnitRange{<:Integer}}) = (@_inline_meta; @boundscheck checkbounds(S, I); I)

"""
LinearIndices(A::AbstractArray)
Expand Down
10 changes: 10 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1933,3 +1933,13 @@ end
@test (10:-1:1) * 0.1 == 1:-0.1:0.1
@test 0.2 * (-2:2:2) == [-0.4, 0, 0.4]
end

@testset "Indexing OneTo with IdentityUnitRange" begin
for endpt in Any[10, big(10), UInt(10)]
r = Base.OneTo(endpt)
inds = Base.IdentityUnitRange(3:5)
rs = r[inds]
@test rs === inds
@test_throws BoundsError r[Base.IdentityUnitRange(-1:100)]
end
end

0 comments on commit ee3e8f3

Please sign in to comment.