summaryrefslogtreecommitdiff
path: root/AK/StdLibExtras.h
diff options
context:
space:
mode:
authorShannon Booth <shannon.ml.booth@gmail.com>2020-01-20 21:54:15 +1300
committerAndreas Kling <kling@serenityos.org>2020-01-20 10:35:12 +0100
commitde74458f138e166785d2f0a66dc083ae8e123976 (patch)
treea6a59376258d1b408e0441677f6638908e802663 /AK/StdLibExtras.h
parentfb7a885cae2177447fd2d94d8905ba522af43ef2 (diff)
downloadserenity-de74458f138e166785d2f0a66dc083ae8e123976.zip
AK: Add clamp() function
This function can be used to more cleanly write the common operation of clamping a value between two values.
Diffstat (limited to 'AK/StdLibExtras.h')
-rw-r--r--AK/StdLibExtras.h12
1 files changed, 12 insertions, 0 deletions
diff --git a/AK/StdLibExtras.h b/AK/StdLibExtras.h
index 565ef2ace7..69f19eb703 100644
--- a/AK/StdLibExtras.h
+++ b/AK/StdLibExtras.h
@@ -84,6 +84,17 @@ inline constexpr T max(const T& a, const T& b)
return a < b ? b : a;
}
+template<typename T>
+inline constexpr T clamp(const T& value, const T& min, const T& max)
+{
+ ASSERT(max > min);
+ if (value > max)
+ return max;
+ if (value < min)
+ return min;
+ return value;
+}
+
template<typename T, typename U>
inline constexpr T ceil_div(T a, U b)
{
@@ -317,6 +328,7 @@ using AK::forward;
using AK::IsSame;
using AK::max;
using AK::min;
+using AK::clamp;
using AK::move;
using AK::RemoveConst;
using AK::swap;