From b60af9053850a748f706d6202e15756484433ad9 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 30 Dec 2022 19:52:16 +0200 Subject: [PATCH] BAN: Add min, max and clamp --- BAN/BAN/String.cpp | 4 ++-- BAN/include/BAN/Math.h | 24 ++++++++++++++++++++++++ BAN/include/BAN/Queue.h | 4 ++-- BAN/include/BAN/Vector.h | 4 ++-- 4 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 BAN/include/BAN/Math.h diff --git a/BAN/BAN/String.cpp b/BAN/BAN/String.cpp index 296878b9..23ef447c 100644 --- a/BAN/BAN/String.cpp +++ b/BAN/BAN/String.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -6,7 +7,6 @@ #include #include -#include namespace BAN { @@ -200,7 +200,7 @@ namespace BAN { if (m_capasity >= size) return {}; - size_type new_cap = MAX(size, m_capasity * 1.5f); + size_type new_cap = BAN::max(size, m_capasity * 1.5f); void* new_data = BAN::allocator(new_cap); if (new_data == nullptr) return Error::FromString("String: Could not allocate memory"); diff --git a/BAN/include/BAN/Math.h b/BAN/include/BAN/Math.h new file mode 100644 index 00000000..c699bbca --- /dev/null +++ b/BAN/include/BAN/Math.h @@ -0,0 +1,24 @@ +#pragma once + +namespace BAN +{ + + template + T min(T a, T b) + { + return a < b ? a : b; + } + + template + T max(T a, T b) + { + return a > b ? a : b; + } + + template + T clamp(T x, T min, T max) + { + return x < min ? min : x > max ? max : x; + } + +} \ No newline at end of file diff --git a/BAN/include/BAN/Queue.h b/BAN/include/BAN/Queue.h index 7ba4063a..b08bf9a6 100644 --- a/BAN/include/BAN/Queue.h +++ b/BAN/include/BAN/Queue.h @@ -1,12 +1,12 @@ #pragma once #include +#include #include #include #include #include -#include namespace BAN { @@ -96,7 +96,7 @@ namespace BAN if (m_capacity > size) return {}; - size_type new_cap = MAX(m_capacity * 1.5f, m_capacity + 1); + size_type new_cap = BAN::max(m_capacity * 1.5f, m_capacity + 1); void* new_data = BAN::allocator(new_cap * sizeof(T)); if (new_data == nullptr) return Error::FromString("Queue: Could not allocate memory"); diff --git a/BAN/include/BAN/Vector.h b/BAN/include/BAN/Vector.h index 8d030032..3f26f83b 100644 --- a/BAN/include/BAN/Vector.h +++ b/BAN/include/BAN/Vector.h @@ -1,11 +1,11 @@ #pragma once #include +#include #include #include #include -#include namespace BAN { @@ -208,7 +208,7 @@ namespace BAN { if (m_capasity >= size) return {}; - size_type new_cap = MAX(size, m_capasity * 1.5f); + size_type new_cap = BAN::max(size, m_capasity * 1.5f); void* new_data = BAN::allocator(new_cap * sizeof(T)); if (new_data == nullptr) return Error::FromString("Vector: Could not allocate memory");