Skip to content

Commit

Permalink
Add TRange::clip() (#2480)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 authored Feb 8, 2022
1 parent 4d3647d commit 436dcc8
Showing 1 changed file with 16 additions and 0 deletions.
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

0 comments on commit 436dcc8

Please sign in to comment.