BAN: Add min, max and clamp

This commit is contained in:
Bananymous 2022-12-30 19:52:16 +02:00
parent ef0b2010e0
commit b60af90538
4 changed files with 30 additions and 6 deletions

View File

@ -1,4 +1,5 @@
#include <BAN/Errors.h> #include <BAN/Errors.h>
#include <BAN/Math.h>
#include <BAN/Memory.h> #include <BAN/Memory.h>
#include <BAN/Move.h> #include <BAN/Move.h>
#include <BAN/String.h> #include <BAN/String.h>
@ -6,7 +7,6 @@
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include <sys/param.h>
namespace BAN namespace BAN
{ {
@ -200,7 +200,7 @@ namespace BAN
{ {
if (m_capasity >= size) if (m_capasity >= size)
return {}; return {};
size_type new_cap = MAX(size, m_capasity * 1.5f); size_type new_cap = BAN::max<size_type>(size, m_capasity * 1.5f);
void* new_data = BAN::allocator(new_cap); void* new_data = BAN::allocator(new_cap);
if (new_data == nullptr) if (new_data == nullptr)
return Error::FromString("String: Could not allocate memory"); return Error::FromString("String: Could not allocate memory");

24
BAN/include/BAN/Math.h Normal file
View File

@ -0,0 +1,24 @@
#pragma once
namespace BAN
{
template<typename T>
T min(T a, T b)
{
return a < b ? a : b;
}
template<typename T>
T max(T a, T b)
{
return a > b ? a : b;
}
template<typename T>
T clamp(T x, T min, T max)
{
return x < min ? min : x > max ? max : x;
}
}

View File

@ -1,12 +1,12 @@
#pragma once #pragma once
#include <BAN/Errors.h> #include <BAN/Errors.h>
#include <BAN/Math.h>
#include <BAN/Memory.h> #include <BAN/Memory.h>
#include <assert.h> #include <assert.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <sys/param.h>
namespace BAN namespace BAN
{ {
@ -96,7 +96,7 @@ namespace BAN
if (m_capacity > size) if (m_capacity > size)
return {}; return {};
size_type new_cap = MAX(m_capacity * 1.5f, m_capacity + 1); size_type new_cap = BAN::max<size_type>(m_capacity * 1.5f, m_capacity + 1);
void* new_data = BAN::allocator(new_cap * sizeof(T)); void* new_data = BAN::allocator(new_cap * sizeof(T));
if (new_data == nullptr) if (new_data == nullptr)
return Error::FromString("Queue: Could not allocate memory"); return Error::FromString("Queue: Could not allocate memory");

View File

@ -1,11 +1,11 @@
#pragma once #pragma once
#include <BAN/Errors.h> #include <BAN/Errors.h>
#include <BAN/Math.h>
#include <BAN/Memory.h> #include <BAN/Memory.h>
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include <sys/param.h>
namespace BAN namespace BAN
{ {
@ -208,7 +208,7 @@ namespace BAN
{ {
if (m_capasity >= size) if (m_capasity >= size)
return {}; return {};
size_type new_cap = MAX(size, m_capasity * 1.5f); size_type new_cap = BAN::max<size_type>(size, m_capasity * 1.5f);
void* new_data = BAN::allocator(new_cap * sizeof(T)); void* new_data = BAN::allocator(new_cap * sizeof(T));
if (new_data == nullptr) if (new_data == nullptr)
return Error::FromString("Vector: Could not allocate memory"); return Error::FromString("Vector: Could not allocate memory");