BAN: Basic containers have shrink_to_fit() method

I also chaged the default memory allocation increase from 1.5 to 2
This commit is contained in:
2023-02-22 02:07:05 +02:00
parent 5d31e89574
commit 4afc4660a4
6 changed files with 84 additions and 13 deletions

View File

@@ -64,6 +64,7 @@ namespace BAN
ErrorOr<void> resize(size_type);
ErrorOr<void> reserve(size_type);
ErrorOr<void> shrink_to_fit();
bool empty() const;
size_type size() const;
@@ -330,6 +331,7 @@ namespace BAN
ASSERT(m_size > 0);
return m_data[0];
}
template<typename T>
T& Vector<T>::front()
{
@@ -358,6 +360,20 @@ namespace BAN
return {};
}
template<typename T>
ErrorOr<void> Vector<T>::shrink_to_fit()
{
size_type temp = m_capacity;
m_capacity = 0;
auto error_or = ensure_capacity(m_size);
if (error_or.is_error())
{
m_capacity = temp;
return error_or;
}
return {};
}
template<typename T>
bool Vector<T>::empty() const
{
@@ -381,7 +397,7 @@ namespace BAN
{
if (m_capacity >= size)
return {};
size_type new_cap = BAN::Math::max<size_type>(size, m_capacity * 3 / 2);
size_type new_cap = BAN::Math::max<size_type>(size, m_capacity * 2);
T* new_data = (T*)BAN::allocator(new_cap * sizeof(T));
if (new_data == nullptr)
return Error::from_string("Vector: Could not allocate memory");