Skip to content

Add 5 arg copyto! into Vector with matching type #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
19 changes: 19 additions & 0 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ function _disk_copyto!(dest, Rdest, src, Rsrc)
return dest
end
view(dest, Rdest) .= view(src, Rsrc)
return dest
end
function _disk_copyto_same_type_vector!(dest, dstart, src, sstart, n)
if iszero(n)
return dest
end
if n < 0
throw(ArgumentError(LazyString("tried to copy n=",
n," elements, but n should be non-negative")))
end
destv = view(dest, range(dstart, length=n))
DiskArrays.readblock!(src, destv, range(sstart, length=n))
return dest
end

# Use a view for lazy reverse
Expand Down Expand Up @@ -76,6 +89,12 @@ macro implement_array_methods(t)
function Base.copyto!(dest::PermutedDimsArray{T,N}, src::$t{T,N}) where {T,N}
return $_disk_copyto!(dest, src)
end
function Base.copyto!(dest::Vector{T}, dstart::Integer, src::$t{T, 1}, sstart::Integer, n::Integer) where {T}
Copy link
Collaborator

@rafaqz rafaqz Jun 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is T always identical for scr and dest in Base copyto! ? What if you have different Real types

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the copyto! in Base uses setindex! so will call convert on each element.
I added a version that uses broadcasting to support that case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming here that readblock! requires the element types to match, and aout to be stored in contiguous memory.

Copy link
Collaborator

@rafaqz rafaqz Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also use setindex! when writing into the chunk buffer, so it should be the same?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. Looking at the docs, it just says "The results shall be written into aout" but I'm not sure if there are implementations of readblock! that make other assumptions about aout's eltype, or memory layout.

return $_disk_copyto_same_type_vector!(dest, dstart, src, sstart, n)
end
function Base.copyto!(dest::SubArray{T, 1, Vector{T}, <:Tuple{AbstractUnitRange}, true}, dstart::Integer, src::$t{T, 1}, sstart::Integer, n::Integer) where {T}
return $_disk_copyto_same_type_vector!(dest, dstart, src, sstart, n)
end

Base.reverse(a::$t; dims=:) = $_disk_reverse(a, dims)
Base.reverse(a::$t{<:Any,1}) = $_disk_reverse1(a)
Expand Down
23 changes: 19 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -770,15 +770,30 @@ end
@test Array(a_disk) == a
@testset "copyto" begin
x = zero(a)
copyto!(x, a_disk)
@test copyto!(x, a_disk) === x
@test x == a
copyto!(x, CartesianIndices((1:3, 1:2)), a_disk, CartesianIndices((8:10, 8:9)))
@test copyto!(x, CartesianIndices((1:3, 1:2)), a_disk, CartesianIndices((8:10, 8:9))) === x
# Test copyto! with zero length index
x_empty = Matrix{Int64}(undef, 0, 2)
copyto!(x_empty, CartesianIndices((1:0, 1:2)), a_disk, CartesianIndices((8:7, 8:9)))
# copyto! with different length should throw an error
@test_throws ArgumentError copyto!(x, CartesianIndices((1:1, 1:2)), a_disk, CartesianIndices((4:6, 8:9)))

# 5 arg copyto!
a_vec = collect(0x00:0x90)
test_dests = [
zero(a_vec),
view(zero(a_vec), 1:10),
]
for x in test_dests
local a_disk = AccessCountDiskArray(a_vec; chunksize=(15,))
x = zero(a_vec)
@test_throws ArgumentError copyto!(x, 1, a_disk, 1, -1)
@test copyto!(x, 1, a_disk, 1, 0) === x
@test copyto!(x, 6, a_disk, 3, 2) === x
@test x[6] == a_vec[3]
@test x[7] == a_vec[4]
@test getindex_count(a_disk) == 1
end
end

@test collect(reverse(a_disk)) == reverse(a)
Expand All @@ -804,7 +819,7 @@ end
@test vcat(a_disk, a_disk) == vcat(a, a)
@test hcat(a_disk, a_disk) == hcat(a, a)
@test cat(a_disk, a_disk; dims=3) == cat(a, a; dims=3)
@test_broken circshift(a_disk, 2) == circshift(a, 2) # This one is super weird. The size changes.
@test circshift(a_disk, 2) == circshift(a, 2)
end

@testset "Reshape" begin
Expand Down
Loading