QRange
A powerful user-friendly tool for working with numeric ranges in Qt.
 
Loading...
Searching...
No Matches
QRange< T > Class Template Reference

#include <qrange.h>

Public Member Functions

 QRange ()=default
 Constructs a null range. Null ranges are also considered zero.
 
 ~QRange ()=default
 
 QRange (T start, T end)
 QRange(T start, T end) - the constructor with 2 parameters.
 
 QRange (std::pair< T, T > start_end)
 QRange(std::pair<T, T> start_end) - A constructor that creates a range based on a pair of set values, where the first element of the pair is the beginning of the range, and the second is the end.
 
 QRange (QString start, QString end, uint8_t first_base, uint8_t second_base=0)
 QRange(QString start, QString end, uint8_t first_base, uint8_t second_base = 0) - A constructor that works with different calculus systems where:
 
 QRange (QString start, T offset, uint8_t base)
 QRange - A constructor that takes the beginning of the range in a defined calculus systems, the calculus system itself and offset from the beginning.
 
start () const
 Gets the start of the range.
 
end () const
 Gets the end of the range.
 
lower () const
 Gets the lower bound of the range.
 
upper () const
 Gets the upper bound of the range.
 
middle () const
 Gets the midpoint of the range.
 
double middleAccurate () const
 Gets the exact center of the range, regardless of its generic type.
 
length () const
 Gets the length of the range.
 
QRangeDirection direction () const
 Gets the direction of the range.
 
std::pair< QString, QString > toAnotherBase (uint8_t base) const
 Gets the range in another base.
 
void setStart (const T start)
 Sets the start of the range.
 
void setEnd (const T end)
 Sets the end of the range.
 
void setDirection (const QRangeDirection &direction)
 Sets the direction of the range by swapping the start and end.
 
void setDirection (int direction)
 
void reverse (const QRangeDirection &direction=QRangeDirection::Zero)
 This method changes the values of the start and end of the range relative to the x-axis.
 
void reverse (int direction)
 
bool isPositive (QRangeSensitivity sensitivity=QRangeSensitivity::Sensitive) const
 isPositive property. Checks the beginning and end of the range relative to the x-axis.
 
bool isNegative (QRangeSensitivity sensitivity=QRangeSensitivity::Sensitive) const
 isNegative property. Checks the beginning and end of the range relative to the x-axis.
 
bool isMixed () const
 isMixed property. Checks the beginning and end of the range relative to the x-axis.
 
bool isZero () const
 isZero property.
 
bool contains (T number, QRangeSensitivity sensitivity=QRangeSensitivity::Sensitive) const
 This method checks whether a number is contained within the range.
 
bool contains (const QRange &range, QRangeSensitivity sensitivity=QRangeSensitivity::Sensitive) const
 This method checks whether an external range is contained within the source range.
 
bool in (const QRange &range, QRangeSensitivity sensitivity=QRangeSensitivity::Sensitive) const
 This method checks whether the source range is contained within the external range.
 
bool overlays (const QRange &range, QRangeSensitivity sensitivity=QRangeSensitivity::Sensitive) const
 This method checks whether one range overlaps with another.
 
QRange operator+ (T number) const
 Operators +, -, *, /, % perform the appropriate mathematical operations with the start and the end of the range.
 
QRange operator- (T number) const
 
QRange operator* (T number) const
 
QRange operator/ (T number) const
 
QRange operator% (T number) const
 
std::pair< QRange, QRangeoperator+ (const QRange &range) const
 Operator + works the same way as the set union operator.
 
std::pair< QRange, QRangeoperator- (const QRange &range) const
 Operator - works the same way as the set difference operator.
 
QRange operator* (const QRange &range) const
 Operator * works the same way as the set intersection operator.
 
std::pair< QRange, QRangeoperator/ (const QRange &range) const
 Operator / works the same way as the set complement operator.
 
bool operator> (const QRange &range) const
 Operators >, <.
 
bool operator< (const QRange &range) const
 
bool operator== (const QRange &range) const
 Operators ==, !=.
 
bool operator!= (const QRange &range) const
 
bool operator>= (const QRange &range) const
 Operators >=, <=.
 
bool operator<= (const QRange &range) const
 

Static Public Member Functions

