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 27, 2021
1 parent 7a1e2b9 commit fcbfbba
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
6 changes: 3 additions & 3 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[UnitRange(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[UnitRange(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[UnitRange(s)]
offset_s = first(axes(s,1)) - 1
IdOffsetRange(UnitRange(rs .- offset_s), offset_s)
_maybewrapoffset(rs, offset_s, axes(s,1))
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ end
@propagate_inbounds Base.getindex(r::IdOffsetRange, i::Integer) = r.parent[i - r.offset] + r.offset
@propagate_inbounds function Base.getindex(r::IdOffsetRange, s::AbstractUnitRange{<:Integer})
offset_s = first(axes(s,1)) - 1
pr = r.parent[s .- r.offset] .+ (r.offset - offset_s)
pr = r.parent[s .- r.offset] .+ r.offset
_maybewrapoffset(pr, offset_s, axes(s,1))
end
# The following method is required to avoid falling back to getindex(::AbstractUnitRange, ::StepRange{<:Integer})
Expand Down
4 changes: 2 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ _maybewrapaxes(A::AbstractVector, ax) = OffsetArray(A, ax)

_maybewrapoffset(r::AbstractUnitRange, of, ::Base.OneTo) = no_offset_view(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)
_maybewrapoffset(r::AbstractUnitRange, of, ::Any) = IdOffsetRange(UnitRange(r .- of), of)
_maybewrapoffset(r::AbstractVector, of, axs) = OffsetArray(r, axs)
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,9 @@ end
]

test_indexing_axes_and_vals(r1, r2)
if axes(r2, 1) isa Base.OneTo
@test r1[r2] isa AbstractRange
end
end
end
end
Expand Down

0 comments on commit fcbfbba

Please sign in to comment.