[+] AuTryReserve

This commit is contained in:
Reece Wilson 2022-10-06 20:18:05 +01:00
parent b2b2b1760a
commit a81e51c0fb

View File

@ -372,22 +372,42 @@ inline bool AuTryInsert(Container &container, const Key_t &key, Type_t &&value,
}
template <class Container>
inline bool AuContainerExpandOne(Container &container)
inline bool AuTryReserve(Container &container, AuUInt uRequired)
{
auto required = container.size() + 1;
if (container.capacity() >= required)
if (container.capacity() >= uRequired)
{
return true;
}
container.reserve(required);
return container.capacity() >= required;
AUROXTL_COMMODITY_TRY
{
container.reserve(uRequired);
}
AUROXTL_COMMODITY_CATCH
{
return false;
}
return container.capacity() >= uRequired;
}
template <class Container>
inline bool AuContainerExpandOne(Container *container)
inline bool AuContainerReserveSpaceForOne(Container &container)
{
return AuContainerExpandOne(*container);
auto uCapacity = container.size();
if (container.capacity() >= (uCapacity + 1))
{
return true;
}
return AuTryReserve(container, uCapacity > 512 ? 512 : uCapacity + 32);
}
template <class Container>
inline bool AuContainerReserveSpaceForOne(Container *container)
{
return AuContainerReserveSpaceForOne(*container);
}
template <class Container, typename Value, AU_TEMPLATE_ENABLE_WHEN(!__audetail::AuHasend_v<Container> && !AuIsPointer_v<Container>)>
@ -397,7 +417,7 @@ inline bool AuTryInsert(Container &container, const Value &value)
{
if constexpr (__audetail::AuIsPreallocatable_v<AuRemoveReference_t<Container>>)
{
if (!AuContainerExpandOne(container))
if (!AuContainerReserveSpaceForOne(container))
{
return false;
}
@ -420,7 +440,7 @@ inline bool AuTryInsert(Container &container, Value &&value)
{
if constexpr (__audetail::AuIsPreallocatable_v<AuRemoveReference_t<Container>>)
{
if (!AuContainerExpandOne(container))
if (!AuContainerReserveSpaceForOne(container))
{
return false;
}
@ -443,7 +463,7 @@ inline bool AuTryInsert(Container &container, Value &&value)
{
if constexpr (__audetail::AuIsPreallocatable_v<AuRemoveReference_t<Container>>)
{
if (!AuContainerExpandOne(container))
if (!AuContainerReserveSpaceForOne(container))
{
return false;
}
@ -466,7 +486,7 @@ inline bool AuTryInsert(Container &container, const Value &value)
{
if constexpr (__audetail::AuIsPreallocatable_v<AuRemoveReference_t<Container>>)
{
if (!AuContainerExpandOne(container))
if (!AuContainerReserveSpaceForOne(container))
{
return false;
}