[+] Attempt to reserve ahead of time when possible [ROXTL]

[*] Update docu
This commit is contained in:
Reece Wilson 2022-03-26 15:02:47 +00:00
parent af39d8858c
commit 21a92faab3
5 changed files with 198 additions and 59 deletions

View File

@ -20,6 +20,7 @@
#include <fmt/format.h>
#endif
#include "auROXTL/AU_Z.hpp"
#include "auROXTL/AU_MACROS.hpp"
#include "auROXTLTypes.hpp"

17
Include/auROXTL/AU_Z.hpp Normal file
View File

@ -0,0 +1,17 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AU_Z.hpp
Date: 2022-3-26
Author: Reece
***/
#pragma once
#if !defined(AUROXTL_NO_TRY)
#define AUROXTL_COMMODITY_TRY try
#define AUROXTL_COMMODITY_CATCH catch (...)
#else
#define AUROXTL_COMMODITY_TRY
#define AUROXTL_COMMODITY_CATCH while (0)
#endif

View File

@ -13,18 +13,21 @@ struct AuAtomicUtils
/**
* @brief Generic bitwise (1 << offset)
* @return original value
* @warning T is bound by platform and compiler constraints
*/
static T Set(T *in, AuUInt8 offset);
/**
* @brief Adds addend to in
* @return updated value
* @warning T is bound by platform and compiler constraints
*/
static T Add(T *in, T addend);
/**
* @brief Subtracts the minuend from in
* @return updated value
* @warning T is bound by platform and compiler constraints
*/
static T Sub(T *in, T minuend);
@ -33,6 +36,7 @@ struct AuAtomicUtils
* @param replace replacement value for in if in matches compare
* @param compare required reference value
* @return original value
* @warning T is bound by platform and compiler constraints
*/
static T CompareExchange(T *in, T replace, T compare);
@ -41,6 +45,7 @@ struct AuAtomicUtils
* @param in
* @param offset Bit index
* @return *in & (1 << offset)
* @warning T is bound by platform and compiler constraints
*/
static bool TestAndSet(T *in, const AuUInt8 offset);
};

View File

@ -14,4 +14,4 @@
#include "auHashUtils.hpp"
template <class T, typename Z, typename LessThan_t = AuHash::less<T>>
using AuBST = AURORA_RUNTIME_AU_BST<T, Z, LessThan_t>;
using AuBST = AURORA_RUNTIME_AU_BST<T, Z, LessThan_t>;

View File

