Skip to content

Commit dd5c756

Browse files
Merge pull request #308 from SciML/recursivecopy_simplification
Simplify and better test recursivecopy!
2 parents 4dc8305 + 777311c commit dd5c756

File tree

3 files changed

+42
-26
lines changed

3 files changed

+42
-26
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "RecursiveArrayTools"
22
uuid = "731186ca-8d62-57ce-b412-fbd966d074cd"
33
authors = ["Chris Rackauckas <[email protected]>"]
4-
version = "3.2.1"
4+
version = "3.2.2"
55

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

src/utils.jl

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,39 +42,47 @@ like `copy!` on arrays of scalars.
4242
"""
4343
function recursivecopy! end
4444

45-
for type in [AbstractArray, AbstractVectorOfArray]
46-
@eval function recursivecopy!(b::$type{T, N},
47-
a::$type{T2, N}) where {T <: StaticArraysCore.StaticArray,
48-
T2 <: StaticArraysCore.StaticArray,
49-
N}
50-
@inbounds for i in eachindex(a)
51-
# TODO: Check for `setindex!`` and use `copy!(b[i],a[i])` or `b[i] = a[i]`, see #19
52-
b[i] = copy(a[i])
53-
end
45+
function recursivecopy!(b::AbstractArray{T, N}, a::AbstractArray{T2, N}) where {T <: StaticArraysCore.StaticArray,
46+
T2 <: StaticArraysCore.StaticArray,
47+
N}
48+
@inbounds for i in eachindex(a)
49+
# TODO: Check for `setindex!`` and use `copy!(b[i],a[i])` or `b[i] = a[i]`, see #19
50+
b[i] = copy(a[i])
5451
end
52+
end
5553

56-
@eval function recursivecopy!(b::$type{T, N},
57-
a::$type{T2, N}) where {T <: Enum, T2 <: Enum, N}
58-
copyto!(b, a)
59-
end
54+
function recursivecopy!(b::AbstractArray{T, N},
55+
a::AbstractArray{T2, N}) where {T <: Enum, T2 <: Enum, N}
56+
copyto!(b, a)
57+
end
58+
59+
function recursivecopy!(b::AbstractArray{T, N},
60+
a::AbstractArray{T2, N}) where {T <: Number, T2 <: Number, N}
61+
copyto!(b, a)
62+
end
6063

61-
@eval function recursivecopy!(b::$type{T, N},
62-
a::$type{T2, N}) where {T <: Number, T2 <: Number, N}
64+
function recursivecopy!(b::AbstractArray{T, N},
65+
a::AbstractArray{T2, N}) where {T <: Union{AbstractArray, AbstractVectorOfArray},
66+
T2 <: Union{AbstractArray, AbstractVectorOfArray}, N}
67+
if ArrayInterface.ismutable(T)
68+
@inbounds for i in eachindex(b, a)
69+
recursivecopy!(b[i], a[i])
70+
end
71+
else
6372
copyto!(b, a)
6473
end
74+
return b
75+
end
6576

66-
@eval function recursivecopy!(b::$type{T, N},
67-
a::$type{T2, N}) where {T <: Union{AbstractArray, AbstractVectorOfArray},
68-
T2 <: Union{AbstractArray, AbstractVectorOfArray}, N}
69-
if ArrayInterface.ismutable(T)
70-
@inbounds for i in eachindex(b, a)
71-
recursivecopy!(b[i], a[i])
72-
end
73-
else
74-
copyto!(b, a)
77+
function recursivecopy!(b::AbstractVectorOfArray, a::AbstractVectorOfArray)
78+
if ArrayInterface.ismutable(eltype(b.u))
79+
@inbounds for i in eachindex(b.u, a.u)
80+
recursivecopy!(b.u[i], a.u[i])
7581
end
76-
return b
82+
else
83+
copyto!(b.u, a.u)
7784
end
85+
return b
7886
end
7987

8088
"""

test/utils_test.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,12 @@ end
122122
@test u1.u[2] == [2.0,2.0]
123123
@test u1.u[1] isa MVector
124124
@test u1.u[2] isa MVector
125+
126+
u1 = VectorOfArray([fill(2, SVector{2, Float64}), ones(SVector{2, Float64})])
127+
u2 = VectorOfArray([fill(4, SVector{2, Float64}), 2 .* ones(SVector{2, Float64})])
128+
recursivecopy!(u1,u2)
129+
@test u1.u[1] == [4.0,4.0]
130+
@test u1.u[2] == [2.0,2.0]
131+
@test u1.u[1] isa SVector
132+
@test u1.u[2] isa SVector
125133
end

0 commit comments

Comments
 (0)