Skip to content

Commit 8f0b906

Browse files
committed
Implement resize! for VectorOfArrays, can only shrink, not grow
1 parent aaa3d44 commit 8f0b906

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/vector_of_arrays.jl

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ differ in size). Internally, `VectorOfArrays` stores all elements of all
99
arrays in a single flat vector. `M` must equal `N - 1`
1010
1111
The `VectorOfArrays` itself supports `push!`, `unshift!`, etc., but the size
12-
of each individual array in the vector is fixed. `resize!` is not supported,
13-
as the size of all arrays in the vector must be defined. However, memory space
14-
for up to `n` arrays with a maximum size `s` can be reserved via
15-
`sizehint!(A::VectorOfArrays, n, s::Dims{N})`
12+
of each individual array in the vector is fixed. `resize!` can be used to
13+
shrink, but not to grow, as the size of the additional element arrays in the
14+
vector would be unknown. However, memory space for up to `n` arrays with a
15+
maximum size `s` can be reserved via
16+
`sizehint!(A::VectorOfArrays, n, s::Dims{N})`.
1617
1718
Constructors:
1819
@@ -218,6 +219,19 @@ end
218219
Base.length(A::VectorOfArrays) = length(A.kernel_size)
219220

220221

222+
@inline function Base.resize!(A::VectorOfArrays{T,N,M}, n::Integer) where {T,M,N}
223+
old_n = length(A)
224+
if n > old_n
225+
throw(ArgumentError("Cannot resize VectorOfArrays from length $old_n to $n, can only shrink, not grow"))
226+
elseif n < old_n
227+
resize!(A.data, A.elem_ptr[n+1] - 1)
228+
resize!(A.elem_ptr, n + 1)
229+
resize!(A.kernel_size, n)
230+
end
231+
A
232+
end
233+
234+
221235
function Base.append!(A::VectorOfArrays{T,N}, B::VectorOfArrays{U,N}) where {T,N,U}
222236
if !isempty(B)
223237
# Implementation supports A === B

0 commit comments

Comments
 (0)