Skip to content

Commit 00c3a1e

Browse files
timholyKristofferC
authored andcommitted
Fix colon-reshaping of OffsetVector (#33890)
* Fix colon-reshaping of OffsetVector * reshape(::AbstractVector, ::Colon) is a no-op (cherry picked from commit f80c3ee)
1 parent 8b52e21 commit 00c3a1e

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

base/reshapedarray.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ reshape(parent::AbstractArray, shp::Tuple{Union{Integer,OneTo}, Vararg{Union{Int
9696
reshape(parent::AbstractArray, dims::Dims) = _reshape(parent, dims)
9797

9898
# Allow missing dimensions with Colon():
99+
reshape(parent::AbstractVector, ::Colon) = parent
99100
reshape(parent::AbstractArray, dims::Int...) = reshape(parent, dims)
100101
reshape(parent::AbstractArray, dims::Union{Int,Colon}...) = reshape(parent, dims)
101102
reshape(parent::AbstractArray, dims::Tuple{Vararg{Union{Int,Colon}}}) = _reshape(parent, _reshape_uncolon(parent, dims))
@@ -201,6 +202,8 @@ dataids(A::ReshapedArray) = dataids(A.parent)
201202
d, r = divrem(ind, strds[1])
202203
(_ind2sub_rs(front(ax), tail(strds), r)..., d + first(ax[end]))
203204
end
205+
offset_if_vec(i::Integer, axs::Tuple{<:AbstractUnitRange}) = i + first(axs[1]) - 1
206+
offset_if_vec(i::Integer, axs::Tuple) = i
204207

205208
@inline function getindex(A::ReshapedArrayLF, index::Int)
206209
@boundscheck checkbounds(A, index)
@@ -218,8 +221,9 @@ end
218221
end
219222

220223
@inline function _unsafe_getindex(A::ReshapedArray{T,N}, indices::Vararg{Int,N}) where {T,N}
221-
i = Base._sub2ind(size(A), indices...)
222-
I = ind2sub_rs(axes(A.parent), A.mi, i)
224+
axp = axes(A.parent)
225+
i = offset_if_vec(Base._sub2ind(size(A), indices...), axp)
226+
I = ind2sub_rs(axp, A.mi, i)
223227
_unsafe_getindex_rs(parent(A), I)
224228
end
225229
@inline _unsafe_getindex_rs(A, i::Integer) = (@inbounds ret = A[i]; ret)
@@ -241,7 +245,9 @@ end
241245
end
242246

243247
@inline function _unsafe_setindex!(A::ReshapedArray{T,N}, val, indices::Vararg{Int,N}) where {T,N}
244-
@inbounds parent(A)[ind2sub_rs(axes(A.parent), A.mi, Base._sub2ind(size(A), indices...))...] = val
248+
axp = axes(A.parent)
249+
i = offset_if_vec(Base._sub2ind(size(A), indices...), axp)
250+
@inbounds parent(A)[ind2sub_rs(axes(A.parent), A.mi, i)...] = val
245251
val
246252
end
247253

test/offsetarray.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,19 @@ A = OffsetArray(rand(4,4), (-3,5))
467467
@test vec(A) == reshape(A, :) == reshape(A, 16) == reshape(A, Val(1)) == A[:] == vec(A.parent)
468468
A = OffsetArray(view(rand(4,4), 1:4, 4:-1:1), (-3,5))
469469
@test vec(A) == reshape(A, :) == reshape(A, 16) == reshape(A, Val(1)) == A[:] == vec(A.parent)
470+
# issue #33614
471+
A = OffsetArray(-1:0, (-2,))
472+
@test reshape(A, :) === A
473+
Arsc = reshape(A, :, 1)
474+
Arss = reshape(A, 2, 1)
475+
@test Arsc[1,1] == Arss[1,1] == -1
476+
@test Arsc[2,1] == Arss[2,1] == 0
477+
@test_throws BoundsError Arsc[0,1]
478+
@test_throws BoundsError Arss[0,1]
479+
A = OffsetArray([-1,0], (-2,))
480+
Arsc = reshape(A, :, 1)
481+
Arsc[1,1] = 5
482+
@test first(A) == 5
470483

471484
# broadcast
472485
a = [1]

0 commit comments

Comments
 (0)