@@ -243,6 +243,11 @@ def __init__(
243243 )
244244 elif self .flattened_data is None :
245245 self .flattened_data = flattened_data
246+ expected_fd_size = self .cumulative_length [- 1 ] if len (self ) > 0 else 0
247+ if len (flattened_data ) != expected_fd_size :
248+ msg = "provided flattened_data array has different size than indicated by cumulative_length. Resizing..."
249+ log .warning (msg )
250+ self .flattened_data .resize (expected_fd_size )
246251
247252 super ().__init__ (attrs )
248253
@@ -474,8 +479,8 @@ def _set_vector_unsafe(
474479 `vec` in ``self.flattened_data[j:sum(lens)]``. Finally updates
475480 ``self.cumulative_length[i]`` with the new flattened data array length.
476481
477- Vectors stored after index `i` can be overridden, producing unintended
478- behavior . This method is typically used for fast sequential fill of a
482+ Vectors stored after index `i` are removed and the VectorOfVectors is
483+ resized . This method is typically used for fast sequential fill of a
479484 pre-allocated vector of vectors.
480485
481486 If i`vec` is 1D array and `lens` is ``None``, set using full array. If
@@ -484,8 +489,9 @@ def _set_vector_unsafe(
484489
485490 Danger
486491 ------
487- This method can lead to undefined behavior or vector invalidation if
488- used improperly. Use it only if you know what you are doing.
492+ This method resizes the array, removes subsequent vectors, and can lead
493+ to undefined behavior or vector-view invalidation if used improperly.
494+ Use it only if you know what you are doing.
489495
490496 See Also
491497 --------
@@ -518,6 +524,9 @@ def _set_vector_unsafe(
518524 nan_val = np .nan
519525 from .vovutils import _nb_fill # noqa: PLC0415
520526
527+ self .flattened_data .resize (cum_lens [- 1 ])
528+ self .cumulative_length .resize (i + len (lens ))
529+
521530 _nb_fill (
522531 vec ,
523532 lens ,
0 commit comments