Skip to content

Commit f80c3ee

Browse files
timholyStefanKarpinski
authored andcommitted
Fix colon-reshaping of OffsetVector (#33890)
* Fix colon-reshaping of OffsetVector * reshape(::AbstractVector, ::Colon) is a no-op
1 parent 5e0f0df commit f80c3ee

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
@@ -112,6 +112,7 @@ reshape(parent::AbstractArray, shp::Tuple{Union{Integer,OneTo}, Vararg{Union{Int
112112
reshape(parent::AbstractArray, dims::Dims) = _reshape(parent, dims)
113113

114114
# Allow missing dimensions with Colon():
115+
reshape(parent::AbstractVector, ::Colon) = parent
115116
reshape(parent::AbstractArray, dims::Int...) = reshape(parent, dims)
116117
reshape(parent::AbstractArray, dims::Union{Int,Colon}...) = reshape(parent, dims)
117118
reshape(parent::AbstractArray, dims::Tuple{Vararg{Union{Int,Colon}}}) = _reshape(parent, _reshape_uncolon(parent, dims))
@@ -220,6 +221,8 @@ dataids(A::ReshapedArray) = dataids(A.parent)
220221
d, r = divrem(ind, strds[1])
221222
(_ind2sub_rs(front(ax), tail(strds), r)..., d + first(ax[end]))
222223
end
224+
offset_if_vec(i::Integer, axs::Tuple{<:AbstractUnitRange}) = i + first(axs[1]) - 1
225+
offset_if_vec(i::Integer, axs::Tuple) = i
223226

224227
@inline function getindex(A::ReshapedArrayLF, index::Int)
225228
@boundscheck checkbounds(A, index)
@@ -237,8 +240,9 @@ end
237240
end
238241

239242
@inline function _unsafe_getindex(A::ReshapedArray{T,N}, indices::Vararg{Int,N}) where {T,N}
240-
i = Base._sub2ind(size(A), indices...)
241-
I = ind2sub_rs(axes(A.parent), A.mi, i)
243+
axp = axes(A.parent)
244+
i = offset_if_vec(Base._sub2ind(size(A), indices...), axp)
245+
I = ind2sub_rs(axp, A.mi, i)
242246
_unsafe_getindex_rs(parent(A), I)
243247
end
244248
@inline _unsafe_getindex_rs(A, i::Integer) = (@inbounds ret = A[i]; ret)
@@ -260,7 +264,9 @@ end
260264
end
261265

262266
@inline function _unsafe_setindex!(A::ReshapedArray{T,N}, val, indices::Vararg{Int,N}) where {T,N}
263-
@inbounds parent(A)[ind2sub_rs(axes(A.parent), A.mi, Base._sub2ind(size(A), indices...))...] = val
267+
axp = axes(A.parent)
268+
i = offset_if_vec(Base._sub2ind(size(A), indices...), axp)
269+
@inbounds parent(A)[ind2sub_rs(axes(A.parent), A.mi, i)...] = val
264270
val
265271
end
266272

test/offsetarray.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,19 @@ A = OffsetArray(rand(4,4), (-3,5))
495495
@test vec(A) == reshape(A, :) == reshape(A, 16) == reshape(A, Val(1)) == A[:] == vec(A.parent)
496496
A = OffsetArray(view(rand(4,4), 1:4, 4:-1:1), (-3,5))
497497
@test vec(A) == reshape(A, :) == reshape(A, 16) == reshape(A, Val(1)) == A[:] == vec(A.parent)
498+
# issue #33614
499+
A = OffsetArray(-1:0, (-2,))
500+
@test reshape(A, :) === A
501+
Arsc = reshape(A, :, 1)
502+
Arss = reshape(A, 2, 1)
503+
@test Arsc[1,1] == Arss[1,1] == -1
504+
@test Arsc[2,1] == Arss[2,1] == 0
505+
@test_throws BoundsError Arsc[0,1]
506+
@test_throws BoundsError Arss[0,1]
507+
A = OffsetArray([-1,0], (-2,))
508+
Arsc = reshape(A, :, 1)
509+
Arsc[1,1] = 5
510+
@test first(A) == 5
498511

499512
# broadcast
500513
a = [1]

0 commit comments

Comments
 (0)