Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TRange::clip() method #2480

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Sming/Core/Data/Range.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

/**
* @brief Manage a range of numbers between specified limits
*
* Values in the range meet the critera (min <= value <= max)
*/
template <typename T> struct TRange {
T min{};
Expand Down Expand Up @@ -75,11 +77,25 @@ template <typename T> struct TRange {
{
}

/**
* @brief Determine if range contains a value
*/
bool contains(T value)
{
return (value >= min) && (value <= max);
}

/**
* @brief Clip values to within the range
*/
T clip(T value)
{
return (value < min) ? min : (value > max) ? max : value;
}

/**
* @brief Return a random value within the range
*/
T random() const
{
auto value = os_random();
Expand Down