diff --git a/src/Val.cc b/src/Val.cc index 94657cdbde..5f39fbba9a 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -3269,8 +3269,11 @@ bool VectorVal::Assign(unsigned int index, ValPtr element) if ( ! CheckElementType(element) ) return false; - if ( index >= vector_val->size() ) + auto n = vector_val->size(); + + if ( index >= n ) { + AddHoles(index - n); vector_val->resize(index + 1); if ( yield_types ) yield_types->resize(index + 1); @@ -3332,14 +3335,7 @@ bool VectorVal::Insert(unsigned int index, ValPtr element) it = vector_val->end(); if ( yield_types ) types_it = yield_types->end(); - - // Initialize any holes the assignment induces in the vector. - TypePtr fill_t = yield_type; - if ( yield_type->Tag() == TYPE_VOID ) - fill_t = base_type(TYPE_ANY); - - for ( auto i = n; i < index; ++i ) - vector_val->emplace_back(ZVal(fill_t)); + AddHoles(index - n); } if ( yield_types ) @@ -3355,6 +3351,16 @@ bool VectorVal::Insert(unsigned int index, ValPtr element) return true; } +void VectorVal::AddHoles(int nholes) + { + TypePtr fill_t = yield_type; + if ( yield_type->Tag() == TYPE_VOID ) + fill_t = base_type(TYPE_ANY); + + for ( auto i = 0; i < nholes; ++i ) + vector_val->emplace_back(ZVal(fill_t)); + } + bool VectorVal::Remove(unsigned int index) { if ( index >= vector_val->size() ) diff --git a/src/Val.h b/src/Val.h index d5665d1dca..70961d0f59 100644 --- a/src/Val.h +++ b/src/Val.h @@ -1571,6 +1571,9 @@ private: // element type-checked. bool CheckElementType(const ValPtr& element); + // Add the given number of "holes" to the end of a vector. + void AddHoles(int nholes); + std::vector* vector_val; // For homogeneous vectors (the usual case), the type of the