@ -10,6 +10,8 @@
***/
#pragma once
#include <auROXTL/auTupleUtils.hpp>
template <class T, bool all>
inline bool _AuRemoveIfBase(T &in, const AuPredicate<const AuToValueType_t<T> &> &predicate);
@ -28,7 +30,7 @@ inline bool AuRemoveIf(T &in, const AuPredicate<const AuToValueType_t<T> &> &pre
template <class T, typename Z>
inline bool AuTryRemove(T &in, const Z &type)
{
try
AUROXTL_COMMODITY_TRY
{
auto itr = in.find(type);
if (itr == in.end())
@ -38,7 +40,7 @@ inline bool AuTryRemove(T &in, const Z &type)
in.erase(itr);
return true;
}
catch (...)
AUROXTL_COMMODITY_CATCH
{
return false;
}
@ -79,7 +81,7 @@ inline void AuRemoveRange(T &in, AuUInt index, AuUInt length)
template <class T>
inline bool AuTryRemoveRange(T &in, AuUInt index, AuUInt length)
{
try
AUROXTL_COMMODITY_TRY
{
if (index + length > in.size())
{
@ -96,7 +98,7 @@ inline bool AuTryRemoveRange(T &in, AuUInt index, AuUInt length)
in.erase(begin++);
}
}
catch (...)
AUROXTL_COMMODITY_CATCH
{
return false;
}
@ -288,17 +290,45 @@ inline bool AuTryRemoveByTupleN(List &list, const Key &key)
return false;
}
#include <auROXTL/auTupleUtils.hpp>
namespace __audetail
{
template <class T>
struct AuHascapacity
{
template <class C> static constexpr AuTrueType Test(decltype(&C::capacity));
template <class C> static constexpr AuFalseType Test(...);
using type = decltype(Test<T>(0));
};
template <class T>
constexpr inline bool AuHascapacity_v = AuHascapacity<T>::type::value;
template <class T>
struct AuHasreserve
{
template <class C> static constexpr AuTrueType Test(decltype(&C::reserve));
template <class C> static constexpr AuFalseType Test(...);
using type = decltype(Test<T>(0));
};
template <class T>
constexpr inline bool AuHasreserve_v = AuHasreserve<T>::type::value;
template <class T>
constexpr inline bool AuIsPreallocatable_v = AuHascapacity_v<T> && AuHasreserve_v<T>;
}
template <class Container, typename Key_t, typename Type_t>
inline bool AuTryInsert(Container &container, const Key_t &key, Type_t &&value)
inline bool AuTryInsert(Container &container, Key_t &&key, Type_t &&value)
{
try
AUROXTL_COMMODITY_TRY
{
auto itr = container.find(key);
if (itr == container.end())
{
container.insert(AuMakePair(key, value));
container.insert(AuMakePair(AuMove(key), AuMove(value)));
}
else
{
@ -306,36 +336,74 @@ inline bool AuTryInsert(Container &container, const Key_t &key, Type_t &&value)
}
return true;
}
catch (...)
AUROXTL_COMMODITY_CATCH
{
return false;
}
}
template <class Container>
inline bool AuContainerExpandOne(Container &container)
{
auto required = container.size() + 1;
if (container.capacity() >= required)
{
return true;
}
container.reserve(required);
return container.capacity() >= required;
}
template <class Container>
inline bool AuContainerExpandOne(Container *container)
{
return AuContainerExpandOne(*container);
}
template <class Container, typename Type>
inline bool AuTryInsert(Container &container, Type &&value)
{
AUROXTL_COMMODITY_TRY
{
if constexpr (__audetail::AuIsPreallocatable_v<AuRemoveReference_t<Container>>)
{
if (!AuContainerExpandOne(container))
{
return false;
}
}
container.insert(container.end(), AuMove(value));
return true;
}
AUROXTL_COMMODITY_CATCH
{
return false;
}
}
template <class Container, typename Type>
inline bool AuTryInsert(Container &container, const Type &value)
{
try
AUROXTL_COMMODITY_TRY
{
if constexpr (__audetail::AuIsPreallocatable_v<AuRemoveReference_t<Container>>)
{
if (!AuContainerExpandOne(container))
{
return false;
}
}
container.insert(container.end(), value);
return true;
}
catch (...)
{
return false;
}
}
template <class Container, typename Type>
inline bool AuTryInsert(Container &container, Type &&value)
{
try
{
container.insert(container.end(), value);
return true;
}
catch (...)
AUROXTL_COMMODITY_CATCH
{
return false;
}
@ -344,13 +412,21 @@ inline bool AuTryInsert(Container &container, Type &&value)
template <class Container, typename Type>
inline bool AuTryInsert(Container *container, const Type &value)
{
try
AUROXTL_COMMODITY_TRY
{
if constexpr (__audetail::AuIsPreallocatable_v<AuRemoveReference_t<Container>>)
{
if (!AuContainerExpandOne(container))
{
return false;
}
}
container->insert(container->end(), value);
return true;
}
catch (...)
AUROXTL_COMMODITY_CATCH
{
return false;
}
@ -359,13 +435,21 @@ inline bool AuTryInsert(Container *container, const Type &value)
template <class Container, typename Type>
inline bool AuTryInsert(Container *container, Type &&value)
{
try
AUROXTL_COMMODITY_TRY
{
container->insert(container->end(), value);
if constexpr (__audetail::AuIsPreallocatable_v<AuRemoveReference_t<Container>>)
{
if (!AuContainerExpandOne(container))
{
return false;
}
}
container->insert(container->end(), AuMove(value));
return true;
}
catch (...)
AUROXTL_COMMODITY_CATCH
{
return false;
}
@ -374,58 +458,90 @@ inline bool AuTryInsert(Container *container, Type &&value)
template <class Container, typename Type>
inline bool AuTryInsertNoEnd(Container &container, Type &&value) // move
{
try
AUROXTL_COMMODITY_TRY
{
if constexpr (__audetail::AuIsPreallocatable_v<AuRemoveReference_t<Container>>)
{
if (!AuContainerExpandOne(container))
{
return false;
}
}
container.insert(AuMove(value));
return true;
}
AUROXTL_COMMODITY_CATCH
{
return false;
}
}
template <class Container, typename Type>
inline bool AuTryInsertNoEnd(Container &container, const Type &value)
{
AUROXTL_COMMODITY_TRY
{
if constexpr (__audetail::AuIsPreallocatable_v<AuRemoveReference_t<Container>>)
{
if (!AuContainerExpandOne(container))
{
return false;
}
}
container.insert(value);
return true;
}
catch (...)
AUROXTL_COMMODITY_CATCH
{
return false;
}
}
template <class Container, typename Type>
inline bool AuTryInsertNoEnd(Container &container, const Type &value) // copy
inline bool AuTryInsertNoEnd(Container *container, Type &&value)
{
try
AUROXTL_COMMODITY_TRY
{
container.insert(value);
if constexpr (__audetail::AuIsPreallocatable_v<AuRemoveReference_t<Container>>)
{
if (!AuContainerExpandOne(container))
{
return false;
}
}
container->insert(AuMove(value));
return true;
}
catch (...)
AUROXTL_COMMODITY_CATCH
{
return false;
}
}
template <class Container, typename Type>
inline bool AuTryInsertNoEnd(Container *container, Type &&value) // move
inline bool AuTryInsertNoEnd(Container *container, const Type &value)
{
try
AUROXTL_COMMODITY_TRY
{
if constexpr (__audetail::AuIsPreallocatable_v<AuRemoveReference_t<Container>>)
{
if (!AuContainerExpandOne(container))
{
return false;
}
}
container->insert(value);
return true;
}
catch (...)
{
return false;
}
}
template <class Container, typename Type>
inline bool AuTryInsertNoEnd(Container *container, const Type &value) // copy
{
try
{
container->insert(value);
return true;
}
catch (...)
AUROXTL_COMMODITY_CATCH
{
return false;
}
@ -439,7 +555,7 @@ namespace Aurora::Memory
template <class T>
inline bool AuTryResize(T &list, AuUInt length)
{
try
AUROXTL_COMMODITY_TRY
{
if constexpr (AuIsSame_v<T, Aurora::Memory::ByteBuffer>)
{
@ -451,7 +567,7 @@ inline bool AuTryResize(T &list, AuUInt length)
return true;
}
}
catch (...)
AUROXTL_COMMODITY_CATCH
{
return false;
}
@ -460,7 +576,7 @@ inline bool AuTryResize(T &list, AuUInt length)
template <class T>
inline bool AuTryDownsize(T &list, AuUInt length)
{
try
AUROXTL_COMMODITY_TRY
{
if constexpr (AuIsSame_v<T, Aurora::Memory::ByteBuffer>)
{
@ -479,7 +595,7 @@ inline bool AuTryDownsize(T &list, AuUInt length)
return true;
}
}
catch (...)
AUROXTL_COMMODITY_CATCH
{
return false;
}