Skip to content

Commit

Permalink
Fix offset LinearIndices range indexing (fixes #27986)
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Jul 9, 2018
1 parent b6f9244 commit d5392ac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ end
function getindex(iter::LinearIndices, i::AbstractRange{<:Integer})
@_inline_meta
@boundscheck checkbounds(iter, i)
@inbounds (first(iter):last(iter))[i]
@inbounds isa(iter, LinearIndices{1}) ? iter.indices[1][i] : (first(iter):last(iter))[i]
end
# More efficient iteration — predominantly for non-vector LinearIndices
# but one-dimensional LinearIndices must be special-cased to support OffsetArrays
Expand Down
23 changes: 23 additions & 0 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ for i = 1:9 @test A_3_3[i] == i end
@test eachindex(A) == 1:4
@test eachindex(S) == CartesianIndices(axes(S)) == CartesianIndices(map(Base.Slice, (0:1,3:4)))

# LinearIndices
# issue 27986
let a1 = [11,12,13], a2 = [1 2; 3 4]
b1 = OffsetArray(a1, (-3,))
i1 = LinearIndices(b1)
@test i1[-2] == -2
@test_throws BoundsError i1[-3]
@test_throws BoundsError i1[1]
@test i1[-2:end] === -2:0
@test @inferred(i1[-2:0]) === -2:0
@test_throws BoundsError i1[-3:end]
@test_throws BoundsError i1[-2:1]
b2 = OffsetArray(a2, (-3,5))
i2 = LinearIndices(b2)
@test i2[3] == 3
@test_throws BoundsError i2[0]
@test_throws BoundsError i2[5]
@test @inferred(i2[2:3]) === 2:3
@test @inferred(i2[1:2:4]) === 1:2:3
@test_throws BoundsError i2[1:5]
@test_throws BoundsError i2[1:2:5]
end

# logical indexing
@test A[A .> 2] == [3,4]
@test_throws BoundsError h[trues(2)]
Expand Down

0 comments on commit d5392ac

Please sign in to comment.