From a32417048eef6a7488ed96e8b09bb1b2165d5b5f Mon Sep 17 00:00:00 2001 From: jishnub Date: Tue, 26 Jan 2021 10:01:16 +0400 Subject: [PATCH 1/2] Fix indexing for 0D views --- src/OffsetArrays.jl | 8 ++++++++ test/runtests.jl | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/src/OffsetArrays.jl b/src/OffsetArrays.jl index 582661dd..5fd83908 100644 --- a/src/OffsetArrays.jl +++ b/src/OffsetArrays.jl @@ -236,6 +236,14 @@ Base.eachindex(::IndexLinear, A::OffsetVector) = axes(A, 1) @inline Base.axes(A::OffsetArray, d) = d <= ndims(A) ? IdOffsetRange(axes(parent(A), d), A.offsets[d]) : IdOffsetRange(axes(parent(A), d)) @inline Base.axes1(A::OffsetArray{T,0}) where {T} = IdOffsetRange(axes(parent(A), 1)) # we only need to specialize this one +# Issue 128 +# See https://github.com/JuliaLang/julia/issues/37274 for the issue reported in Base +# We might not need this if the issue in Base is fixed, at that point we may impose an upper bound +@inline function Base.compute_linindex(A::OffsetVector, I::NTuple{N,Any}) where N + IP = Base.fill_to_length(axes(A), Base.OneTo(1), Val(N)) + Base.compute_linindex(first(LinearIndices(A)), 1, IP, I) +end + Base.similar(A::OffsetArray, ::Type{T}, dims::Dims) where T = similar(parent(A), T, dims) function Base.similar(A::AbstractArray, ::Type{T}, inds::Tuple{OffsetAxisKnownLength,Vararg{OffsetAxisKnownLength}}) where T diff --git a/test/runtests.jl b/test/runtests.jl index cdce5773..bdf2f525 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -943,6 +943,11 @@ end d = OffsetArray(c, 1:2) @test same_value(d, c) @test axes(d,1) == 1:2 + + # Issue 128 + a = OffsetArray(1:3, 0:2); + b = @view a[0] + @test b[] == b[1] == 1 end @testset "iteration" begin From 0b762762cecfd6b502bddc07fe15d6920980e79e Mon Sep 17 00:00:00 2001 From: jishnub Date: Tue, 2 Feb 2021 10:50:33 +0400 Subject: [PATCH 2/2] add version upper bound --- src/OffsetArrays.jl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/OffsetArrays.jl b/src/OffsetArrays.jl index 5fd83908..def2c11e 100644 --- a/src/OffsetArrays.jl +++ b/src/OffsetArrays.jl @@ -238,10 +238,13 @@ Base.eachindex(::IndexLinear, A::OffsetVector) = axes(A, 1) # Issue 128 # See https://github.com/JuliaLang/julia/issues/37274 for the issue reported in Base -# We might not need this if the issue in Base is fixed, at that point we may impose an upper bound -@inline function Base.compute_linindex(A::OffsetVector, I::NTuple{N,Any}) where N - IP = Base.fill_to_length(axes(A), Base.OneTo(1), Val(N)) - Base.compute_linindex(first(LinearIndices(A)), 1, IP, I) +# The fix https://github.com/JuliaLang/julia/pull/39404 should be available on v1.6 +# The following method is added on older Julia versions to ensure correct behavior for OffsetVectors +if VERSION < v"1.6" + @inline function Base.compute_linindex(A::OffsetVector, I::NTuple{N,Any}) where N + IP = Base.fill_to_length(axes(A), Base.OneTo(1), Val(N)) + Base.compute_linindex(first(LinearIndices(A)), 1, IP, I) + end end Base.similar(A::OffsetArray, ::Type{T}, dims::Dims) where T =