template<typename... Args>
static QRange min (const Args &... ranges)
 This method finds the minimum length range from a set of ranges.
 
template<typename... Args>
static QRange max (const Args &... ranges)
 This method finds the maximum length range from a set of ranges.
 
template<typename... Args>
static QRange minX (const Args &... ranges)
 This method finds the minimum range on the x-axis from a set of ranges.
 
template<typename... Args>
static QRange maxX (const Args &... ranges)
 This method finds the maximum range on the x-axis from a set of ranges.
 
template<typename... Args>
static QRange minMidX (const Args &... ranges)
 This method finds the range that is closest to minus infinity on the x-axis.
 
template<typename... Args>
static QRange maxMidX (const Args &... ranges)
 This method finds the range that is closest to plus infinity on the x-axis.
 
static QList< T > brake (const QList< QRange > &ranges)
 Method converts all ranges in the list to a number list.
 
static QList< QRangemerge (const QList< T > &numbers)
 Method converts a list of numbers to the list of ranges. If there is an even number of elements, it will take each 2 numbers and create QRanges, where the first number will be the start of the range and the second will be the end. If there is an odd number of elements, the last QRange will start and end with the value of the last element.
 
static QList< QRangecut (const QRange &range, int number)
 Method cuts the range into equal parts, the number of which is determined by.
 
static QRange combine (const QList< QRange > &ranges, QRangeSensitivity sensitivity=QRangeSensitivity::Sensitive)
 Method connects ranges from a QList to a single range.
 

Protected Types

using ArgsType
 QRange also has several functions with a variable number of parameters. These functions use the alias ArgsType. This allows you to pass in functions (such as QRange::min()) both single objects and arrays. That is, you can simultaneously pass QRange and QList<QRange> to the QRange::min() function and it will calculate the result correctly.
 

Protected Attributes

m_start { 0 }
 
m_end { 0 }
 
QRangeDirection m_direction { Zero }
 

Static Protected Attributes

static const bool is_normal_v
 The library works with certain types of numeric data described below:
 

Friends

std::ostream & operator<< (std::ostream &os, const QRange &range)
 
QDebug operator<< (QDebug dbg, const QRange &range)
 

Detailed Description

template<typename T>
class QRange< T >
Author
25-masik-52
Version
1.0.0
Date
04.11.2024

Member Typedef Documentation

◆ ArgsType

template<typename T>
using QRange< T >::ArgsType
protected
Initial value:
std::variant<QRange,
std::pair<QRange, QRange>,
QList<QRange>,
std::list<QRange>,
std::vector<QRange>>
QRange()=default
Constructs a null range. Null ranges are also considered zero.

QRange also has several functions with a variable number of parameters. These functions use the alias ArgsType. This allows you to pass in functions (such as QRange::min()) both single objects and arrays. That is, you can simultaneously pass QRange and QList<QRange> to the QRange::min() function and it will calculate the result correctly.

Constructor & Destructor Documentation

◆ QRange() [1/5]

template<typename T>
QRange< T >::QRange ( )
default

Constructs a null range. Null ranges are also considered zero.

See also
QRange::isZero().

◆ ~QRange()

template<typename T>
QRange< T >::~QRange ( )
default

◆ QRange() [2/5]

template<typename T>
QRange< T >::QRange ( T start,
T end )
inlineexplicit

QRange(T start, T end) - the constructor with 2 parameters.

Parameters
start- the beginning of the range.
end- the end of the range.

◆ QRange() [3/5]

template<typename T>
QRange< T >::QRange ( std::pair< T, T > start_end)
inlineexplicit

QRange(std::pair<T, T> start_end) - A constructor that creates a range based on a pair of set values, where the first element of the pair is the beginning of the range, and the second is the end.

◆ QRange() [4/5]

template<typename T>
QRange< T >::QRange ( QString start,
QString end,
uint8_t first_base,
uint8_t second_base = 0 )
inlineexplicit

QRange(QString start, QString end, uint8_t first_base, uint8_t second_base = 0) - A constructor that works with different calculus systems where:

