Skip to content

Commit 82e76d7

Browse files
Merge pull request #24 from Gregstrq/master
Base.similar and Base.fill! for VectorOfArray
2 parents 7076632 + 2b51e08 commit 82e76d7

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/vector_of_array.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ function Base.append!{T, N}(VA::AbstractVectorOfArray{T, N}, new_item::AbstractV
6262
return VA
6363
end
6464

65+
# Tools for creating similar objects
66+
@inline Base.similar(VA::VectorOfArray, ::Type{T} = eltype(VA)) where {T} = VectorOfArray([similar(VA[i], T) for i in eachindex(VA)])
67+
68+
# fill!
69+
# For DiffEqArray it ignores ts and fills only u
70+
function Base.fill!(VA::AbstractVectorOfArray, x)
71+
for i in eachindex(VA)
72+
fill!(VA[i], x)
73+
end
74+
return VA
75+
end
76+
77+
# Need this for ODE_DEFAULT_UNSTABLE_CHECK from DiffEqBase to work properly
78+
@inline Base.any(f, VA::AbstractVectorOfArray) = any(any(f,VA[i]) for i in eachindex(VA))
79+
@inline Base.all(f, VA::AbstractVectorOfArray) = all(all(f,VA[i]) for i in eachindex(VA))
80+
6581
# conversion tools
6682
@deprecate vecarr_to_arr(VA::AbstractVectorOfArray) convert(Array,VA)
6783
@deprecate vecarr_to_arr{T<:AbstractArray}(VA::Vector{T}) convert(Array,VA)

test/interface_tests.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,29 @@ testa = cat(3, recs...)
4242
recs = [[1, 2, 3], [3 5; 6 7], [8, 9, 10, 11]]
4343
testva = VectorOfArray(recs)
4444
@test size(convert(Array,testva)) == (3,3)
45+
46+
# create similar VectorOfArray
47+
recs = [rand(6) for i = 1:4]
48+
testva = VectorOfArray(recs)
49+
testva2 = similar(testva)
50+
@test typeof(testva2) == typeof(testva)
51+
@test size(testva2) == size(testva)
52+
53+
# Fill AbstractVectorOfArray and check all
54+
testval = 3.0
55+
fill!(testva2, testval)
56+
@test all(x->(x==testval), testva2)
57+
testts = rand(size(testva.u))
58+
testda = DiffEqArray(recursivecopy(testva.u), testts)
59+
fill!(testda, testval)
60+
@test all(x->(x==testval), testda)
61+
62+
# check any
63+
recs = [collect(1:5), collect(6:10), collect(11:15)]
64+
testts = rand(5)
65+
testva = VectorOfArray(recs)
66+
testda = DiffEqArray(recs, testts)
67+
testval1 = 4
68+
testval2 = 17
69+
@test any(x->(x==testval1), testva)
70+
@test !any(x->(x==testval2), testda)

0 commit comments

Comments
 (0)