Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New QgsRange template based class for storing interval ranges
QgsRange classes represent a range of values of some element type. For instance, ranges of QDateTime might be used to represent the ranges of timestamp ranges. Ranges can indicate whether the upper and lower values are inclusive or exclusive. The inclusivity or exclusivity of bounds is considered when determining things like whether ranges overlap or during calculation of range intersections. Includes typedefs for QgsDoubleRange, QgsIntRange, QgsDateRange.
- Loading branch information
1 parent
33b6c41
commit c4ea048
Showing
7 changed files
with
687 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
class QgsDoubleRange | ||
{ | ||
%TypeHeaderCode | ||
#include <qgsrange.h> | ||
%End | ||
|
||
public: | ||
|
||
QgsDoubleRange( double lower, double upper, bool includeLower = true, bool includeUpper = true ); | ||
double lower() const; | ||
double upper() const; | ||
bool includeLower() const; | ||
bool includeUpper() const; | ||
bool isEmpty() const; | ||
bool contains( const QgsDoubleRange &other ) const; | ||
bool contains( double element ) const; | ||
bool overlaps( const QgsDoubleRange &other ) const; | ||
}; | ||
|
||
class QgsIntRange | ||
{ | ||
%TypeHeaderCode | ||
#include <qgsrange.h> | ||
%End | ||
|
||
public: | ||
|
||
QgsIntRange( int lower, int upper, bool includeLower = true, bool includeUpper = true ); | ||
int lower() const; | ||
int upper() const; | ||
bool includeLower() const; | ||
bool includeUpper() const; | ||
bool isEmpty() const; | ||
bool contains( const QgsIntRange &other ) const; | ||
bool contains( int element ) const; | ||
bool overlaps( const QgsIntRange &other ) const; | ||
}; | ||
|
||
class QgsDateRange | ||
{ | ||
%TypeHeaderCode | ||
#include <qgsrange.h> | ||
%End | ||
|
||
public: | ||
|
||
QgsDateRange( QDate lower, QDate upper, bool includeLower = true, bool includeUpper = true ); | ||
QDate lower() const; | ||
QDate upper() const; | ||
bool includeLower() const; | ||
bool includeUpper() const; | ||
bool isEmpty() const; | ||
bool contains( const QgsDateRange &other ) const; | ||
bool contains( QDate element ) const; | ||
bool overlaps( const QgsDateRange &other ) const; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,307 @@ | ||
/*************************************************************************** | ||
qgsrange.h | ||
---------- | ||
begin : April 2017 | ||
copyright : (C) 2017 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSRANGE_H | ||
#define QGSRANGE_H | ||
|
||
#include "qgis.h" | ||
#include "qgis_core.h" | ||
|
||
/** | ||
* \class QgsRange | ||
* \ingroup core | ||
* A template based class for storing ranges (lower to upper values). | ||
* | ||
* QgsRange classes represent a range of values of some element type. For instance, | ||
* ranges of QDateTime might be used to represent the ranges of timestamp ranges. | ||
* | ||
* Ranges can indicate whether the upper and lower values are inclusive or exclusive. | ||
* The inclusivity or exclusivity of bounds is considered when determining things like | ||
* whether ranges overlap or during calculation of range intersections. | ||
* | ||
* \since QGIS 3.0 | ||
* \see QgsDoubleRange | ||
* \see QgsIntRange | ||
* \note not available in Python bindings | ||
*/ | ||
template <class T> class CORE_EXPORT QgsRange | ||
{ | ||
public: | ||
|
||
/** | ||
* Constructor for QgsRange. The \a lower and \a upper bounds are specified, | ||
* and optionally whether or not these bounds are included in the range. | ||
*/ | ||
QgsRange( T lower, T upper, bool includeLower = true, bool includeUpper = true ) | ||
: mLower( lower ) | ||
, mUpper( upper ) | ||
, mIncludeLower( includeLower ) | ||
, mIncludeUpper( includeUpper ) | ||
{} | ||
|
||
/** | ||
* Returns the lower bound of the range. | ||
* \see upper() | ||
* \see includeLower() | ||
*/ | ||
T lower() const { return mLower; } | ||
|
||
/** | ||
* Returns the upper bound of the range. | ||
* \see lower() | ||
* \see includeUpper() | ||
*/ | ||
T upper() const { return mUpper; } | ||
|
||
/** | ||
* Returns true if the lower bound is inclusive, or false if the lower | ||
* bound is exclusive. | ||
* \see lower() | ||
* \see includeUpper() | ||
*/ | ||
bool includeLower() const { return mIncludeLower; } | ||
|
||
/** | ||
* Returns true if the upper bound is inclusive, or false if the upper | ||
* bound is exclusive. | ||
* \see upper() | ||
* \see includeLower() | ||
*/ | ||
bool includeUpper() const { return mIncludeUpper; } | ||
|
||
/** | ||
* Returns true if the range is empty, ie the lower bound equals (or exceeds) the upper bound | ||
* and either the bounds are exclusive. | ||
*/ | ||
bool isEmpty() const { return mLower > mUpper || ( mUpper == mLower && !( mIncludeLower || mIncludeUpper ) ); } | ||
|
||
/** | ||
* Returns true if this range contains another range. | ||
* \see overlaps() | ||
*/ | ||
bool contains( const QgsRange<T> &other ) const | ||
{ | ||
bool lowerOk = ( mIncludeLower && mLower <= other.mLower ) | ||
|| ( !mIncludeLower && mLower < other.mLower ) | ||
|| ( !mIncludeLower && !other.mIncludeLower && mLower <= other.mLower ); | ||
if ( !lowerOk ) | ||
return false; | ||
|
||
bool upperOk = ( mIncludeUpper && mUpper >= other.mUpper ) | ||
|| ( !mIncludeUpper && mUpper > other.mUpper ) | ||
|| ( !mIncludeUpper && !other.mIncludeUpper && mUpper >= other.mUpper ); | ||
if ( !upperOk ) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Returns true if this range contains a specified \a element. | ||
*/ | ||
bool contains( T element ) const | ||
{ | ||
bool lowerOk = ( mIncludeLower && mLower <= element ) | ||
|| ( !mIncludeLower && mLower < element ); | ||
if ( !lowerOk ) | ||
return false; | ||
|
||
bool upperOk = ( mIncludeUpper && mUpper >= element ) | ||
|| ( !mIncludeUpper && mUpper > element ); | ||
if ( !upperOk ) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Returns true if this range overlaps another range. | ||
* \see contains() | ||
*/ | ||
bool overlaps( const QgsRange<T> &other ) const | ||
{ | ||
if ( ( ( mIncludeLower && mLower <= other.mLower ) || ( !mIncludeLower && mLower < other.mLower ) ) | ||
&& ( ( mIncludeUpper && mUpper >= other.mUpper ) || ( !mIncludeUpper && mUpper > other.mUpper ) ) ) | ||
return true; | ||
|
||
if ( ( ( mIncludeLower && mLower <= other.mLower ) || ( !mIncludeLower && mLower < other.mLower ) ) | ||
&& ( ( mIncludeUpper && mUpper >= other.mLower ) || ( !mIncludeUpper && mUpper > other.mLower ) ) ) | ||
return true; | ||
|
||
if ( ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) ) | ||
&& ( ( mIncludeUpper && mUpper >= other.mUpper ) || ( !mIncludeUpper && mUpper > other.mUpper ) ) ) | ||
return true; | ||
|
||
if ( ( ( mIncludeLower && mLower >= other.mLower ) || ( !mIncludeLower && mLower > other.mLower ) ) | ||
&& ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) ) ) | ||
return true; | ||
|
||
if ( mLower == other.mLower && mUpper == other.mUpper ) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
|
||
private: | ||
|
||
T mLower; | ||
T mUpper; | ||
bool mIncludeLower = true; | ||
bool mIncludeUpper = true; | ||
|
||
}; | ||
|
||
|
||
/** | ||
* QgsRange which stores a range of double values. | ||
* \since QGIS 3.0 | ||
* \see QgsIntRange | ||
* \see QgsDateRange | ||
* \see QgsDateTimeRange | ||
*/ | ||
typedef QgsRange< double > QgsDoubleRange; | ||
|
||
/** | ||
* QgsRange which stores a range of integer values. | ||
* \since QGIS 3.0 | ||
* \see QgsDoubleRange | ||
* \see QgsDateRange | ||
* \see QgsDateTimeRange | ||
*/ | ||
typedef QgsRange< int > QgsIntRange; | ||
|
||
|
||
// specialization required to handle invalid QDate bounds | ||
template<> | ||
bool QgsRange<QDate>::isEmpty() const | ||
{ | ||
if ( !mLower.isValid() && !mUpper.isValid() ) | ||
return true; | ||
|
||
if ( mLower.isValid() != mUpper.isValid() ) | ||
return false; | ||
|
||
if ( mLower > mUpper ) | ||
return true; | ||
|
||
if ( mLower == mUpper && !( mIncludeLower || mIncludeUpper ) ) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
template<> | ||
bool QgsRange<QDate>::contains( const QgsRange<QDate> &other ) const | ||
{ | ||
if ( !other.mLower.isValid() && mLower.isValid() ) | ||
return false; | ||
|
||
if ( mLower.isValid() ) | ||
{ | ||
bool lowerOk = ( mIncludeLower && mLower <= other.mLower ) | ||
|| ( !mIncludeLower && mLower < other.mLower ) | ||
|| ( !mIncludeLower && !other.mIncludeLower && mLower <= other.mLower ); | ||
if ( !lowerOk ) | ||
return false; | ||
} | ||
|
||
if ( !other.mUpper.isValid() && mUpper.isValid() ) | ||
return false; | ||
|
||
if ( mUpper.isValid() ) | ||
{ | ||
bool upperOk = ( mIncludeUpper && mUpper >= other.mUpper ) | ||
|| ( !mIncludeUpper && mUpper > other.mUpper ) | ||
|| ( !mIncludeUpper && !other.mIncludeUpper && mUpper >= other.mUpper ); | ||
if ( !upperOk ) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
template<> | ||
bool QgsRange<QDate>::contains( QDate element ) const | ||
{ | ||
if ( !element.isValid() ) | ||
return false; | ||
|
||
if ( mLower.isValid() ) | ||
{ | ||
bool lowerOk = ( mIncludeLower && mLower <= element ) | ||
|| ( !mIncludeLower && mLower < element ); | ||
if ( !lowerOk ) | ||
return false; | ||
} | ||
|
||
if ( mUpper.isValid() ) | ||
{ | ||
bool upperOk = ( mIncludeUpper && mUpper >= element ) | ||
|| ( !mIncludeUpper && mUpper > element ); | ||
if ( !upperOk ) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
template<> | ||
bool QgsRange<QDate>::overlaps( const QgsRange<QDate> &other ) const | ||
{ | ||
if ( !mUpper.isValid() && ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) ) ) | ||
return true; | ||
|
||
if ( ( ( mIncludeLower && mLower <= other.mLower ) || ( !mIncludeLower && mLower < other.mLower ) ) | ||
&& ( ( mIncludeUpper && mUpper >= other.mUpper ) || ( !mIncludeUpper && mUpper > other.mUpper ) ) ) | ||
return true; | ||
|
||
if ( ( ( mIncludeLower && mLower <= other.mLower ) || ( !mIncludeLower && mLower < other.mLower ) ) | ||
&& ( ( mIncludeUpper && mUpper >= other.mLower ) || ( !mIncludeUpper && mUpper > other.mLower ) ) ) | ||
return true; | ||
|
||
if ( ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) ) | ||
&& ( ( mIncludeUpper && mUpper >= other.mUpper ) || ( !mIncludeUpper && mUpper > other.mUpper ) ) ) | ||
return true; | ||
|
||
if ( ( ( mIncludeLower && mLower >= other.mLower ) || ( !mIncludeLower && mLower > other.mLower ) ) | ||
&& ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) ) ) | ||
return true; | ||
|
||
if ( mLower == other.mLower && mUpper == other.mUpper ) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* QgsRange which stores a range of dates. | ||
* | ||
* Invalid QDates as the lower or upper bound are permitted. In this case, | ||
* the bound is considered to be infinite. E.g. QgsDateRange(QDate(),QDate(2017,1,1)) | ||
* is treated as a range containing all dates before 2017-1-1. | ||
* QgsDateRange(QDate(2017,1,1),QDate()) is treated as a range containing all dates after 2017-1-1. | ||
* \since QGIS 3.0 | ||
* \see QgsIntRange | ||
* \see QgsDoubleRange | ||
* \see QgsDateTimeRange | ||
*/ | ||
typedef QgsRange< QDate > QgsDateRange; | ||
|
||
|
||
|
||
#endif // QGSRANGE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.