Skip to content

Commit ee0fe71

Browse files
authored
Fix linear indexing for 0D views of OffsetVectors (#197)
Fix by adding a method for Julia versions < 1.6.
1 parent 97cef1a commit ee0fe71

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/OffsetArrays.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,17 @@ Base.eachindex(::IndexLinear, A::OffsetVector) = axes(A, 1)
236236
@inline Base.axes(A::OffsetArray, d) = d <= ndims(A) ? IdOffsetRange(axes(parent(A), d), A.offsets[d]) : IdOffsetRange(axes(parent(A), d))
237237
@inline Base.axes1(A::OffsetArray{T,0}) where {T} = IdOffsetRange(axes(parent(A), 1)) # we only need to specialize this one
238238

239+
# Issue 128
240+
# See https://github.com/JuliaLang/julia/issues/37274 for the issue reported in Base
241+
# The fix https://github.com/JuliaLang/julia/pull/39404 should be available on v1.6
242+
# The following method is added on older Julia versions to ensure correct behavior for OffsetVectors
243+
if VERSION < v"1.6"
244+
@inline function Base.compute_linindex(A::OffsetVector, I::NTuple{N,Any}) where N
245+
IP = Base.fill_to_length(axes(A), Base.OneTo(1), Val(N))
246+
Base.compute_linindex(first(LinearIndices(A)), 1, IP, I)
247+
end
248+
end
249+
239250
Base.similar(A::OffsetArray, ::Type{T}, dims::Dims) where T =
240251
similar(parent(A), T, dims)
241252
function Base.similar(A::AbstractArray, ::Type{T}, inds::Tuple{OffsetAxisKnownLength,Vararg{OffsetAxisKnownLength}}) where T

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,11 @@ end
936936
d = OffsetArray(c, 1:2)
937937
@test same_value(d, c)
938938
@test axes(d,1) == 1:2
939+
940+
# Issue 128
941+
a = OffsetArray(1:3, 0:2);
942+
b = @view a[0]
943+
@test b[] == b[1] == 1
939944
end
940945

941946
@testset "iteration" begin

0 commit comments

Comments
 (0)