Add extend to QgsRange

This commit is contained in:
Alessandro Pasotti 2019-10-20 14:04:04 +02:00 committed by Nyall Dawson
parent 9432ac8af1
commit 65d0f5b67f
3 changed files with 109 additions and 4 deletions

View File

@ -9,6 +9,7 @@
template <T>
class QgsRange
{
@ -116,7 +117,6 @@ Returns ``True`` if this range overlaps another range.
.. seealso:: :py:func:`contains`
%End
};
@ -241,6 +241,19 @@ Returns ``True`` if this range contains a specified ``element``.
bool overlaps( const QgsTemporalRange<T> &other ) const;
%Docstring
Returns ``True`` if this range overlaps another range.
%End
bool extend( const QgsTemporalRange<T> &other );
%Docstring
Extends the range in place by adding an ``other`` range.
If ``other`` is empty the range is not changed.
If the range is empty and ``other`` is not, the range is changed and set to ``other``.
.. seealso:: :py:func:`isEmpty`
.. versionadded:: 3.12
:return: ``True`` if the range was extended
%End
bool operator==( const QgsTemporalRange<T> &other ) const;

View File

@ -21,6 +21,9 @@
#include "qgis_sip.h"
#include "qgis_core.h"
#include <QDate>
#include <QDateTime>
/**
* \class QgsRange
* \ingroup core
@ -164,7 +167,6 @@ class QgsRange
return false;
}
private:
T mLower;
@ -392,12 +394,66 @@ class QgsTemporalRange
return false;
}
/**
* Extends the range in place by adding an \a other range.
* If \a other is empty the range is not changed.
* If the range is empty and \a other is not, the range is changed and set to \a other.
* \see isEmpty()
* \since QGIS 3.12
* \return TRUE if the range was extended
*/
bool extend( const QgsTemporalRange<T> &other )
{
if ( other.isEmpty() || other.isInfinite() )
{
return false;
}
else if ( isEmpty() || isInfinite() )
{
mLower = other.begin();
mUpper = other.end();
mIncludeLower = other.includeBeginning();
mIncludeUpper = other.includeEnd();
return true;
}
// Both not empty, do some math
bool changed { false };
// Lower
if ( other.begin() < mLower )
{
mLower = other.begin();
mIncludeLower = other.includeBeginning();
changed = true;
}
else if ( other.begin() == mLower && other.includeBeginning() && ! mIncludeLower )
{
mIncludeLower = true;
changed = true;
}
// Upper
if ( other.end() > mUpper )
{
mUpper = other.end();
mIncludeUpper = other.includeEnd();
changed = true;
}
else if ( other.end() == mUpper && other.includeEnd() && ! mIncludeUpper )
{
mIncludeUpper = true;
changed = true;
}
return changed;
}
bool operator==( const QgsTemporalRange<T> &other ) const
{
return mLower == other.mLower &&
mUpper == other.mUpper &&
mIncludeLower == other.mIncludeLower &&
mIncludeUpper == other.mIncludeUpper;
mIncludeLower == other.includeBeginning() &&
mIncludeUpper == other.includeEnd();
}
private:

View File

@ -348,6 +348,42 @@ class TestQgsDateRange(unittest.TestCase):
self.assertNotEqual(range, QgsDateRange(QDate(2010, 3, 1), QDate(2010, 6, 3), False, False))
self.assertNotEqual(range, QgsDateRange(QDate(2010, 3, 2), QDate(2010, 6, 2), False, False))
def testExtend(self):
range_empty = QgsDateRange(QDate(2010, 6, 2), QDate(2010, 3, 1))
# Empty
self.assertFalse(range_empty.extend(range_empty))
range = QgsDateRange(QDate(2010, 3, 1), QDate(2010, 6, 2), False, False)
self.assertFalse(range.extend(range_empty))
range = QgsDateRange(QDate(2010, 6, 2), QDate(2010, 3, 1))
self.assertTrue(range.extend(QgsDateRange(QDate(2010, 3, 1), QDate(2010, 6, 2), False, False)))
self.assertEqual(range, QgsDateRange(QDate(2010, 3, 1), QDate(2010, 6, 2), False, False))
# Extend low
range = QgsDateRange(QDate(2010, 3, 1), QDate(2010, 6, 2), False, False)
self.assertTrue(range.extend(QgsDateRange(QDate(2010, 2, 1), QDate(2010, 6, 2), False, False)))
self.assertEqual(range, QgsDateRange(QDate(2010, 2, 1), QDate(2010, 6, 2), False, False))
range = QgsDateRange(QDate(2010, 3, 1), QDate(2010, 6, 2), False, False)
self.assertTrue(range.extend(QgsDateRange(QDate(2010, 2, 1), QDate(2010, 5, 2), True, False)))
self.assertEqual(range, QgsDateRange(QDate(2010, 2, 1), QDate(2010, 6, 2), True, False))
# Extend high
range = QgsDateRange(QDate(2010, 3, 1), QDate(2010, 6, 2), False, False)
self.assertTrue(range.extend(QgsDateRange(QDate(2010, 3, 1), QDate(2010, 7, 2), False, False)))
self.assertEqual(range, QgsDateRange(QDate(2010, 3, 1), QDate(2010, 7, 2), False, False))
range = QgsDateRange(QDate(2010, 3, 1), QDate(2010, 6, 2), False, False)
self.assertTrue(range.extend(QgsDateRange(QDate(2010, 3, 1), QDate(2010, 6, 2), False, True)))
self.assertEqual(range, QgsDateRange(QDate(2010, 3, 1), QDate(2010, 6, 2), False, True))
# Extend both
range = QgsDateRange(QDate(2010, 3, 1), QDate(2010, 6, 2), False, False)
self.assertTrue(range.extend(QgsDateRange(QDate(2010, 2, 1), QDate(2010, 7, 2), False, False)))
self.assertEqual(range, QgsDateRange(QDate(2010, 2, 1), QDate(2010, 7, 2), False, False))
# Extend none
range = QgsDateRange(QDate(2010, 3, 1), QDate(2010, 6, 2), False, False)
self.assertFalse(range.extend(QgsDateRange(QDate(2010, 4, 6), QDate(2010, 5, 2), False, False)))
if __name__ == "__main__":
unittest.main()