Skip to content

Commit 8b58d81

Browse files
committed
add parent and specialize Base.similar
1 parent 239bd04 commit 8b58d81

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
@@ -156,13 +156,16 @@ function VectorOfArray(vec::AbstractVector{VT}) where {T, N, VT <: AbstractArray
156156
VectorOfArray{T, N + 1, typeof(vec)}(vec)
157157
end
158158

159-
# allow multi-dimensional arrays as long as they're linearly indexed
159+
# allow multi-dimensional arrays as long as they're linearly indexed.
160+
# currently restricted to arrays whose elements are all the same type
160161
function VectorOfArray(array::AbstractArray{AT}) where {T, N, AT <: AbstractArray{T, N}}
161162
@assert IndexStyle(typeof(array)) isa IndexLinear
162163

163164
return VectorOfArray{T, N + 1, typeof(array)}(array)
164165
end
165166

167+
Base.parent(vec::VectorOfArray) = vec.u
168+
166169
function DiffEqArray(vec::AbstractVector{T},
167170
ts::AbstractVector,
168171
::NTuple{N, Int},
@@ -721,6 +724,16 @@ end
721724
VectorOfArray([similar(VA[:, i], T) for i in eachindex(VA.u)])
722725
end
723726

727+
# for VectorOfArray with multi-dimensional parent arrays of arrays where all elements are the same type
728+
@inline function Base.similar(vec::VectorOfArray{T, N, AT}) where {T, N, AT <: AbstractArray{<:AbstractArray{T}}}
729+
return VectorOfArray(similar(Base.parent(vec)))
730+
end
731+
732+
# special-case when the multi-dimensional parent array is just an AbstractVector (call the old method)
733+
@inline function Base.similar(vec::VectorOfArray{T, N, AT}) where {T, N, AT <: AbstractVector{<:AbstractArray{T}}}
734+
return Base.similar(vec, eltype(vec))
735+
end
736+
724737
# fill!
725738
# For DiffEqArray it ignores ts and fills only u
726739
function Base.fill!(VA::AbstractVectorOfArray, x)

0 commit comments

Comments
 (0)