Skip to content

Commit a324170

Browse files
committed
Fix indexing for 0D views
1 parent 1263513 commit a324170

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/OffsetArrays.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ 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+
# We might not need this if the issue in Base is fixed, at that point we may impose an upper bound
242+
@inline function Base.compute_linindex(A::OffsetVector, I::NTuple{N,Any}) where N
243+
IP = Base.fill_to_length(axes(A), Base.OneTo(1), Val(N))
244+
Base.compute_linindex(first(LinearIndices(A)), 1, IP, I)
245+
end
246+
239247
Base.similar(A::OffsetArray, ::Type{T}, dims::Dims) where T =
240248
similar(parent(A), T, dims)
241249
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
@@ -943,6 +943,11 @@ end
943943
d = OffsetArray(c, 1:2)
944944
@test same_value(d, c)
945945
@test axes(d,1) == 1:2
946+
947+
# Issue 128
948+
a = OffsetArray(1:3, 0:2);
949+
b = @view a[0]
950+
@test b[] == b[1] == 1
946951
end
947952

948953
@testset "iteration" begin

0 commit comments

Comments
 (0)