From 04119473f154c37400446d28486527bddd41b6cd Mon Sep 17 00:00:00 2001 From: jishnub Date: Thu, 25 Feb 2021 11:01:25 +0400 Subject: [PATCH] Preserve ranges in indexing with IIUR(::OneTo) --- src/OffsetArrays.jl | 8 ++++---- src/utils.jl | 2 +- test/runtests.jl | 25 ++++++++++++++++++++++--- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/OffsetArrays.jl b/src/OffsetArrays.jl index f02bd30a..d6e06fca 100644 --- a/src/OffsetArrays.jl +++ b/src/OffsetArrays.jl @@ -349,19 +349,19 @@ end for OR in [:IIUR, :IdOffsetRange] for R in [:StepRange, :StepRangeLen, :LinRange, :UnitRange] - @eval @propagate_inbounds Base.getindex(r::$R, s::$OR) = OffsetArray(r[no_offset_view(s)], axes(s)) + @eval @propagate_inbounds Base.getindex(r::$R, s::$OR) = _maybewrapaxes(r[UnitRange(s)], axes(s,1)) end # this method is needed for ambiguity resolution @eval @propagate_inbounds Base.getindex(r::StepRangeLen{T,<:Base.TwicePrecision,<:Base.TwicePrecision}, s::$OR) where T = - OffsetArray(r[no_offset_view(s)], axes(s)) + _maybewrapaxes(r[UnitRange(s)], axes(s,1)) #= Integer UnitRanges may return an appropriate AbstractUnitRange{<:Integer}, as the result may be used in indexing, and indexing is faster with ranges =# @eval @propagate_inbounds function Base.getindex(r::UnitRange{<:Integer}, s::$OR) - rs = r[no_offset_view(s)] + rs = r[UnitRange(s)] offset_s = first(axes(s,1)) - 1 - IdOffsetRange(UnitRange(rs .- offset_s), offset_s) + _maybewrapoffset(UnitRange(rs .- offset_s), offset_s, axes(s,1)) end end diff --git a/src/utils.jl b/src/utils.jl index ba52fce7..585a3838 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -75,7 +75,7 @@ end _maybewrapaxes(A::AbstractVector, ::Base.OneTo) = no_offset_view(A) _maybewrapaxes(A::AbstractVector, ax) = OffsetArray(A, ax) -_maybewrapoffset(r::AbstractUnitRange, of, ::Base.OneTo) = no_offset_view(r) +_maybewrapoffset(r::AbstractUnitRange, of, ::Base.OneTo) = UnitRange(r) _maybewrapoffset(r::AbstractVector, of, ::Base.OneTo) = no_offset_view(r) _maybewrapoffset(r::AbstractUnitRange, of, ::Any) = IdOffsetRange(UnitRange(r), of) _maybewrapoffset(r::AbstractVector, of, axs) = OffsetArray(r .+ of, axs) diff --git a/test/runtests.jl b/test/runtests.jl index 68cb5db8..898ce1ce 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -852,15 +852,34 @@ end # AbstractRanges with 1-based indices for r2 in [ - 5:80, - 5:2:80, - IdOffsetRange(5:80), + 5:80, + 5:2:80, + IdOffsetRange(5:80), IdOffsetRange(ZeroBasedUnitRange(4:79), 1), ] test_indexing_axes_and_vals(r1, r2) end end + + # Indexing with IdentityUnitRange(::Base.OneTo) is special as this has 1-based + # indices and the indexing operation leads to the correct axes in Base. + # The result should not be changed in this package. + # Also Issue 209 for versions v"1,0.x" + for r1 in [ + UnitRange(1.0, 99.0), + 1:99, + 1:1:99, + 1.0:1.0:99.0, + StepRangeLen(Float64(1), Float64(99), 99), + LinRange(1, 99, 99), + ] + + for r2 in [IdentityUnitRange(Base.OneTo(10))] + test_indexing_axes_and_vals(r1, r2) + @test r1[r2] isa AbstractRange + end + end end @testset "Vector indexing with offset ranges" begin