Template Class range

Inheritance Relationships

Base Types

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(const T &lb, const T &ub) noexcept
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 T lb(void) const

Get the lower bound of the range.

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 T span(void) const

Get the size of the range (max - max).

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 void ub(const T &ub)

Set the range upper bound. It must be greater than the current lb() or an assertion will trigger.

inline T ub(void) const

Get the upper bound of the range.

inline T wrap_value(T value) const

Wrap the specified value into the range [min, max] using wrap around addition/subtraction.

Returns:

The wrapped value.

Friends

inline friend std::ostream &operator<<(std::ostream &stream, const range &c_range)
inline friend std::istream &operator>>(std::istream &is, range &r)

For parsing a range from a string in the form of "LB:UB".