Skip to content

Commit 2973fc6

Browse files
committed
restore Base.similar behavior
1 parent fd61439 commit 2973fc6

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/vector_of_array.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,9 +712,22 @@ Base.eltype(::Type{<:AbstractVectorOfArray{T}}) where {T} = T
712712
end
713713
end
714714
@inline function Base.similar(VA::VectorOfArray, ::Type{T} = eltype(VA)) where {T}
715-
VectorOfArray(similar.(Base.parent(VA), T))
715+
VectorOfArray([similar(VA[:, i], T) for i in eachindex(VA.u)])
716716
end
717717

718+
# for VectorOfArray with multi-dimensional parent arrays of arrays where all elements are the same type
719+
function Base.similar(vec::VectorOfArray{
720+
T, N, AT}) where {T, N, AT <: AbstractArray{<:AbstractArray{T}}}
721+
return VectorOfArray(similar.(Base.parent(vec)))
722+
end
723+
724+
# special-case when the multi-dimensional parent array is just an AbstractVector (call the old method)
725+
function Base.similar(vec::VectorOfArray{
726+
T, N, AT}) where {T, N, AT <: AbstractVector{<:AbstractArray{T}}}
727+
return Base.similar(vec, eltype(vec))
728+
end
729+
730+
718731
# fill!
719732
# For DiffEqArray it ignores ts and fills only u
720733
function Base.fill!(VA::AbstractVectorOfArray, x)

0 commit comments

Comments
 (0)