Skip to content

Commit

Permalink
Use ind2sub directly in reinterpret(reshape, ...) fallbacks (JuliaLan…
Browse files Browse the repository at this point in the history
…g#37903)

`_to_subscript_indices` doesn't do quite the same thing in
higher-dimensional contexts, so best to use `ind2sub` directly.
  • Loading branch information
timholy committed Oct 6, 2020
1 parent 08a233d commit 7f266fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 4 additions & 4 deletions base/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,13 @@ end
# but which don't handle SCartesianIndex2
function _getindex(::IndexSCartesian2, A::AbstractArray{T,N}, ind::SCartesianIndex2) where {T,N}
@_propagate_inbounds_meta
I = _to_subscript_indices(A, ind.i, ind.j)
getindex(A, I...)
J = _ind2sub(tail(axes(A)), ind.j)
getindex(A, ind.i, J...)
end
function _setindex!(::IndexSCartesian2, A::AbstractArray{T,N}, v, ind::SCartesianIndex2) where {T,N}
@_propagate_inbounds_meta
I = _to_subscript_indices(A, ind.i, ind.j)
setindex!(A, v, I...)
J = _ind2sub(tail(axes(A)), ind.j)
setindex!(A, v, ind.i, J...)
end


Expand Down
6 changes: 6 additions & 0 deletions test/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ end
@test B[1] === Complex{Int64}(11+12im)
@test B[2] === Complex{Int64}(13+14im)
@test B[3] === Complex{Int64}(15+16im)
z3 = (0x00, 0x00, 0x00)
Az = [z3 z3; z3 z3]
Azr = reinterpret(reshape, UInt8, Az)
W = WrapperArray(Azr)
copyto!(W, fill(0x01, 3, 2, 2))
@test all(isequal((0x01, 0x01, 0x01)), Az)

# ensure that reinterpret arrays aren't erroneously classified as strided
let A = reshape(1:20, 5, 4)
Expand Down

0 comments on commit 7f266fa

Please sign in to comment.