Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/vector_of_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,12 @@ function Base.similar(vec::VectorOfArray{
return VectorOfArray(similar.(Base.parent(vec)))
end

function Base.similar(vec::VectorOfArray{
T, N, AT}) where {T, N, AT <: AbstractArray{<:StaticArraysCore.StaticVecOrMat{T}}}
# this avoids behavior such as similar(SVector) returning an MVector
return VectorOfArray(similar(Base.parent(vec)))
end

@inline function Base.similar(VA::VectorOfArray, ::Type{T} = eltype(VA)) where {T}
VectorOfArray(similar.(VA.u, T))
end
Expand Down
12 changes: 11 additions & 1 deletion test/copy_static_array_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,14 @@ x = StructArray{SVector{2, Float64}}((randn(2), randn(2)))
vx = VectorOfArray(x)
vx2 = copy(vx) .+ 1
ans = vx .+ vx2
@test ans.u isa StructArray
@test ans.u isa StructArray

# check that Base.similar(VectorOfArray{<:StaticArray}) returns the
# same type as the original VectorOfArray
x_staticvector = [SVector(0.0, 0.0) for _ in 1:2]
x_structarray = StructArray{SVector{2, Float64}}((randn(2), randn(2)))
x_mutablefv = [MutableFV(1.0, 2.0)]
x_immutablefv = [ImmutableFV(1.0, 2.0)]
for vec in [x_staticvector, x_structarray, x_mutablefv, x_immutablefv]
@test typeof(similar(VectorOfArray(vec))) === typeof(VectorOfArray(vec))
end
Loading