diff --git a/src/array.jl b/src/array.jl index d6f5146..ddf674b 100644 --- a/src/array.jl +++ b/src/array.jl @@ -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 @@ -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} + 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) diff --git a/test/runtests.jl b/test/runtests.jl index 717e2fb..bf3bf25 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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) @@ -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