Parameters
start- QString value in a certain calculus systems of the beginning of the range.
end- QString value in a certain calculus systems of the end of the range.
first_base- the calculus systems base for the beginning of the range.
second_base- the calculus systems base for the end of the range.
Warning
QString format must be only a number in a certain calculus systems. Some kind of "0xFFF" or "#FFF" in the hexadecimal calculus systems will not work and you will get zero range (QRange{0, 0}).
See also
QRange::toAnotherBase(uint8_t base).

◆ QRange() [5/5]

template<typename T>
QRange< T >::QRange ( QString start,
T offset,
uint8_t base )
inlineexplicit

QRange - A constructor that takes the beginning of the range in a defined calculus systems, the calculus system itself and offset from the beginning.

Parameters
start- QString value in a certain calculus systems of the beginning of the range.
offset- offset from the beginning of the range.
base- the calculus system base for the beginning of the range.
See also
QRange::QRange(QString start, QString end, uint8_t first_base, uint8_t second_base = 0).

Member Function Documentation

◆ brake()

template<typename T>
static QList< T > QRange< T >::brake ( const QList< QRange< T > > & ranges)
inlinestatic

Method converts all ranges in the list to a number list.

Parameters
rangesQList of ranges.
Returns
Number list.

◆ combine()

template<typename T>
static QRange QRange< T >::combine ( const QList< QRange< T > > & ranges,
QRangeSensitivity sensitivity = QRangeSensitivity::Sensitive )
inlinestatic

Method connects ranges from a QList to a single range.

Parameters
ranges
sensitivity.If QRangeSensitivity::Insensitive is set, the resulting range will have the same direction as most ranges with the same direction.
Warning
If QRangeSensitivity::Sensitive is set, ensure that the ranges do not have gaps between them and that all of them have the same direction!
Returns
Calculated range.
QRange<int> r{1, 10};
QRange<int> r1{-7, 0};
QRange<int> r2{10, 1};
QRange<int> r3{10, 15};
auto res = QRange<int>::combine({r, r1}, Sensitive); // gap error
auto res1 = QRange<int>::combine({r, r2}, Sensitive); // direction error
auto res2 = QRange<int>::combine({r, r3}, Sensitive); // res2 == QRange{1, 15}
auto res3 = QRange<int>::combine({r, r1, r2, r3}, Insensitive); // res3 == QRange{-7, 15}
Definition qrange.h:36
static QRange combine(const QList< QRange > &ranges, QRangeSensitivity sensitivity=QRangeSensitivity::Sensitive)
Method connects ranges from a QList to a single range.
Definition qrange.h:562
@ Insensitive
Definition qrange.h:23
@ Sensitive
Definition qrange.h:22
See also
enum QRangeSensitivity

◆ contains() [1/2]

template<typename T>
bool QRange< T >::contains ( const QRange< T > & range,
QRangeSensitivity sensitivity = QRangeSensitivity::Sensitive ) const
inline

This method checks whether an external range is contained within the source range.

Parameters
rangeThe external range to check.
sensitivitySensitivity level for the check.
Returns
True if the external range is within the source range; otherwise, returns false.
See also
enum QRangeSensitivity.

◆ contains() [2/2]

template<typename T>
bool QRange< T >::contains ( T number,
QRangeSensitivity sensitivity = QRangeSensitivity::Sensitive ) const
inline

This method checks whether a number is contained within the range.

Parameters
numberThe number to check.
sensitivitySensitivity level for the check.
Returns
True if the number is within the range; otherwise, returns false.
See also
enum QRangeSensitivity.

◆ cut()

template<typename T>
static QList< QRange > QRange< T >::cut ( const QRange< T > & range,
int number )
inlinestatic

Method cuts the range into equal parts, the number of which is determined by.

Parameters
number.
rangeThe QRange that should be cut.
Returns
Calculated QList of ranges.
Warning
If number is greater than the range length, the method may behave unexpectedly.

◆ direction()

template<typename T>
QRangeDirection QRange< T >::direction ( ) const
inline

Gets the direction of the range.

Returns
The direction of the range.
See also
enum QRangeDirection.

◆ end()

template<typename T>
T QRange< T >::end ( ) const
inline

Gets the end of the range.

Returns
The end of the range.

◆ in()

template<typename T>
bool QRange< T >::in ( const QRange< T > & range,
QRangeSensitivity sensitivity = QRangeSensitivity::Sensitive ) const
inline

