scidavis  2.3.0
About: SciDAVis is a free application for Scientific Data Analysis and Visualization (a fork off of QtiPlot).
  Fossies Dox: scidavis-2.3.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Interval.h
Go to the documentation of this file.
1 
12 
31 #ifndef INTERVAL_H
32 #define INTERVAL_H
33 
34 #include <QList>
35 #include <QString>
36 #include <QtGlobal>
37 
38 // forward declaration
39 template<class T> class Interval;
40 
41 template<class T> class IntervalBase
42 {
43  public:
44  IntervalBase() : d_start(-1), d_end(-1){}
45  IntervalBase(const IntervalBase<T>& other) {
46  d_start = other.start();
47  d_end = other.end();
48  }
50  d_start = start;
51  d_end = end;
52  }
53  virtual ~IntervalBase() {}
54  T start() const { return d_start; }
55  T end() const { return d_end; }
56  void setStart(T start) { d_start = start; }
57  void setEnd(T end) { d_end = end; }
58  bool contains(const Interval<T>& other) const { return ( d_start <= other.start() && d_end >= other.end() ); }
59  bool contains(T value) const { return ( d_start <= value && d_end >= value ); }
60  bool intersects(const Interval<T>& other) const { return ( contains(other.start()) || contains(other.end()) ); }
62 
65  static Interval<T> intersection(const Interval<T>& first, const Interval<T>& second)
66  {
67  return Interval<T>( qMax(first.start(), second.start()), qMin(first.end(), second.end()) );
68  }
69  void translate(T offset) { d_start += offset; d_end += offset; }
70  bool operator==(const Interval<T>& other) const { return ( d_start == other.start() && d_end == other.end() ); }
72  d_start = other.start();
73  d_end = other.end();
74  return *this;
75  }
77  virtual bool touches(const Interval<T>& other) const = 0;
79  static Interval<T> merge(const Interval<T>& a, const Interval<T>& b) {
80  if( !(a.intersects(b) || a.touches(b)) )
81  return a;
82  return Interval<T>( qMin(a.start(), b.start()), qMax(a.end(), b.end()) );
83  }
85  static QList< Interval<T> > subtract(const Interval<T>& src_iv, const Interval<T>& minus_iv) {
86  QList< Interval<T> > list;
87  if( (src_iv == minus_iv) || (minus_iv.contains(src_iv)) )
88  return list;
89 
90  if( !src_iv.intersects(minus_iv) )
91  list.append(src_iv);
92  else if( src_iv.end() <= minus_iv.end() )
93  list.append( Interval<T>(src_iv.start(), minus_iv.start()-1) );
94  else if( src_iv.start() >= minus_iv.start() )
95  list.append( Interval<T>(minus_iv.end()+1, src_iv.end()) );
96  else {
97  list.append( Interval<T>(src_iv.start(), minus_iv.start()-1) );
98  list.append( Interval<T>(minus_iv.end()+1, src_iv.end()) );
99  }
100 
101  return list;
102  }
104  static QList< Interval<T> > split(const Interval<T>& i, T before) {
105  QList< Interval<T> > list;
106  if( before < i.start() || before > i.end() )
107  {
108  list.append(i);
109  }
110  else
111  {
112  Interval<T> left(i.start(), before-1);
113  Interval<T> right(before, i.end());
114  if(left.isValid())
115  list.append(left);
116  if(right.isValid())
117  list.append(right);
118  }
119  return list;
120  }
122  /*
123  * This function merges all intervals in the list until none of them
124  * intersect or touch anymore.
125  */
126  static void mergeIntervalIntoList(QList< Interval<T> > * list, Interval<T> i) {
127  for(int c=0; c<list->size(); c++)
128  {
129  if( list->at(c).touches(i) || list->at(c).intersects(i) )
130  {
131  Interval<T> result = merge(list->takeAt(c), i);
132  mergeIntervalIntoList(list, result);
133  return;
134  }
135  }
136  list->append(i);
137  }
139 
142  static void restrictList(QList< Interval<T> > * list, Interval<T> i)
143  {
144  Interval<T> temp;
145  for(int c=0; c<list->size(); c++)
146  {
147  temp = intersection(list->at(c), i);
148  if(!temp.isValid())
149  list->removeAt(c--);
150  else
151  list->replace(c, temp);
152  }
153 
154  }
156 
159  static void subtractIntervalFromList(QList< Interval<T> > * list, Interval<T> i) {
160  QList< Interval<T> > temp_list;
161  for(int c=0; c<list->size(); c++)
162  {
163  temp_list = subtract(list->at(c), i);
164  if(temp_list.isEmpty())
165  list->removeAt(c--);
166  else
167  {
168  list->replace(c, temp_list.at(0));
169  if(temp_list.size()>1)
170  list->insert(c, temp_list.at(1));
171  }
172  }
173  }
174  QList< Interval<T> > operator-(QList< Interval<T> > subtrahend) {
175  QList< Interval<T> > *tmp1, *tmp2;
176  tmp1 = new QList< Interval<T> >();
177  *tmp1 << *static_cast< Interval<T>* >(this);
178  foreach(Interval<T> i, subtrahend) {
179  tmp2 = new QList< Interval<T> >();
180  foreach(Interval<T> j, *tmp1)
181  *tmp2 << subtract(j, i);
182  delete tmp1;
183  tmp1 = tmp2;
184  }
185  QList< Interval<T> > result = *tmp1;
186  delete tmp1;
187  return result;
188  }
190  QString toString() const {
191  return "[" + QString::number(d_start) + "," + QString::number(d_end) + "]";
192  }
193 
194  protected:
198  T d_end;
199 };
200 
202 
210 template<class T> class Interval : public IntervalBase<T>
211 {
212  public:
213  Interval() {}
214  Interval(T start, T end) : IntervalBase<T>(start, end) {}
215  Interval(const Interval<T>& other) : IntervalBase<T>(other) {}
216  T size() const {
218  }
219  bool isValid() const {
220  return ( IntervalBase<T>::d_start >= 0 && IntervalBase<T>::d_end >= 0 &&
222  }
223  bool touches(const Interval<T>& other) const {
224  return ( (other.end() == IntervalBase<T>::d_start-1) ||
225  (other.start() == IntervalBase<T>::d_end+1) );
226  }
227 };
228 
229 template<> class Interval<float> : public IntervalBase<float> {
230  Interval() {}
231  Interval(float start, float end) : IntervalBase<float>(start, end) {}
232  Interval(const Interval<float>& other) : IntervalBase<float>(other) {}
235  bool touches(const Interval<float>& other) const {
236  return ( (other.end() == IntervalBase<float>::d_start) ||
237  (other.start() == IntervalBase<float>::d_end) );
238  }
239 };
240 
241 template<> class Interval<double> : public IntervalBase<double> {
242  Interval() {}
243  Interval(double start, double end) : IntervalBase<double>(start, end) {}
244  Interval(const Interval<double>& other) : IntervalBase<double>(other) {}
247  bool touches(const Interval<double>& other) const {
248  return ( (other.end() == IntervalBase<double>::d_start) ||
249  (other.start() == IntervalBase<double>::d_end) );
250  }
251 };
252 
253 template<> class Interval<long double> : public IntervalBase<long double> {
254  Interval() {}
255  Interval(long double start, long double end) : IntervalBase<long double>(start, end) {}
256  Interval(const Interval<long double>& other) : IntervalBase<long double>(other) {}
259  bool touches(const Interval<long double>& other) const {
260  return ( (other.end() == IntervalBase<long double>::d_start) ||
262  }
263 };
264 
265 #endif
266 
Interval::touches
bool touches(const Interval< T > &other) const
Definition: Interval.h:223
IntervalBase::IntervalBase
IntervalBase()
Definition: Interval.h:44
Interval< double >
Definition: Interval.h:241
IntervalBase::contains
bool contains(T value) const
Definition: Interval.h:59
IntervalBase::operator-
QList< Interval< T > > operator-(QList< Interval< T > > subtrahend)
Definition: Interval.h:174
Interval< long double >::isValid
bool isValid() const
Definition: Interval.h:258
Interval< long double >::size
long double size() const
Definition: Interval.h:257
Interval::Interval
Interval()
Definition: Interval.h:213
Interval< double >::Interval
Interval(const Interval< double > &other)
Definition: Interval.h:244
Interval< long double >::Interval
Interval(const Interval< long double > &other)
Definition: Interval.h:256
Interval::Interval
Interval(T start, T end)
Definition: Interval.h:214
Interval< float >::Interval
Interval(float start, float end)
Definition: Interval.h:231
IntervalBase::intersects
bool intersects(const Interval< T > &other) const
Definition: Interval.h:60
Interval::size
T size() const
Definition: Interval.h:216
IntervalBase::split
static QList< Interval< T > > split(const Interval< T > &i, T before)
Split an interval into two.
Definition: Interval.h:104
Interval< long double >
Definition: Interval.h:253
IntervalBase::merge
static Interval< T > merge(const Interval< T > &a, const Interval< T > &b)
Merge two intervals that touch or intersect.
Definition: Interval.h:79
IntervalBase::IntervalBase
IntervalBase(const IntervalBase< T > &other)
Definition: Interval.h:45
IntervalBase::setEnd
void setEnd(T end)
Definition: Interval.h:57
IntervalBase::intersection
static Interval< T > intersection(const Interval< T > &first, const Interval< T > &second)
Return the intersection of two intervals.
Definition: Interval.h:65
Interval< long double >::Interval
Interval(long double start, long double end)
Definition: Interval.h:255
Interval< double >::Interval
Interval()
Definition: Interval.h:242
Interval< double >::touches
bool touches(const Interval< double > &other) const
Returns true if no gap is between two intervals.
Definition: Interval.h:247
Interval< double >::Interval
Interval(double start, double end)
Definition: Interval.h:243
Interval< float >::Interval
Interval(const Interval< float > &other)
Definition: Interval.h:232
IntervalBase::operator=
Interval< T > & operator=(const Interval< T > &other)
Definition: Interval.h:71
Interval< long double >::touches
bool touches(const Interval< long double > &other) const
Returns true if no gap is between two intervals.
Definition: Interval.h:259
Interval< float >::isValid
bool isValid() const
Definition: Interval.h:234
Interval< double >::size
double size() const
Definition: Interval.h:245
Interval::isValid
bool isValid() const
Definition: Interval.h:219
Interval< float >
Definition: Interval.h:229
IntervalBase::subtractIntervalFromList
static void subtractIntervalFromList(QList< Interval< T > > *list, Interval< T > i)
Subtract an interval from all intervals in the list.
Definition: Interval.h:159
IntervalBase
Definition: Interval.h:42
IntervalBase::operator==
bool operator==(const Interval< T > &other) const
Definition: Interval.h:70
IntervalBase::start
T start() const
Definition: Interval.h:54
IntervalBase::IntervalBase
IntervalBase(T start, T end)
Definition: Interval.h:49
IntervalBase::mergeIntervalIntoList
static void mergeIntervalIntoList(QList< Interval< T > > *list, Interval< T > i)
Merge an interval into a list.
Definition: Interval.h:126
IntervalBase::translate
void translate(T offset)
Definition: Interval.h:69
IntervalBase::d_start
T d_start
Interval start.
Definition: Interval.h:196
IntervalBase::subtract
static QList< Interval< T > > subtract(const Interval< T > &src_iv, const Interval< T > &minus_iv)
Subtract an interval from another.
Definition: Interval.h:85
Interval< double >::isValid
bool isValid() const
Definition: Interval.h:246
Interval< float >::touches
bool touches(const Interval< float > &other) const
Returns true if no gap is between two intervals.
Definition: Interval.h:235
IntervalBase::~IntervalBase
virtual ~IntervalBase()
Definition: Interval.h:53
IntervalBase::contains
bool contains(const Interval< T > &other) const
Definition: Interval.h:58
IntervalBase::touches
virtual bool touches(const Interval< T > &other) const =0
Returns true if no gap is between two intervals.
Interval< long double >::Interval
Interval()
Definition: Interval.h:254
IntervalBase::toString
QString toString() const
Return a string in the format '[start,end]'.
Definition: Interval.h:190
IntervalBase::d_end
T d_end
Interval end.
Definition: Interval.h:198
Interval::Interval
Interval(const Interval< T > &other)
Definition: Interval.h:215
IntervalBase::end
T end() const
Definition: Interval.h:55
Interval
Auxiliary class for interval based data.
Definition: Interval.h:211
IntervalBase::restrictList
static void restrictList(QList< Interval< T > > *list, Interval< T > i)
Restrict all intervals in the list to their intersection with a given interval.
Definition: Interval.h:142
Interval< float >::Interval
Interval()
Definition: Interval.h:230
IntervalBase::setStart
void setStart(T start)
Definition: Interval.h:56
Interval< float >::size
float size() const
Definition: Interval.h:233