Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/chainedvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ function Base.copyto!(dest::ChainedVector{T}, doffs::Union{Signed, Unsigned},
(doffs > 0 && (doffs + n - 1) <= length(dest) &&
soffs > 0 && (soffs + n - 1) <= length(src)) || throw(ArgumentError("out of range arguments to copyto! on ChainedVector"))
n == 0 && return dest
# copying chunk by chunk would overwrite an overlapping source before reading it
src = Base.unalias(dest, src)
N = length(dest.inds)
# find first chunk where we'll start copying to
aidx, _ = index(dest, doffs)
Expand Down Expand Up @@ -452,6 +454,7 @@ function Base.copyto!(dest::AbstractVector{T}, doffs::Union{Signed, Unsigned},
(doffs > 0 && (doffs + n - 1) <= length(dest) &&
soffs > 0 && (soffs + n - 1) <= length(src)) || throw(ArgumentError("out of range arguments to copyto! on ChainedVector"))
n == 0 && return dest
src = Base.unalias(dest, src)
N = length(src.inds)
# find first chunk where we'll start copying from
aidx, i = index(src, soffs)
Expand Down Expand Up @@ -482,6 +485,7 @@ function Base.copyto!(dest::ChainedVector{T}, doffs::Union{Signed, Unsigned},
(doffs > 0 && (doffs + n - 1) <= length(dest) &&
soffs > 0 && (soffs + n - 1) <= length(src)) || throw(ArgumentError("out of range arguments to copyto! on ChainedVector"))
n == 0 && return dest
src = Base.unalias(dest, src)
cleanup!(src)
# find first chunk where we'll start copying to
dN = length(dest.inds)
Expand Down
14 changes: 14 additions & 0 deletions test/chainedvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,20 @@ end
@test sum(unique!(ChainedVector([[1],[2],[3]]))) == 6
end

@testset "copyto! within one ChainedVector handles overlap" begin
for (d, s, n) in ((2, 1, 2), (1, 2, 2), (2, 1, 4), (3, 1, 3))
# the source range is read before any write, so a copy of it gives the expected result
expected = copyto!([1, 2, 3, 4, 5], d, [1, 2, 3, 4, 5][s:s+n-1])
x = ChainedVector([[1, 2], [3], [4, 5]])
@test copyto!(x, d, x, s, n) == expected
x = ChainedVector([[1, 2], [3], [4, 5]])
@test copyto!(x, d, view(x, s:s+n-1)) == expected
x = ChainedVector([[1, 2], [3], [4, 5]])
copyto!(view(x, d:d+n-1), 1, x, s, n)
@test x == expected
end
end

@testset "no-op resizing ChainedVector" begin
emptied = empty!(ChainedVector([[1, 2], [3]]))
popped = ChainedVector([[1]])
Expand Down
Loading