mirror of
https://github.com/SerenityOS/serenity
synced 2024-07-21 10:05:32 +00:00
AK: Add clamp() function
This function can be used to more cleanly write the common operation of clamping a value between two values.
This commit is contained in:
parent
fb7a885cae
commit
de74458f13
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue