Skip to content

Commit

Permalink
Consistently use linear indexing in copyto!(dest, src) (JuliaLang#34049)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablosanjose committed Feb 23, 2020
1 parent 8e6abe2 commit 3fdfb67
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
19 changes: 15 additions & 4 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -961,12 +961,23 @@ julia> y
copyto!(dest, src)

function copyto!(dest::AbstractArray{T1,N}, src::AbstractArray{T2,N}) where {T1,T2,N}
checkbounds(dest, axes(src)...)
src′ = unalias(dest, src)
for I in eachindex(IndexStyle(src′,dest), src′)
@inbounds dest[I] = src′[I]
# fastpath for equal axes (#34025)
if axes(dest) == axes(src)
for I in eachindex(IndexStyle(src′,dest), src′)
@inbounds dest[I] = src′[I]
end
# otherwise enforce linear indexing
else
isrc = eachindex(IndexLinear(), src)
idest = eachindex(IndexLinear(), dest)
ΔI = first(idest) - first(isrc)
checkbounds(dest, last(isrc) + ΔI)
for I in isrc
@inbounds dest[I + ΔI] = src′[I]
end
end
dest
return dest
end

function copyto!(dest::AbstractArray{T1,N}, Rdest::CartesianIndices{N},
Expand Down
15 changes: 15 additions & 0 deletions test/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,18 @@ end
# Test that edges are not shared
@test ci2.edges !== ci.edges
end

@testset "issue #34025" begin
s = [2 0; 0 3]
r = ones(Int, 3, 3)
@test copyto!(copy(r), s') == [2 3 1; 0 1 1; 0 1 1]
@test copyto!(copy(r), s) == copyto!(copy(r), s') ==
copyto!(copy(r)', s) == copyto!(copy(r)', s')
r = ones(Int, 3, 3)
s = [1 2 3 4]'
@test copyto!(r, s) == [1 4 1; 2 1 1; 3 1 1]
a = fill(1, 5)
r = Base.IdentityUnitRange(-1:1)
copyto!(a, r)
@test a[1:3] == [-1, 0, 1]
end
11 changes: 7 additions & 4 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,15 @@ copyto!(a, -2, (1,2,3), 1, 2)

b = 1:2 # copy between AbstractArrays
bo = OffsetArray(1:2, (-3,))
@test_throws BoundsError copyto!(a, b)
copyto!(a, b) # no BoundsError, see #34049
@test a[-3] == 1
@test a[-2] == 2
@test a[-1] == 2
fill!(a, -1)
copyto!(a, bo)
@test a[-3] == -1
@test a[-2] == 1
@test a[-1] == 2
@test a[-3] == 1
@test a[-2] == 2
@test a[-1] == -1
fill!(a, -1)
copyto!(a, -2, bo)
@test a[-3] == -1
Expand Down

0 comments on commit 3fdfb67

Please sign in to comment.