mirror of
https://github.com/ETLCPP/etl.git
synced 2026-05-01 03:19:10 +08:00
Added template parameter clamp functions
This commit is contained in:
parent
1908ab5b57
commit
bbfce66abc
@ -2142,19 +2142,33 @@ namespace etl
|
||||
///\ingroup algorithm
|
||||
//***************************************************************************
|
||||
template<typename T, typename TCompare>
|
||||
ETL_CONSTEXPR
|
||||
const T& clamp(const T& value, const T& low, const T& high, TCompare compare)
|
||||
ETL_CONSTEXPR
|
||||
T clamp(const T& value, const T& low, const T& high, TCompare compare)
|
||||
{
|
||||
return compare(value, low) ? low : compare(high, value) ? high : value;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
ETL_CONSTEXPR
|
||||
const T& clamp(const T& value, const T& low, const T& high )
|
||||
ETL_CONSTEXPR
|
||||
T clamp(const T& value, const T& low, const T& high)
|
||||
{
|
||||
return clamp(value, low, high, etl::less<T>());
|
||||
}
|
||||
|
||||
template<typename T, T Low, T High, typename TCompare>
|
||||
ETL_CONSTEXPR
|
||||
T clamp(const T& value, TCompare compare)
|
||||
{
|
||||
return compare(value, Low) ? Low : compare(High, value) ? High : value;
|
||||
}
|
||||
|
||||
template <typename T, T Low, T High>
|
||||
ETL_CONSTEXPR
|
||||
T clamp(const T& value)
|
||||
{
|
||||
return clamp<T, Low, High>(value, etl::less<T>());
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Remove
|
||||
///\ingroup algorithm
|
||||
|
||||
@ -2384,5 +2384,41 @@ namespace
|
||||
data = initial;
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(clamp_run_time)
|
||||
{
|
||||
CHECK_EQUAL(5, etl::clamp(5, 0, 10));
|
||||
CHECK_EQUAL(0, etl::clamp(-5, 0, 10));
|
||||
CHECK_EQUAL(10, etl::clamp(15, 0, 10));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(clamp_compile_time)
|
||||
{
|
||||
CHECK_EQUAL(5, (etl::clamp<int, 0, 10>(5)));
|
||||
CHECK_EQUAL(0, (etl::clamp<int, 0, 10>(-5)));
|
||||
CHECK_EQUAL(10, (etl::clamp<int, 0, 10>(15)));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(clamp_constexpr)
|
||||
{
|
||||
constexpr int result1 = etl::clamp(5, 0, 10);
|
||||
constexpr int result2 = etl::clamp(-5, 0, 10);
|
||||
constexpr int result3 = etl::clamp(15, 0, 10);
|
||||
|
||||
constexpr int result4 = etl::clamp<int, 0, 10>(5);
|
||||
constexpr int result5 = etl::clamp<int, 0, 10>(-5);
|
||||
constexpr int result6 = etl::clamp<int, 0, 10>(15);
|
||||
|
||||
CHECK_EQUAL(5, result1);
|
||||
CHECK_EQUAL(0, result2);
|
||||
CHECK_EQUAL(10, result3);
|
||||
|
||||
CHECK_EQUAL(5, result4);
|
||||
CHECK_EQUAL(0, result5);
|
||||
CHECK_EQUAL(10, result6);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user