From c645f54428eba336ccc9af26f7bea767f7f00ca5 Mon Sep 17 00:00:00 2001 From: Matt Bauman Date: Mon, 25 Jan 2021 14:52:55 -0500 Subject: [PATCH 1/2] =?UTF-8?q?fix=20#39379,=20use=20first=E2=88=98LinearI?= =?UTF-8?q?ndices=20instead=20of=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit to resolve a zero-index getindex to a linear index. --- base/abstractarray.jl | 2 +- test/offsetarray.jl | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 383814ff38ddc..a880b4eecfd3f 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1193,7 +1193,7 @@ function _getindex(::IndexLinear, A::AbstractArray, I::Vararg{Int,M}) where M end _to_linear_index(A::AbstractArray, i::Integer) = i _to_linear_index(A::AbstractVector, i::Integer, I::Integer...) = i -_to_linear_index(A::AbstractArray) = 1 +_to_linear_index(A::AbstractArray) = first(LinearIndices(A)) _to_linear_index(A::AbstractArray, I::Integer...) = (@_inline_meta; _sub2ind(A, I...)) ## IndexCartesian Scalar indexing: Canonical method is full dimensionality of Ints diff --git a/test/offsetarray.jl b/test/offsetarray.jl index bb0b4b13b8da4..e411da3eadd2e 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -715,6 +715,16 @@ end @test axes(S) == (OffsetArrays.IdOffsetRange(0:1), Base.OneTo(2), OffsetArrays.IdOffsetRange(2:5)) end +@testset "Zero-index indexing" begin + @test OffsetArray([6], 2:2)[] == 6 + @test OffsetArray(fill(6, 1, 1), 2:2, 3:3)[] == 6 + @test OffsetArray(fill(6))[] == 6 + + @test_throws BoundsError OffsetArray([6,7], 2:3)[] + @test_throws BoundsError OffsetArray([6 7], 2:2, 2:3)[] + @test_throws BoundsError OffsetArray([], 2:1)[] +end + @testset "IdentityUnitRange indexing" begin a = OffsetVector(3:4, 2:3) ax = IdentityUnitRange(2:3) From 6681d56500483a9ecb355af135e3af3aa804c9fb Mon Sep 17 00:00:00 2001 From: Matt Bauman Date: Mon, 25 Jan 2021 15:33:06 -0500 Subject: [PATCH 2/2] whitespace --- test/offsetarray.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/offsetarray.jl b/test/offsetarray.jl index e411da3eadd2e..07f2e12999f90 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -719,7 +719,6 @@ end @test OffsetArray([6], 2:2)[] == 6 @test OffsetArray(fill(6, 1, 1), 2:2, 3:3)[] == 6 @test OffsetArray(fill(6))[] == 6 - @test_throws BoundsError OffsetArray([6,7], 2:3)[] @test_throws BoundsError OffsetArray([6 7], 2:2, 2:3)[] @test_throws BoundsError OffsetArray([], 2:1)[]