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