Skip to content

Commit 877fd67

Browse files
authored
Add 5 arg copyto! into Vector with matching type (#264)
* Add 5 arg `copyto!` into `Vector` * add support for views and different types * remove copyto! with different types * remove matching eltype restriction
1 parent 4210d28 commit 877fd67

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/array.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ function _disk_copyto!(dest, Rdest, src, Rsrc)
2121
return dest
2222
end
2323
view(dest, Rdest) .= view(src, Rsrc)
24+
return dest
25+
end
26+
function _disk_copyto_5arg!(dest, dstart, src, sstart, n)
27+
if iszero(n)
28+
return dest
29+
end
30+
if n < 0
31+
throw(ArgumentError(LazyString("tried to copy n=",
32+
n," elements, but n should be non-negative")))
33+
end
34+
destv = view(dest, range(dstart, length=n))
35+
DiskArrays.readblock!(src, destv, range(sstart, length=n))
36+
return dest
2437
end
2538

2639
# Use a view for lazy reverse
@@ -76,6 +89,12 @@ macro implement_array_methods(t)
7689
function Base.copyto!(dest::PermutedDimsArray{T,N}, src::$t{T,N}) where {T,N}
7790
return $_disk_copyto!(dest, src)
7891
end
92+
function Base.copyto!(dest::Vector, dstart::Integer, src::$t{<:Any, 1}, sstart::Integer, n::Integer)
93+
return $_disk_copyto_5arg!(dest, dstart, src, sstart, n)
94+
end
95+
function Base.copyto!(dest::SubArray{T, 1, Vector{T}, <:Tuple{AbstractUnitRange}, true} where {T}, dstart::Integer, src::$t{<:Any, 1}, sstart::Integer, n::Integer)
96+
return $_disk_copyto_5arg!(dest, dstart, src, sstart, n)
97+
end
7998

8099
Base.reverse(a::$t; dims=:) = $_disk_reverse(a, dims)
81100
Base.reverse(a::$t{<:Any,1}) = $_disk_reverse1(a)

test/runtests.jl

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -770,15 +770,31 @@ end
770770
@test Array(a_disk) == a
771771
@testset "copyto" begin
772772
x = zero(a)
773-
copyto!(x, a_disk)
773+
@test copyto!(x, a_disk) === x
774774
@test x == a
775-
copyto!(x, CartesianIndices((1:3, 1:2)), a_disk, CartesianIndices((8:10, 8:9)))
775+
@test copyto!(x, CartesianIndices((1:3, 1:2)), a_disk, CartesianIndices((8:10, 8:9))) === x
776776
# Test copyto! with zero length index
777777
x_empty = Matrix{Int64}(undef, 0, 2)
778778
copyto!(x_empty, CartesianIndices((1:0, 1:2)), a_disk, CartesianIndices((8:7, 8:9)))
779779
# copyto! with different length should throw an error
780780
@test_throws ArgumentError copyto!(x, CartesianIndices((1:1, 1:2)), a_disk, CartesianIndices((4:6, 8:9)))
781-
781+
# 5 arg copyto!
782+
a_vec = collect(0x00:0x90)
783+
test_dests = [
784+
zero(a_vec),
785+
view(zero(a_vec), 1:10),
786+
zeros(length(a_vec)), # This tests type conversion
787+
]
788+
for x in test_dests
789+
local a_disk = AccessCountDiskArray(a_vec; chunksize=(15,))
790+
x = zero(a_vec)
791+
@test_throws ArgumentError copyto!(x, 1, a_disk, 1, -1)
792+
@test copyto!(x, 1, a_disk, 1, 0) === x
793+
@test copyto!(x, 6, a_disk, 3, 2) === x
794+
@test x[6] == a_vec[3]
795+
@test x[7] == a_vec[4]
796+
@test getindex_count(a_disk) == 1
797+
end
782798
end
783799

784800
@test collect(reverse(a_disk)) == reverse(a)
@@ -804,7 +820,7 @@ end
804820
@test vcat(a_disk, a_disk) == vcat(a, a)
805821
@test hcat(a_disk, a_disk) == hcat(a, a)
806822
@test cat(a_disk, a_disk; dims=3) == cat(a, a; dims=3)
807-
@test_broken circshift(a_disk, 2) == circshift(a, 2) # This one is super weird. The size changes.
823+
@test circshift(a_disk, 2) == circshift(a, 2)
808824
end
809825

810826
@testset "Reshape" begin

0 commit comments

Comments
 (0)