Skip to content

Commit

Permalink
Preserve ranges in indexing with IIUR(::OneTo)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Feb 25, 2021
1 parent ee0dced commit 0411947
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
25 changes: 22 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0411947

Please sign in to comment.