diff options
author | Shannon Booth <shannon.ml.booth@gmail.com> | 2020-01-20 21:54:15 +1300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-01-20 10:35:12 +0100 |
commit | de74458f138e166785d2f0a66dc083ae8e123976 (patch) | |
tree | a6a59376258d1b408e0441677f6638908e802663 /AK/StdLibExtras.h | |
parent | fb7a885cae2177447fd2d94d8905ba522af43ef2 (diff) | |
download | serenity-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.h | 12 |
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; |