Template Class range
Defined in File range.hpp
Inheritance Relationships
Base Types
public rcppsw::er::client< range< T > >
(Template Class client)private rcppsw::er::stringizable
(Class stringizable)
Class Documentation
-
template<typename T>
class range : public rcppsw::er::client<range<T>>, private rcppsw::er::stringizable Convenience class holding a [min, max] range. Makes comparisons like “is this number in this range” much more intuitive and easy to debug.
If you use the default constructor you must call set() or read initialization from a stream before calling any member functions to avoid undefined behavior.
To call any member functions other than lb() and ub(), the range must be non-empty, meaning that the min must not be equal to the max (if it is an assertion will trigger).
No mutator functions are provided beyond setters, with the idea that ranges should generally be read-only. If you want to modify a range (e.g., translate() it), a new range should be returned and the original range be unmodified.
Public Functions
-
inline range(void) noexcept
-
inline T center(void) const
Get the midpoint of the range.
For integer ranges where span() is odd, this will truncate (i.e., the centerpoint is always of type
T
).
-
inline bool contains(const range<T> &other) const
Determine if one range completely contains another (boundary points are included with both ranges).
-
template<typename U = T, typename std::enable_if<!std::is_floating_point<U>::value, int>::type = 0>
inline bool contains(const T &value) const Determine if a value is within the range [lb, ub] (boundary points included).
- Parameters:
value – The value to test.
-
template<typename U = T, typename std::enable_if<std::is_floating_point<U>::value, int>::type = 0>
inline bool contains(const T &value) const
-
inline void lb(const T &lb)
Set the range lower bound. It must be less than the current ub() or an assertion will trigger.
-
inline bool overlaps_with(const range<T> &other) const
Determine if one range overlaps with another.
Overlap is a commutative calculation (i.e. overlap(A,B) <-> overlap(B,A)).
To implement this, we need to check if either bound of the argument is contained in our range, AND if either of our bounds are contained in the argument’s range. The second part is necessary if A is completely contained inside B in order for the calculation to be commutative.
-
inline range recenter(const T &value) const
Re-center the current range around the specified value, returning a new range centered at that value.
-
inline void set(const T &lb, const T &ub)
Set the lb() and ub() for the range simultaneously. The lb must be < ub or an assertion will trigger.
-
inline range shrink(const T &value) const
Shrink the current range in both directions with the specified value, returning a new range resulting from the shrink.
- Returns:
The shrunken range.
-
inline virtual std::string to_str(void) const override
Return a string representation of the range in the form of ‘[lb,ub]’.
-
inline range translate(const T &value) const
Translate the current range to the specified value, returning a new range resulting from the translation.
- Returns:
The new translated range.
-
inline range(void) noexcept