Skip to content

Commit 0bcb332

Browse files
timholyararslan
authored andcommitted
Fix IndexLinear views with a 1-dimensional parent having offset indices
Fixes JuliaArrays/OffsetArrays.jl#27 Ref #23686 (cherry picked from commit afd56a6)
1 parent 5f9ef77 commit 0bcb332

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

base/subarray.jl

+3
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ end
260260
# Computing the first index simply steps through the indices, accumulating the
261261
# sum of index each multiplied by the parent's stride.
262262
# The running sum is `f`; the cumulative stride product is `s`.
263+
# If the parent is a vector, then we offset the parent's own indices with parameters of I
264+
compute_offset1(parent::AbstractVector, stride1::Integer, I::Tuple{Range}) =
265+
(@_inline_meta; first(I[1]) - first(indices1(I[1]))*stride1)
263266
# If the result is one-dimensional and it's a Colon, then linear
264267
# indexing uses the indices along the given dimension. Otherwise
265268
# linear indexing always starts with 1.

test/offsetarray.jl

+11
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ S = view(A, :, :)
115115
@test S[1,4] == S[4] == 4
116116
@test_throws BoundsError S[1,1]
117117
@test indices(S) === (0:1, 3:4)
118+
# https://github.com/JuliaArrays/OffsetArrays.jl/issues/27
119+
g = OffsetArray(collect(-2:3), (-3,))
120+
gv = view(g, -1:2)
121+
@test indices(gv, 1) === Base.OneTo(4)
122+
@test collect(gv) == collect(-1:2)
123+
gv = view(g, OffsetArray(-1:2, (-2,)))
124+
@test indices(gv, 1) === -1:2
125+
@test collect(gv) == collect(-1:2)
126+
gv = view(g, OffsetArray(-1:2, (-1,)))
127+
@test indices(gv, 1) === 0:3
128+
@test collect(gv) == collect(-1:2)
118129

119130
# iteration
120131
for (a,d) in zip(A, A0)

0 commit comments

Comments
 (0)