Skip to content

Commit 97b3bb7

Browse files
Merge pull request #21 from ranocha/pull-request/9065539d
fix copyat_or_push! for StaticArrays
2 parents a70c164 + 4999bca commit 97b3bb7

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/utils.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
"""
2+
is_mutable_type(x::DataType)
3+
4+
Query whether a type is mutable or not, see
5+
https://github.com/JuliaDiffEq/RecursiveArrayTools.jl/issues/19.
6+
"""
7+
Base.@pure is_mutable_type(x::DataType) = x.mutable
8+
9+
110
function recursivecopy{T<:Number,N}(a::AbstractArray{T,N})
211
copy(a)
312
end
@@ -52,7 +61,9 @@ end
5261

5362
@inline function copyat_or_push!{T,perform_copy}(a::AbstractVector{T},i::Int,x,nc::Type{Val{perform_copy}}=Val{true})
5463
@inbounds if length(a) >= i
55-
if T <: Number || !perform_copy
64+
if T <: Number || T <: SArray || (T <: FieldVector && !is_mutable_type(T)) || !perform_copy
65+
# TODO: Check for `setindex!`` if T <: StaticArray and use `copy!(b[i],a[i])`
66+
# or `b[i] = a[i]`, see https://github.com/JuliaDiffEq/RecursiveArrayTools.jl/issues/19
5667
a[i] = x
5768
else
5869
recursivecopy!(a[i],x)

test/copy_static_array_test.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ a = [vec]
1616
b = zeros(a)
1717
recursivecopy!(b, a)
1818
@test a[1] == b[1]
19+
copyat_or_push!(a, 2, b[1])
20+
@test a[2] == b[1]
21+
b[1] = 2*b[1]
22+
copyat_or_push!(a, 2, b[1])
23+
@test a[2] == b[1]
1924

2025
# Mutable FieldVector
2126
vec = MutableFV(1.,2.)
@@ -25,13 +30,25 @@ recursivecopy!(b, a)
2530
@test a[1] == b[1]
2631
a[1][1] *= 5
2732
@test a[1] != b[1]
33+
copyat_or_push!(a, 2, b[1])
34+
@test a[2] == b[1]
35+
a[2][1] *= 5
36+
@test a[2] != b[1]
37+
b[1] = 2*b[1]
38+
copyat_or_push!(a, 2, b[1])
39+
@test a[2] == b[1]
2840

2941
# SArray
3042
vec = @SArray [1., 2.]
3143
a = [vec]
3244
b = zeros(a)
3345
recursivecopy!(b, a)
3446
@test a[1] == b[1]
47+
copyat_or_push!(a, 2, b[1])
48+
@test a[2] == b[1]
49+
b[1] = 2*b[1]
50+
copyat_or_push!(a, 2, b[1])
51+
@test a[2] == b[1]
3552

3653
# MArray
3754
vec = @MArray [1., 2.]
@@ -40,3 +57,10 @@ b = zeros(a)
4057
recursivecopy!(b, a)
4158
a[1][1] *= 5
4259
@test a[1] != b[1]
60+
copyat_or_push!(a, 2, b[1])
61+
@test a[2] == b[1]
62+
a[2][1] *= 5
63+
@test a[2] != b[1]
64+
b[1] = 2*b[1]
65+
copyat_or_push!(a, 2, b[1])
66+
@test a[2] == b[1]

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ tic()
77
@time @testset "Partitions Tests" begin include("partitions_test.jl") end
88
@time @testset "VecOfArr Indexing Tests" begin include("basic_indexing.jl") end
99
@time @testset "VecOfArr Interface Tests" begin include("interface_tests.jl") end
10-
@time @testset "StaticArrays (recursivecopy!) Tests" begin include("copy_static_array_test.jl") end
10+
@time @testset "StaticArrays Tests" begin include("copy_static_array_test.jl") end
1111
toc()
1212
# Test the VectorOfArray code

0 commit comments

Comments
 (0)