This method checks whether the source range is contained within the external range.

Parameters
rangeThe external range to check against.
sensitivitySensitivity level for the check.
Returns
True if the source range is within the external range; otherwise, returns false.
See also
enum QRangeSensitivity.

◆ isMixed()

template<typename T>
bool QRange< T >::isMixed ( ) const
inline

isMixed property. Checks the beginning and end of the range relative to the x-axis.

Returns
True if start and end belong to different sets of positive and negative numbers; otherwise, returns false.

◆ isNegative()

template<typename T>
bool QRange< T >::isNegative ( QRangeSensitivity sensitivity = QRangeSensitivity::Sensitive) const
inline

isNegative property. Checks the beginning and end of the range relative to the x-axis.

Parameters
sensitivityThe sensitivity level for the check.
Returns
True if both start and end belong to the set of negative numbers; otherwise, returns false.
See also
enum QRangeSensitivity.

◆ isPositive()

template<typename T>
bool QRange< T >::isPositive ( QRangeSensitivity sensitivity = QRangeSensitivity::Sensitive) const
inline

isPositive property. Checks the beginning and end of the range relative to the x-axis.

Parameters
sensitivityThe sensitivity level for the check.
Returns
True if both start and end belong to the set of positive numbers; otherwise, returns false.
See also
enum QRangeSensitivity.

◆ isZero()

template<typename T>
bool QRange< T >::isZero ( ) const
inline

isZero property.

Returns
True if both start and end equal 0; otherwise, returns false.

◆ length()

template<typename T>
T QRange< T >::length ( ) const
inline

Gets the length of the range.

Returns
The difference between the beginning and the end of the range.

◆ lower()

template<typename T>
T QRange< T >::lower ( ) const
inline

Gets the lower bound of the range.

Returns
The minimum value between the start and end of the range.

◆ max()

template<typename T>
template<typename... Args>
static QRange QRange< T >::max ( const Args &... ranges)
inlinestatic

This method finds the maximum length range from a set of ranges.

Parameters
rangesA collection of QRanges in formats described by ArgsType.
Returns
The maximum length range.
See also
ArgsType.

◆ maxMidX()

template<typename T>
template<typename... Args>
static QRange QRange< T >::maxMidX ( const Args &... ranges)
inlinestatic

This method finds the range that is closest to plus infinity on the x-axis.

Parameters
rangesA collection of QRanges in formats described by ArgsType.
Returns
The range that is closest to plus infinity on the x-axis.
QRange<int> r{1, 11};
QRange<int> r1{8, 10};
auto res = QRange::maxMidX(r, r1); // res == r1
static QRange maxMidX(const Args &... ranges)
This method finds the range that is closest to plus infinity on the x-axis.
Definition qrange.h:459
See also
ArgsType.

◆ maxX()

template<typename T>
template<typename... Args>
static QRange QRange< T >::maxX ( const Args &... ranges)
inlinestatic

This method finds the maximum range on the x-axis from a set of ranges.

Parameters
rangesA collection of QRanges in formats described by ArgsType.
Returns
The maximum range on the x-axis.
See also
ArgsType.

◆ merge()

template<typename T>
static QList< QRange > QRange< T >::merge ( const QList< T > & numbers)
inlinestatic

Method converts a list of numbers to the list of ranges. If there is an even number of elements, it will take each 2 numbers and create QRanges, where the first number will be the start of the range and the second will be the end. If there is an odd number of elements, the last QRange will start and end with the value of the last element.

Parameters
numbers
Returns
QList of ranges.

◆ middle()

template<typename T>
T QRange< T >::middle ( ) const
inline

Gets the midpoint of the range.

Returns
The center of the range.

◆ middleAccurate()

template<typename T>
double QRange< T >::middleAccurate ( ) const
inline

Gets the exact center of the range, regardless of its generic type.

Returns
The precise center of the range.

◆ min()

template<typename T>
template<typename... Args>
static QRange QRange< T >::min ( const Args &... ranges)
inlinestatic

This method finds the minimum length range from a set of ranges.

Parameters
rangesA collection of QRanges in formats described by ArgsType.
Returns
The minimum length range.
See also
ArgsType.

◆ minMidX()

