Skip to content

Commit 23de436

Browse files
authored
preserve Slice(::OneTo) in no_offset_view (#212)
1 parent ee0dced commit 23de436

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/OffsetArrays.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ for OR in [:IIUR, :IdOffsetRange]
355355
# this method is needed for ambiguity resolution
356356
@eval @propagate_inbounds Base.getindex(r::StepRangeLen{T,<:Base.TwicePrecision,<:Base.TwicePrecision}, s::$OR) where T =
357357
OffsetArray(r[no_offset_view(s)], axes(s))
358-
358+
359359
#= Integer UnitRanges may return an appropriate AbstractUnitRange{<:Integer}, as the result may be used in indexing, and
360360
indexing is faster with ranges =#
361361
@eval @propagate_inbounds function Base.getindex(r::UnitRange{<:Integer}, s::$OR)
@@ -437,7 +437,9 @@ julia> A
437437
"""
438438
no_offset_view(A::OffsetArray) = no_offset_view(parent(A))
439439
if isdefined(Base, :IdentityUnitRange)
440-
no_offset_view(a::Base.Slice) = Base.Slice(UnitRange(a)) # valid only if Slice is distinguished from IdentityUnitRange
440+
# valid only if Slice is distinguished from IdentityUnitRange
441+
no_offset_view(a::Base.Slice{<:Base.OneTo}) = a
442+
no_offset_view(a::Base.Slice) = Base.Slice(UnitRange(a))
441443
no_offset_view(S::SubArray) = view(parent(S), map(no_offset_view, parentindices(S))...)
442444
end
443445
no_offset_view(a::Array) = a

test/runtests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,14 @@ end
11181118
a = OffsetArray(1:3, 0:2);
11191119
b = @view a[0]
11201120
@test b[] == b[1] == 1
1121+
1122+
a = reshape(1:16, 4, 4);
1123+
for ax1 in (:, axes(a,1), UnitRange(axes(a,1))),
1124+
ax2 in (:, axes(a,2), UnitRange(axes(a,2)))
1125+
av = @view a[ax1, ax2]
1126+
av_nooffset = OffsetArrays.no_offset_view(av)
1127+
@test axes(av_nooffset) === axes(av)
1128+
end
11211129
end
11221130

11231131
@testset "iteration" begin

0 commit comments

Comments
 (0)