Skip to content

Commit 61786d0

Browse files
authored
Define copyto (#16)
1 parent b2fea1d commit 61786d0

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Derive"
22
uuid = "a07dfc7f-7d04-4eb5-84cc-a97f051f655a"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.3.2"
4+
version = "0.3.3"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/abstractarrayinterface.jl

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ using BroadcastMapConversion: map_function, map_args
6969
@interface interface::AbstractArrayInterface function Base.copyto!(
7070
dest::AbstractArray, bc::Broadcast.Broadcasted
7171
)
72-
@interface interface map!(map_function(bc), dest, map_args(bc)...)
73-
return dest
72+
return @interface interface map!(map_function(bc), dest, map_args(bc)...)
7473
end
7574

7675
# This is defined in this way so we can rely on the Broadcast logic
@@ -121,12 +120,22 @@ end
121120
return @interface interface all(isreal, a)
122121
end
123122

124-
@interface ::AbstractArrayInterface function Base.permutedims!(
123+
@interface interface::AbstractArrayInterface function Base.permutedims!(
125124
a_dest::AbstractArray, a_src::AbstractArray, perm
126125
)
127-
# TODO: Should this be `@interface interface ...`?
128-
a_dest .= PermutedDimsArray(a_src, perm)
129-
return a_dest
126+
return @interface interface map!(identity, a_dest, PermutedDimsArray(a_src, perm))
127+
end
128+
129+
@interface interface::AbstractArrayInterface function Base.copyto!(
130+
a_dest::AbstractArray, a_src::AbstractArray
131+
)
132+
return @interface interface map!(identity, a_dest, a_src)
133+
end
134+
135+
@interface interface::AbstractArrayInterface function Base.copy!(
136+
a_dest::AbstractArray, a_src::AbstractArray
137+
)
138+
return @interface interface map!(identity, a_dest, a_src)
130139
end
131140

132141
using LinearAlgebra: LinearAlgebra

src/traits.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ function derive(::Val{:AbstractArrayOps}, type)
1717
Base.similar(::$type, ::Type, ::Tuple{Vararg{Int}})
1818
Base.similar(::$type, ::Type, ::Tuple{Base.OneTo,Vararg{Base.OneTo}})
1919
Base.copy(::$type)
20+
Base.copy!(::AbstractArray, ::$type)
21+
Base.copyto!(::AbstractArray, ::$type)
2022
Base.map(::Any, ::$type...)
2123
Base.map!(::Any, ::AbstractArray, ::$type...)
2224
Base.mapreduce(::Any, ::Any, ::$type...; kwargs...)

test/basics/test_basics.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,12 @@ elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
8181
@test isreal(a)
8282
@test sum(a) == 33
8383
@test mapreduce(x -> 2x, +, a) == 66
84+
85+
a = SparseArrayDOK{elt}(2, 2)
86+
a[1, 2] = 12
87+
b = similar(a)
88+
copyto!(b, a)
89+
@test b == a
90+
@test b[1, 2] == 12
91+
@test storedlength(b) == 1
8492
end

0 commit comments

Comments
 (0)