Skip to content

copyto! with CartesianIndices assumes identical step in source and destination indices #45282

@jishnub

Description

@jishnub

julia/base/multidimensional.jl

Lines 1105 to 1129 in 9a0e797

function copyto!(dest::AbstractArray{T1,N}, Rdest::CartesianIndices{N},
src::AbstractArray{T2,N}, Rsrc::CartesianIndices{N}) where {T1,T2,N}
isempty(Rdest) && return dest
if size(Rdest) != size(Rsrc)
throw(ArgumentError("source and destination must have same size (got $(size(Rsrc)) and $(size(Rdest)))"))
end
checkbounds(dest, first(Rdest))
checkbounds(dest, last(Rdest))
checkbounds(src, first(Rsrc))
checkbounds(src, last(Rsrc))
src′ = unalias(dest, src)
ΔI = first(Rdest) - first(Rsrc)
if @generated
quote
@nloops $N i (n->Rsrc.indices[n]) begin
@inbounds @nref($N,dest,n->i_n+ΔI[n]) = @nref($N,src′,i)
end
end
else
for I in Rsrc
@inbounds dest[I + ΔI] = src′[I]
end
end
dest
end

Now that CartesianIndices may contain StepRanges, the implementation doesn't seem correct.

julia> a = rand(4)
4-element Vector{Float64}:
 0.7766672217743518
 0.5138339000883105
 0.7793932733439368
 0.6377375487030642

julia> b = zeros(2);

julia> copyto!(b, CartesianIndices(b), a, CartesianIndices((1:2:3,)))
2-element Vector{Float64}:
 0.7766672217743518
 0.0

julia> a[CartesianIndices((1:2:3,))]
2-element Vector{Float64}:
 0.7766672217743518
 0.7793932733439368

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]bugIndicates an unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions