[vector] Construct items when enlarging

This commit is contained in:
Behdad Esfahbod 2022-01-15 17:16:40 -07:00
parent 813eaba6ea
commit 7171917b52
1 changed files with 21 additions and 2 deletions

View File

@ -232,6 +232,26 @@ struct hb_vector_t
return new_array;
}
template <typename T = Type,
hb_enable_if (std::is_trivially_copy_assignable<T>::value)>
void
grow_vector (unsigned size)
{
memset (arrayZ + length, 0, (size - length) * sizeof (*arrayZ));
length = size;
}
template <typename T = Type,
hb_enable_if (!std::is_trivially_copy_assignable<T>::value)>
void
grow_vector (unsigned size)
{
while (length < size)
{
length++;
new (std::addressof (arrayZ[length - 1])) Type ();
}
}
template <typename T = Type,
hb_enable_if (std::is_trivially_copy_assignable<T>::value)>
void
@ -294,8 +314,7 @@ struct hb_vector_t
return false;
if (size > length)
// XXX construct objects.
memset (arrayZ + length, 0, (size - length) * sizeof (*arrayZ));
grow_vector (size);
else if (size < length)
shrink_vector (size);