|
| 1 | +/*************************************************************************** |
| 2 | + qgsrange.h |
| 3 | + ---------- |
| 4 | + begin : April 2017 |
| 5 | + copyright : (C) 2017 by Nyall Dawson |
| 6 | + email : nyall dot dawson at gmail dot com |
| 7 | + ***************************************************************************/ |
| 8 | + |
| 9 | +/*************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | + |
| 18 | +#ifndef QGSRANGE_H |
| 19 | +#define QGSRANGE_H |
| 20 | + |
| 21 | +#include "qgis.h" |
| 22 | +#include "qgis_core.h" |
| 23 | + |
| 24 | +/** |
| 25 | + * \class QgsRange |
| 26 | + * \ingroup core |
| 27 | + * A template based class for storing ranges (lower to upper values). |
| 28 | + * |
| 29 | + * QgsRange classes represent a range of values of some element type. For instance, |
| 30 | + * ranges of QDateTime might be used to represent the ranges of timestamp ranges. |
| 31 | + * |
| 32 | + * Ranges can indicate whether the upper and lower values are inclusive or exclusive. |
| 33 | + * The inclusivity or exclusivity of bounds is considered when determining things like |
| 34 | + * whether ranges overlap or during calculation of range intersections. |
| 35 | + * |
| 36 | + * \since QGIS 3.0 |
| 37 | + * \see QgsDoubleRange |
| 38 | + * \see QgsIntRange |
| 39 | + * \note not available in Python bindings |
| 40 | + */ |
| 41 | +template <class T> class CORE_EXPORT QgsRange |
| 42 | +{ |
| 43 | + public: |
| 44 | + |
| 45 | + /** |
| 46 | + * Constructor for QgsRange. The \a lower and \a upper bounds are specified, |
| 47 | + * and optionally whether or not these bounds are included in the range. |
| 48 | + */ |
| 49 | + QgsRange( T lower, T upper, bool includeLower = true, bool includeUpper = true ) |
| 50 | + : mLower( lower ) |
| 51 | + , mUpper( upper ) |
| 52 | + , mIncludeLower( includeLower ) |
| 53 | + , mIncludeUpper( includeUpper ) |
| 54 | + {} |
| 55 | + |
| 56 | + /** |
| 57 | + * Returns the lower bound of the range. |
| 58 | + * \see upper() |
| 59 | + * \see includeLower() |
| 60 | + */ |
| 61 | + T lower() const { return mLower; } |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns the upper bound of the range. |
| 65 | + * \see lower() |
| 66 | + * \see includeUpper() |
| 67 | + */ |
| 68 | + T upper() const { return mUpper; } |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns true if the lower bound is inclusive, or false if the lower |
| 72 | + * bound is exclusive. |
| 73 | + * \see lower() |
| 74 | + * \see includeUpper() |
| 75 | + */ |
| 76 | + bool includeLower() const { return mIncludeLower; } |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns true if the upper bound is inclusive, or false if the upper |
| 80 | + * bound is exclusive. |
| 81 | + * \see upper() |
| 82 | + * \see includeLower() |
| 83 | + */ |
| 84 | + bool includeUpper() const { return mIncludeUpper; } |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns true if the range is empty, ie the lower bound equals (or exceeds) the upper bound |
| 88 | + * and either the bounds are exclusive. |
| 89 | + */ |
| 90 | + bool isEmpty() const { return mLower > mUpper || ( mUpper == mLower && !( mIncludeLower || mIncludeUpper ) ); } |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns true if this range contains another range. |
| 94 | + * \see overlaps() |
| 95 | + */ |
| 96 | + bool contains( const QgsRange<T> &other ) const |
| 97 | + { |
| 98 | + bool lowerOk = ( mIncludeLower && mLower <= other.mLower ) |
| 99 | + || ( !mIncludeLower && mLower < other.mLower ) |
| 100 | + || ( !mIncludeLower && !other.mIncludeLower && mLower <= other.mLower ); |
| 101 | + if ( !lowerOk ) |
| 102 | + return false; |
| 103 | + |
| 104 | + bool upperOk = ( mIncludeUpper && mUpper >= other.mUpper ) |
| 105 | + || ( !mIncludeUpper && mUpper > other.mUpper ) |
| 106 | + || ( !mIncludeUpper && !other.mIncludeUpper && mUpper >= other.mUpper ); |
| 107 | + if ( !upperOk ) |
| 108 | + return false; |
| 109 | + |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Returns true if this range contains a specified \a element. |
| 115 | + */ |
| 116 | + bool contains( T element ) const |
| 117 | + { |
| 118 | + bool lowerOk = ( mIncludeLower && mLower <= element ) |
| 119 | + || ( !mIncludeLower && mLower < element ); |
| 120 | + if ( !lowerOk ) |
| 121 | + return false; |
| 122 | + |
| 123 | + bool upperOk = ( mIncludeUpper && mUpper >= element ) |
| 124 | + || ( !mIncludeUpper && mUpper > element ); |
| 125 | + if ( !upperOk ) |
| 126 | + return false; |
| 127 | + |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Returns true if this range overlaps another range. |
| 133 | + * \see contains() |
| 134 | + */ |
| 135 | + bool overlaps( const QgsRange<T> &other ) const |
| 136 | + { |
| 137 | + if ( ( ( mIncludeLower && mLower <= other.mLower ) || ( !mIncludeLower && mLower < other.mLower ) ) |
| 138 | + && ( ( mIncludeUpper && mUpper >= other.mUpper ) || ( !mIncludeUpper && mUpper > other.mUpper ) ) ) |
| 139 | + return true; |
| 140 | + |
| 141 | + if ( ( ( mIncludeLower && mLower <= other.mLower ) || ( !mIncludeLower && mLower < other.mLower ) ) |
| 142 | + && ( ( mIncludeUpper && mUpper >= other.mLower ) || ( !mIncludeUpper && mUpper > other.mLower ) ) ) |
| 143 | + return true; |
| 144 | + |
| 145 | + if ( ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) ) |
| 146 | + && ( ( mIncludeUpper && mUpper >= other.mUpper ) || ( !mIncludeUpper && mUpper > other.mUpper ) ) ) |
| 147 | + return true; |
| 148 | + |
| 149 | + if ( ( ( mIncludeLower && mLower >= other.mLower ) || ( !mIncludeLower && mLower > other.mLower ) ) |
| 150 | + && ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) ) ) |
| 151 | + return true; |
| 152 | + |
| 153 | + if ( mLower == other.mLower && mUpper == other.mUpper ) |
| 154 | + return true; |
| 155 | + |
| 156 | + return false; |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | + private: |
| 161 | + |
| 162 | + T mLower; |
| 163 | + T mUpper; |
| 164 | + bool mIncludeLower = true; |
| 165 | + bool mIncludeUpper = true; |
| 166 | + |
| 167 | +}; |
| 168 | + |
| 169 | + |
| 170 | +/** |
| 171 | + * QgsRange which stores a range of double values. |
| 172 | + * \since QGIS 3.0 |
| 173 | + * \see QgsIntRange |
| 174 | + * \see QgsDateRange |
| 175 | + * \see QgsDateTimeRange |
| 176 | + */ |
| 177 | +typedef QgsRange< double > QgsDoubleRange; |
| 178 | + |
| 179 | +/** |
| 180 | + * QgsRange which stores a range of integer values. |
| 181 | + * \since QGIS 3.0 |
| 182 | + * \see QgsDoubleRange |
| 183 | + * \see QgsDateRange |
| 184 | + * \see QgsDateTimeRange |
| 185 | + */ |
| 186 | +typedef QgsRange< int > QgsIntRange; |
| 187 | + |
| 188 | + |
| 189 | +// specialization required to handle invalid QDate bounds |
| 190 | +template<> |
| 191 | +bool QgsRange<QDate>::isEmpty() const |
| 192 | +{ |
| 193 | + if ( !mLower.isValid() && !mUpper.isValid() ) |
| 194 | + return true; |
| 195 | + |
| 196 | + if ( mLower.isValid() != mUpper.isValid() ) |
| 197 | + return false; |
| 198 | + |
| 199 | + if ( mLower > mUpper ) |
| 200 | + return true; |
| 201 | + |
| 202 | + if ( mLower == mUpper && !( mIncludeLower || mIncludeUpper ) ) |
| 203 | + return true; |
| 204 | + |
| 205 | + return false; |
| 206 | +} |
| 207 | + |
| 208 | +template<> |
| 209 | +bool QgsRange<QDate>::contains( const QgsRange<QDate> &other ) const |
| 210 | +{ |
| 211 | + if ( !other.mLower.isValid() && mLower.isValid() ) |
| 212 | + return false; |
| 213 | + |
| 214 | + if ( mLower.isValid() ) |
| 215 | + { |
| 216 | + bool lowerOk = ( mIncludeLower && mLower <= other.mLower ) |
| 217 | + || ( !mIncludeLower && mLower < other.mLower ) |
| 218 | + || ( !mIncludeLower && !other.mIncludeLower && mLower <= other.mLower ); |
| 219 | + if ( !lowerOk ) |
| 220 | + return false; |
| 221 | + } |
| 222 | + |
| 223 | + if ( !other.mUpper.isValid() && mUpper.isValid() ) |
| 224 | + return false; |
| 225 | + |
| 226 | + if ( mUpper.isValid() ) |
| 227 | + { |
| 228 | + bool upperOk = ( mIncludeUpper && mUpper >= other.mUpper ) |
| 229 | + || ( !mIncludeUpper && mUpper > other.mUpper ) |
| 230 | + || ( !mIncludeUpper && !other.mIncludeUpper && mUpper >= other.mUpper ); |
| 231 | + if ( !upperOk ) |
| 232 | + return false; |
| 233 | + } |
| 234 | + |
| 235 | + return true; |
| 236 | +} |
| 237 | + |
| 238 | +template<> |
| 239 | +bool QgsRange<QDate>::contains( QDate element ) const |
| 240 | +{ |
| 241 | + if ( !element.isValid() ) |
| 242 | + return false; |
| 243 | + |
| 244 | + if ( mLower.isValid() ) |
| 245 | + { |
| 246 | + bool lowerOk = ( mIncludeLower && mLower <= element ) |
| 247 | + || ( !mIncludeLower && mLower < element ); |
| 248 | + if ( !lowerOk ) |
| 249 | + return false; |
| 250 | + } |
| 251 | + |
| 252 | + if ( mUpper.isValid() ) |
| 253 | + { |
| 254 | + bool upperOk = ( mIncludeUpper && mUpper >= element ) |
| 255 | + || ( !mIncludeUpper && mUpper > element ); |
| 256 | + if ( !upperOk ) |
| 257 | + return false; |
| 258 | + } |
| 259 | + |
| 260 | + return true; |
| 261 | +} |
| 262 | + |
| 263 | +template<> |
| 264 | +bool QgsRange<QDate>::overlaps( const QgsRange<QDate> &other ) const |
| 265 | +{ |
| 266 | + if ( !mUpper.isValid() && ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) ) ) |
| 267 | + return true; |
| 268 | + |
| 269 | + if ( ( ( mIncludeLower && mLower <= other.mLower ) || ( !mIncludeLower && mLower < other.mLower ) ) |
| 270 | + && ( ( mIncludeUpper && mUpper >= other.mUpper ) || ( !mIncludeUpper && mUpper > other.mUpper ) ) ) |
| 271 | + return true; |
| 272 | + |
| 273 | + if ( ( ( mIncludeLower && mLower <= other.mLower ) || ( !mIncludeLower && mLower < other.mLower ) ) |
| 274 | + && ( ( mIncludeUpper && mUpper >= other.mLower ) || ( !mIncludeUpper && mUpper > other.mLower ) ) ) |
| 275 | + return true; |
| 276 | + |
| 277 | + if ( ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) ) |
| 278 | + && ( ( mIncludeUpper && mUpper >= other.mUpper ) || ( !mIncludeUpper && mUpper > other.mUpper ) ) ) |
| 279 | + return true; |
| 280 | + |
| 281 | + if ( ( ( mIncludeLower && mLower >= other.mLower ) || ( !mIncludeLower && mLower > other.mLower ) ) |
| 282 | + && ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) ) ) |
| 283 | + return true; |
| 284 | + |
| 285 | + if ( mLower == other.mLower && mUpper == other.mUpper ) |
| 286 | + return true; |
| 287 | + |
| 288 | + return false; |
| 289 | +} |
| 290 | + |
| 291 | +/** |
| 292 | + * QgsRange which stores a range of dates. |
| 293 | + * |
| 294 | + * Invalid QDates as the lower or upper bound are permitted. In this case, |
| 295 | + * the bound is considered to be infinite. E.g. QgsDateRange(QDate(),QDate(2017,1,1)) |
| 296 | + * is treated as a range containing all dates before 2017-1-1. |
| 297 | + * QgsDateRange(QDate(2017,1,1),QDate()) is treated as a range containing all dates after 2017-1-1. |
| 298 | + * \since QGIS 3.0 |
| 299 | + * \see QgsIntRange |
| 300 | + * \see QgsDoubleRange |
| 301 | + * \see QgsDateTimeRange |
| 302 | + */ |
| 303 | +typedef QgsRange< QDate > QgsDateRange; |
| 304 | + |
| 305 | + |
| 306 | + |
| 307 | +#endif // QGSRANGE_H |
0 commit comments