Skip to content
This repository has been archived by the owner on Jan 20, 2025. It is now read-only.

Commit

Permalink
Define copyto (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Dec 12, 2024
1 parent b2fea1d commit 61786d0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Derive"
uuid = "a07dfc7f-7d04-4eb5-84cc-a97f051f655a"
authors = ["ITensor developers <support@itensor.org> and contributors"]
version = "0.3.2"
version = "0.3.3"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
21 changes: 15 additions & 6 deletions src/abstractarrayinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ using BroadcastMapConversion: map_function, map_args
@interface interface::AbstractArrayInterface function Base.copyto!(
dest::AbstractArray, bc::Broadcast.Broadcasted
)
@interface interface map!(map_function(bc), dest, map_args(bc)...)
return dest
return @interface interface map!(map_function(bc), dest, map_args(bc)...)
end

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

@interface ::AbstractArrayInterface function Base.permutedims!(
@interface interface::AbstractArrayInterface function Base.permutedims!(
a_dest::AbstractArray, a_src::AbstractArray, perm
)
# TODO: Should this be `@interface interface ...`?
a_dest .= PermutedDimsArray(a_src, perm)
return a_dest
return @interface interface map!(identity, a_dest, PermutedDimsArray(a_src, perm))
end

@interface interface::AbstractArrayInterface function Base.copyto!(
a_dest::AbstractArray, a_src::AbstractArray
)
return @interface interface map!(identity, a_dest, a_src)
end

@interface interface::AbstractArrayInterface function Base.copy!(
a_dest::AbstractArray, a_src::AbstractArray
)
return @interface interface map!(identity, a_dest, a_src)
end

using LinearAlgebra: LinearAlgebra
Expand Down
2 changes: 2 additions & 0 deletions src/traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ function derive(::Val{:AbstractArrayOps}, type)
Base.similar(::$type, ::Type, ::Tuple{Vararg{Int}})
Base.similar(::$type, ::Type, ::Tuple{Base.OneTo,Vararg{Base.OneTo}})
Base.copy(::$type)
Base.copy!(::AbstractArray, ::$type)
Base.copyto!(::AbstractArray, ::$type)
Base.map(::Any, ::$type...)
Base.map!(::Any, ::AbstractArray, ::$type...)
Base.mapreduce(::Any, ::Any, ::$type...; kwargs...)
Expand Down
8 changes: 8 additions & 0 deletions test/basics/test_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,12 @@ elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
@test isreal(a)
@test sum(a) == 33
@test mapreduce(x -> 2x, +, a) == 66

a = SparseArrayDOK{elt}(2, 2)
a[1, 2] = 12
b = similar(a)
copyto!(b, a)
@test b == a
@test b[1, 2] == 12
@test storedlength(b) == 1
end

0 comments on commit 61786d0

Please sign in to comment.