Skip to content

Commit a30d7d0

Browse files
feat: add ability to set VectorOfArray with Array using broadcast
1 parent dd5c756 commit a30d7d0

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/vector_of_array.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,20 @@ end
663663
bc = Broadcast.flatten(bc)
664664
N = narrays(bc)
665665
@inbounds for i in 1:N
666-
if dest[:, i] isa AbstractArray && !isa(dest[:, i], StaticArraysCore.SArray)
666+
if dest[:, i] isa AbstractArray && ArrayInterface.ismutable(dest[:, i])
667+
copyto!(dest[:, i], unpack_voa(bc, i))
668+
else
669+
dest[:, i] = copy(unpack_voa(bc, i))
670+
end
671+
end
672+
dest
673+
end
674+
675+
@inline function Base.copyto!(dest::AbstractVectorOfArray,
676+
bc::Broadcast.Broadcasted{<:Broadcast.DefaultArrayStyle})
677+
bc = Broadcast.flatten(bc)
678+
@inbounds for i in 1:length(dest.u)
679+
if dest[:, i] isa AbstractArray && ArrayInterface.ismutable(dest[:, i])
667680
copyto!(dest[:, i], unpack_voa(bc, i))
668681
else
669682
dest[:, i] = copy(unpack_voa(bc, i))

test/interface_tests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,11 @@ z = VectorOfArray([zeros(SVector{2, Float64}), zeros(SVector{2, Float64})])
125125
z .= x .+ y
126126

127127
@test z == VectorOfArray([fill(4, SVector{2, Float64}), fill(2, SVector{2, Float64})])
128+
129+
yy = [2.0 1.0; 2.0 1.0]
130+
zz = x .+ yy
131+
@test zz == [4.0, 2.0; 4.0, 2.0]
132+
133+
z = VectorOfArray([zeros(SVector{2, Float64}), zeros(SVector{2, Float64})])
134+
z .= zz
135+
@test z == VectorOfArray([fill(4, SVector{2, Float64}), fill(2, SVector{2, Float64})])

0 commit comments

Comments
 (0)