template<typename T>
template<typename... Args>
static QRange QRange< T >::minMidX ( const Args &... ranges)
inlinestatic

This method finds the range that is closest to minus infinity on the x-axis.

Parameters
rangesA collection of QRanges in formats described by ArgsType.
Returns
The range that is closest to minus infinity on the x-axis.
QRange<int> r{0, 10};
QRange<int> r1{1, 3};
auto res = QRange::minMidX(r, r1); // res == r1
static QRange minMidX(const Args &... ranges)
This method finds the range that is closest to minus infinity on the x-axis.
Definition qrange.h:439
See also
ArgsType.

◆ minX()

template<typename T>
template<typename... Args>
static QRange QRange< T >::minX ( const Args &... ranges)
inlinestatic

This method finds the minimum range on the x-axis from a set of ranges.

Parameters
rangesA collection of QRanges in formats described by ArgsType.
Returns
The minimum range on the x-axis.
See also
ArgsType.

◆ operator!=()

template<typename T>
bool QRange< T >::operator!= ( const QRange< T > & range) const
inline

◆ operator%()

template<typename T>
QRange QRange< T >::operator% ( T number) const
inline

◆ operator*() [1/2]

template<typename T>
QRange QRange< T >::operator* ( const QRange< T > & range) const
inline

Operator * works the same way as the set intersection operator.

