Skip to content

Commit

Permalink
Merge pull request #28001 from JuliaLang/teh/sliceindexing
Browse files Browse the repository at this point in the history
Fix offset LinearIndices range indexing (fixes #27986)
  • Loading branch information
timholy authored Jul 10, 2018
2 parents 8443ad3 + d5392ac commit 092b6c9
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 092b6c9

Please sign in to comment.