[vector] Add emplacing push implementation

This commit is contained in:
Behdad Esfahbod 2022-05-19 17:34:58 -06:00
parent 25393288f0
commit 30ba9a39e2
1 changed files with 21 additions and 3 deletions

View File

@ -189,12 +189,14 @@ struct hb_vector_t : std::conditional<sorted, hb_vector_t<Type, false>, hb_empty
{
if (unlikely (!resize (length + 1)))
return &Crap (Type);
return &arrayZ[length - 1];
return std::addressof (arrayZ[length - 1]);
}
template <typename T>
template <typename T,
typename T2 = Type,
hb_enable_if (!std::is_copy_constructible<T2>::value &&
std::is_copy_assignable<T>::value)>
Type *push (T&& v)
{
/* TODO Emplace? */
Type *p = push ();
if (p == &Crap (Type))
// If push failed to allocate then don't copy v, since this may cause
@ -204,6 +206,22 @@ struct hb_vector_t : std::conditional<sorted, hb_vector_t<Type, false>, hb_empty
*p = std::forward<T> (v);
return p;
}
template <typename T,
typename T2 = Type,
hb_enable_if (std::is_copy_constructible<T2>::value)>
Type *push (T&& v)
{
if (unlikely (!alloc (length + 1)))
// If push failed to allocate then don't copy v, since this may cause
// the created copy to leak memory since we won't have stored a
// reference to it.
return &Crap (Type);
/* Emplace. */
length++;
Type *p = std::addressof (arrayZ[length - 1]);
return new (p) Type (std::forward<T> (v));
}
bool in_error () const { return allocated < 0; }