Parameters
range
Returns
Calculated ranges.
See also
Fundamentals of mathematical analysis. (http://www.mathprofi.ru/mnozhestva.html)

◆ operator*() [2/2]

template<typename T>
QRange QRange< T >::operator* ( T number) const
inline

◆ operator+() [1/2]

template<typename T>
std::pair< QRange, QRange > QRange< T >::operator+ ( const QRange< T > & range) const
inline

Operator + works the same way as the set union operator.

Parameters
range
Returns
Calculated ranges.
See also
Fundamentals of mathematical analysis. (http://www.mathprofi.ru/mnozhestva.html)

◆ operator+() [2/2]

template<typename T>
QRange QRange< T >::operator+ ( T number) const
inline

Operators +, -, *, /, % perform the appropriate mathematical operations with the start and the end of the range.

Parameters
number
Returns
New QRange with calculated values of the start and end of the range.

◆ operator-() [1/2]

template<typename T>
std::pair< QRange, QRange > QRange< T >::operator- ( const QRange< T > & range) const
inline

Operator - works the same way as the set difference operator.

Parameters
range
Returns
Calculated ranges.
See also
Fundamentals of mathematical analysis. (http://www.mathprofi.ru/mnozhestva.html)

◆ operator-() [2/2]

template<typename T>
QRange QRange< T >::operator- ( T number) const
inline

◆ operator/() [1/2]

template<typename T>
std::pair< QRange, QRange > QRange< T >::operator/ ( const QRange< T > & range) const
inline

Operator / works the same way as the set complement operator.

Parameters
range
Returns
Calculated ranges.
See also
Fundamentals of mathematical analysis. (http://www.mathprofi.ru/mnozhestva.html)

◆ operator/() [2/2]

template<typename T>
QRange QRange< T >::operator/ ( T number) const
inline

◆ operator<()

template<typename T>
bool QRange< T >::operator< ( const QRange< T > & range) const
inline

◆ operator<=()

template<typename T>
bool QRange< T >::operator<= ( const QRange< T > & range) const
inline

◆ operator==()

template<typename T>
bool QRange< T >::operator== ( const QRange< T > & range) const
inline

Operators ==, !=.

Parameters
range
Returns
True if the ranges, taking into account the direction, are equal or unequal, respectively; otherwise, return false.

◆ operator>()

template<typename T>
bool QRange< T >::operator> ( const QRange< T > & range) const
inline

Operators >, <.

Parameters
range
Returns
True if the ranges being compared are closer or further to plus infinity on the x-axis, respectively; otherwise, return false.

◆ operator>=()

template<typename T>
bool QRange< T >::operator>= ( const QRange< T > & range) const
inline

Operators >=, <=.

Parameters
range
Returns
True for the operator >= if true for the operators > and ==; otherwise, return false.
True for the operator <= if true for the operators < and ==; otherwise, return false.

◆ overlays()

template<typename T>
bool QRange< T >::overlays ( const QRange< T > & range,
QRangeSensitivity sensitivity = QRangeSensitivity::Sensitive ) const
inline

This method checks whether one range overlaps with another.

Parameters
rangeThe range to check for overlap.
sensitivitySensitivity level for the check.
Returns
True if part of the source range overlaps with the external range; otherwise, returns false.
See also
enum QRangeSensitivity.

◆ reverse() [1/2]

template<typename T>
void QRange< T >::reverse ( const QRangeDirection & direction = QRangeDirection::Zero)
inline

This method changes the values of the start and end of the range relative to the x-axis.

Parameters
directionThe direction to reverse the range.
QRange<int> r{-1, 10};
r.reverse(Zero); // r == QRange<int>{1, -10}
r.reverse(Minus); // r == QRange<int>{-1, -10}
r.reverse(Plus); // r == QRange<int>{1, 10}
@ Plus
Definition qrange.h:13
@ Minus
Definition qrange.h:11
@ Zero
Definition qrange.h:12
See also
enum QRangeDirection.

◆ reverse() [2/2]

template<typename T>
void QRange< T >::reverse ( int direction)
inline

◆ setDirection() [1/2]

template<typename T>
void QRange< T >::setDirection ( const QRangeDirection & direction)
inline

Sets the direction of the range by swapping the start and end.

Parameters
directionThe QRangeDirection parameter.
Warning
Direction cannot be changed in a zero-length range.
See also
enum QRangeDirection.

◆ setDirection() [2/2]

template<typename T>
void QRange< T >::setDirection ( int direction)
inline

◆ setEnd()

template<typename T>
void QRange< T >::setEnd ( const T end)
inline

Sets the end of the range.

Parameters
endThe end of the range.
Note
This method may change the direction of the range.
See also
enum QRangeDirection.

◆ setStart()

template<typename T>
void QRange< T >::setStart ( const T start)
inline

Sets the start of the range.

Parameters
startThe beginning of the range.
Note
This method may change the direction of the range.
See also
enum QRangeDirection.

◆ start()

template<typename T>
T QRange< T >::start ( ) const
inline

Gets the start of the range.

Returns
The beginning of the range.

◆ toAnotherBase()

template<typename T>
std::pair< QString, QString > QRange< T >::toAnotherBase ( uint8_t base) const
inline

Gets the range in another base.

Parameters
baseThe base for the calculus system of the beginning and end of the range.
Returns
A pair of values in the specified calculus system, where the first value is the beginning and the second is the end of the range.

◆ upper()

template<typename T>
T QRange< T >::upper ( ) const
inline

Gets the upper bound of the range.

Returns
The maximum value between the start and end of the range.

Friends And Related Symbol Documentation

◆ operator<< [1/2]

template<typename T>
QDebug operator<< ( QDebug dbg,
const QRange< T > & range )
friend

◆ operator<< [2/2]

template<typename T>
std::ostream & operator<< ( std::ostream & os,
const QRange< T > & range )
friend

Member Data Documentation

◆ is_normal_v

template<typename T>
const bool QRange< T >::is_normal_v
staticprotected
Initial value:
= std::is_same<T, int>::value
|| std::is_same<T, long>::value
|| std::is_same<T, long long>::value
|| std::is_same<T, unsigned int>::value
|| std::is_same<T, unsigned long>::value
|| std::is_same<T, unsigned long long>::value
|| std::is_same<T, float>::value
|| std::is_same<T, double>::value
|| std::is_same<T, long double>::value

The library works with certain types of numeric data described below:

  • int
  • long
  • long long
  • unsigned int
  • unsigned long
  • unsigned long long
  • float
  • double
  • long double
Note
When working with floating-point numbers, you will not be able to use methods that work with different number systems. This is due to the fact that in standard Qt and C++ there is no simple conversion of floating-point numbers in a number system other than decimal to decimal.

◆ m_direction

template<typename T>
QRangeDirection QRange< T >::m_direction { Zero }
protected

◆ m_end

template<typename T>
T QRange< T >::m_end { 0 }
protected

◆ m_start

template<typename T>
T QRange< T >::m_start { 0 }
protected

The documentation for this class was generated from the following file: