QGIS API Documentation  3.6.0-Noosa (5873452)
qgsrange.h
Go to the documentation of this file.
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_sip.h"
22 #include "qgis_core.h"
23 
41 template <typename T>
42 class QgsRange
43 {
44  public:
45 
50  QgsRange( T lower, T upper, bool includeLower = true, bool includeUpper = true )
51  : mLower( lower )
52  , mUpper( upper )
53  , mIncludeLower( includeLower )
54  , mIncludeUpper( includeUpper )
55  {}
56 
62  T lower() const { return mLower; }
63 
69  T upper() const { return mUpper; }
70 
77  bool includeLower() const { return mIncludeLower; }
78 
85  bool includeUpper() const { return mIncludeUpper; }
86 
92  bool isEmpty() const { return mLower > mUpper || ( mUpper == mLower && !( mIncludeLower || mIncludeUpper ) ); }
93 
98  bool isSingleton() const { return mLower == mUpper && ( mIncludeLower || mIncludeUpper ); }
99 
104  bool contains( const QgsRange<T> &other ) const
105  {
106  bool lowerOk = ( mIncludeLower && mLower <= other.mLower )
107  || ( !mIncludeLower && mLower < other.mLower )
108  || ( !mIncludeLower && !other.mIncludeLower && mLower <= other.mLower );
109  if ( !lowerOk )
110  return false;
111 
112  bool upperOk = ( mIncludeUpper && mUpper >= other.mUpper )
113  || ( !mIncludeUpper && mUpper > other.mUpper )
114  || ( !mIncludeUpper && !other.mIncludeUpper && mUpper >= other.mUpper );
115  if ( !upperOk )
116  return false;
117 
118  return true;
119  }
120 
124  bool contains( T element ) const
125  {
126  bool lowerOk = ( mIncludeLower && mLower <= element )
127  || ( !mIncludeLower && mLower < element );
128  if ( !lowerOk )
129  return false;
130 
131  bool upperOk = ( mIncludeUpper && mUpper >= element )
132  || ( !mIncludeUpper && mUpper > element );
133  if ( !upperOk )
134  return false;
135 
136  return true;
137  }
138 
143  bool overlaps( const QgsRange<T> &other ) const
144  {
145  if ( ( ( mIncludeLower && mLower <= other.mLower ) || ( !mIncludeLower && mLower < other.mLower ) )
146  && ( ( mIncludeUpper && mUpper >= other.mUpper ) || ( !mIncludeUpper && mUpper > other.mUpper ) ) )
147  return true;
148 
149  if ( ( ( mIncludeLower && mLower <= other.mLower ) || ( !mIncludeLower && mLower < other.mLower ) )
150  && ( ( mIncludeUpper && mUpper >= other.mLower ) || ( !mIncludeUpper && mUpper > other.mLower ) ) )
151  return true;
152 
153  if ( ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) )
154  && ( ( mIncludeUpper && mUpper >= other.mUpper ) || ( !mIncludeUpper && mUpper > other.mUpper ) ) )
155  return true;
156 
157  if ( ( ( mIncludeLower && mLower >= other.mLower ) || ( !mIncludeLower && mLower > other.mLower ) )
158  && ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) ) )
159  return true;
160 
161  if ( mLower == other.mLower && mUpper == other.mUpper )
162  return true;
163 
164  return false;
165  }
166 
167 
168  private:
169 
170  T mLower;
171  T mUpper;
172  bool mIncludeLower = true;
173  bool mIncludeUpper = true;
174 
175 };
176 
177 
186 
187 
188 
189 
198 
199 
216 template <typename T>
218 {
219  public:
220 
226 #ifndef SIP_RUN
227  QgsTemporalRange( const T &begin = T(), const T &end = T(), bool includeBeginning = true, bool includeEnd = true )
228  : mLower( begin )
229  , mUpper( end )
230  , mIncludeLower( includeBeginning )
231  , mIncludeUpper( includeEnd )
232  {}
233 #else
234  QgsTemporalRange( const T &begin, const T &end, bool includeBeginning = true, bool includeEnd = true );
235  // default constructor as default value for templates is not handled in SIP
236 #endif
237 
243  T begin() const { return mLower; }
244 
250  T end() const { return mUpper; }
251 
258  bool includeBeginning() const { return mIncludeLower; }
259 
265  bool includeEnd() const { return mIncludeUpper; }
266 
272  bool isInstant() const { return mLower.isValid() && mUpper.isValid() && mLower == mUpper && ( mIncludeLower || mIncludeUpper ); }
273 
279  bool isInfinite() const
280  {
281  return !mLower.isValid() && !mUpper.isValid();
282  }
283 
289  bool isEmpty() const
290  {
291  if ( !mLower.isValid() && !mUpper.isValid() )
292  return false;
293 
294  if ( mLower.isValid() != mUpper.isValid() )
295  return false;
296 
297  if ( mLower > mUpper )
298  return true;
299 
300  if ( mLower == mUpper && !( mIncludeLower || mIncludeUpper ) )
301  return true;
302 
303  return false;
304  }
305 
309  bool contains( const QgsTemporalRange<T> &other ) const
310  {
311  if ( !other.mLower.isValid() && mLower.isValid() )
312  return false;
313 
314  if ( mLower.isValid() )
315  {
316  bool lowerOk = ( mIncludeLower && mLower <= other.mLower )
317  || ( !mIncludeLower && mLower < other.mLower )
318  || ( !mIncludeLower && !other.mIncludeLower && mLower <= other.mLower );
319  if ( !lowerOk )
320  return false;
321  }
322 
323  if ( !other.mUpper.isValid() && mUpper.isValid() )
324  return false;
325 
326  if ( mUpper.isValid() )
327  {
328  bool upperOk = ( mIncludeUpper && mUpper >= other.mUpper )
329  || ( !mIncludeUpper && mUpper > other.mUpper )
330  || ( !mIncludeUpper && !other.mIncludeUpper && mUpper >= other.mUpper );
331  if ( !upperOk )
332  return false;
333  }
334 
335  return true;
336  }
337 
341  bool contains( const T &element ) const
342  {
343  if ( !element.isValid() )
344  return false;
345 
346  if ( mLower.isValid() )
347  {
348  bool lowerOk = ( mIncludeLower && mLower <= element )
349  || ( !mIncludeLower && mLower < element );
350  if ( !lowerOk )
351  return false;
352  }
353 
354  if ( mUpper.isValid() )
355  {
356  bool upperOk = ( mIncludeUpper && mUpper >= element )
357  || ( !mIncludeUpper && mUpper > element );
358  if ( !upperOk )
359  return false;
360  }
361 
362  return true;
363  }
364 
368  bool overlaps( const QgsTemporalRange<T> &other ) const
369  {
370  if ( !mUpper.isValid() && ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) ) )
371  return true;
372 
373  if ( ( ( mIncludeLower && mLower <= other.mLower ) || ( !mIncludeLower && mLower < other.mLower ) )
374  && ( ( mIncludeUpper && mUpper >= other.mUpper ) || ( !mIncludeUpper && mUpper > other.mUpper ) ) )
375  return true;
376 
377  if ( ( ( mIncludeLower && mLower <= other.mLower ) || ( !mIncludeLower && mLower < other.mLower ) )
378  && ( ( mIncludeUpper && mUpper >= other.mLower ) || ( !mIncludeUpper && mUpper > other.mLower ) ) )
379  return true;
380 
381  if ( ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) )
382  && ( ( mIncludeUpper && mUpper >= other.mUpper ) || ( !mIncludeUpper && mUpper > other.mUpper ) ) )
383  return true;
384 
385  if ( ( ( mIncludeLower && mLower >= other.mLower ) || ( !mIncludeLower && mLower > other.mLower ) )
386  && ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) ) )
387  return true;
388 
389  if ( mLower == other.mLower && mUpper == other.mUpper )
390  return true;
391 
392  return false;
393  }
394 
395  bool operator==( const QgsTemporalRange<T> &other ) const
396  {
397  return mLower == other.mLower &&
398  mUpper == other.mUpper &&
399  mIncludeLower == other.mIncludeLower &&
400  mIncludeUpper == other.mIncludeUpper;
401  }
402 
403  private:
404 
405  T mLower;
406  T mUpper;
407  bool mIncludeLower = true;
408  bool mIncludeUpper = true;
409 };
410 
411 
423 
434 typedef QgsTemporalRange< QDateTime > QgsDateTimeRange SIP_DOC_TEMPLATE;
435 
436 #endif // QGSRANGE_H
bool contains(const QgsTemporalRange< T > &other) const
Returns true if this range contains another range.
Definition: qgsrange.h:309
bool includeEnd() const
Returns true if the end is inclusive, or false if the end is exclusive.
Definition: qgsrange.h:265
bool isInfinite() const
Returns true if the range consists of all possible values.
Definition: qgsrange.h:279
A template based class for storing temporal ranges (beginning to end values).
Definition: qgsrange.h:217
bool overlaps(const QgsRange< T > &other) const
Returns true if this range overlaps another range.
Definition: qgsrange.h:143
T lower() const
Returns the lower bound of the range.
Definition: qgsrange.h:62
QgsTemporalRange(const T &begin=T(), const T &end=T(), bool includeBeginning=true, bool includeEnd=true)
Constructor for QgsTemporalRange.
Definition: qgsrange.h:227
bool isEmpty() const
Returns true if the range is empty, ie the lower bound equals (or exceeds) the upper bound and either...
Definition: qgsrange.h:92
bool contains(const T &element) const
Returns true if this range contains a specified element.
Definition: qgsrange.h:341
T begin() const
Returns the beginning of the range.
Definition: qgsrange.h:243
bool isEmpty() const
Returns true if the range is empty, ie the beginning equals (or exceeds) the end and either of the bo...
Definition: qgsrange.h:289
QgsRange< double > QgsDoubleRange
QgsRange which stores a range of double values.
Definition: qgsrange.h:185
bool isSingleton() const
Returns true if the range consists only of a single value or instant.
Definition: qgsrange.h:98
bool contains(const QgsRange< T > &other) const
Returns true if this range contains another range.
Definition: qgsrange.h:104
bool isInstant() const
Returns true if the range consists only of a single instant.
Definition: qgsrange.h:272
bool operator==(const QgsTemporalRange< T > &other) const
Definition: qgsrange.h:395
bool overlaps(const QgsTemporalRange< T > &other) const
Returns true if this range overlaps another range.
Definition: qgsrange.h:368
bool includeUpper() const
Returns true if the upper bound is inclusive, or false if the upper bound is exclusive.
Definition: qgsrange.h:85
bool includeBeginning() const
Returns true if the beginning is inclusive, or false if the beginning is exclusive.
Definition: qgsrange.h:258
bool includeLower() const
Returns true if the lower bound is inclusive, or false if the lower bound is exclusive.
Definition: qgsrange.h:77
QgsTemporalRange< QDate > QgsDateRange SIP_DOC_TEMPLATE
QgsRange which stores a range of dates.
Definition: qgsrange.h:422
T upper() const
Returns the upper bound of the range.
Definition: qgsrange.h:69
QgsRange(T lower, T upper, bool includeLower=true, bool includeUpper=true)
Constructor for QgsRange.
Definition: qgsrange.h:50
A template based class for storing ranges (lower to upper values).
Definition: qgsrange.h:42
QgsRange< int > QgsIntRange
QgsRange which stores a range of integer values.
Definition: qgsrange.h:197
bool contains(T element) const
Returns true if this range contains a specified element.
Definition: qgsrange.h:124
T end() const
Returns the upper bound of the range.
Definition: qgsrange.h:250