intervals.h
1 /********************************************************************************
2  * FARSA Utilities Library *
3  * Copyright (C) 2007-2012 *
4  * Gianluca Massera <emmegian@yahoo.it> *
5  * Stefano Nolfi <stefano.nolfi@istc.cnr.it> *
6  * Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
21  ********************************************************************************/
22 
23 #ifndef INTERVALS_H
24 #define INTERVALS_H
25 
26 #include "utilitiesconfig.h"
27 
28 #include <QLinkedList>
29 #include <QString>
30 #include <cmath>
31 #include <limits>
32 #include "mathutils.h"
33 
34 namespace farsa {
35 
44 class FARSA_UTIL_API SimpleInterval
45 {
46 public:
53  start(),
54  end(start)
55  {
56  }
57 
65  start(s),
66  end(e)
67  {
68  }
69 
76  start(other.start),
77  end(other.end)
78  {
79  }
80 
88  {
89  if (&other == this) {
90  return *this;
91  }
92 
93  start = other.start;
94  end = other.end;
95 
96  return *this;
97  }
98 
103  {
104  }
105 
115  bool operator<(const SimpleInterval& other) const
116  {
117  return start < other.start;
118  }
119 
125  real length() const
126  {
127  return end - start;
128  }
129 
136  bool equals(const SimpleInterval& other) const
137  {
138  return (start == other.start) && (end == other.end);
139  }
140 
146  operator QString() const
147  {
148  return QString("[%1, %2]").arg(start).arg(end);
149  }
150 
156  QString toString() const
157  {
158  return *this;
159  }
160 
172  static SimpleInterval fromString(QString str, bool* ok = NULL);
173 
180  static QString vectorOfSimpleIntervalsToString(QVector<SimpleInterval> v);
181 
192  static QVector<SimpleInterval> vectorOfSimpleIntervalsFromString(QString s, bool* ok = NULL);
193 
198 
203 };
204 
218 class FARSA_UTIL_API Intervals
219 {
220 public:
224  typedef QLinkedList<SimpleInterval>::const_iterator const_iterator;
225 
226 public:
233  m_length(0.0),
234  m_intervals()
235  {
236  }
237 
244  Intervals(const SimpleInterval& interval);
245 
252  template <class List_t>
253  Intervals(const List_t& list) :
254  m_length(0.0),
255  m_intervals()
256  {
257  for (typename List_t::const_iterator it = list.begin(); it != list.end(); it++) {
258  unite(*it);
259  }
260  }
261 
267  Intervals(const Intervals& other) :
268  m_length(other.m_length),
269  m_intervals(other.m_intervals)
270  {
271  }
272 
279  Intervals& operator=(const Intervals& other);
280 
285  {
286  }
287 
293  operator QString() const;
294 
300  QString toString() const
301  {
302  return *this;
303  }
304 
311  real length() const
312  {
313  return m_length;
314  }
315 
324  const_iterator begin() const
325  {
326  return m_intervals.begin();
327  }
328 
337  const_iterator constBegin() const
338  {
339  return m_intervals.constBegin();
340  }
341 
349  const_iterator end() const
350  {
351  return m_intervals.end();
352  }
353 
361  const_iterator constEnd() const
362  {
363  return m_intervals.constEnd();
364  }
365 
371  const QLinkedList<SimpleInterval>& getSimpleIntervalList() const
372  {
373  return m_intervals;
374  }
375 
381  bool isEmpty() const
382  {
383  return m_intervals.isEmpty();
384  }
385 
391  bool empty() const
392  {
393  return m_intervals.empty();
394  }
395 
399  void clear();
400 
408  {
409  intersect(other.constBegin(), other.constEnd());
410  return *this;
411  }
412 
420  {
421  intersect(&i, &i + 1);
422  return *this;
423  }
424 
431  Intervals operator&(const Intervals& other) const
432  {
433  Intervals i(*this);
434  return i.intersect(other);
435  }
436 
444  {
445  Intervals intrv(*this);
446  return intrv.intersect(i);
447  }
448 
456  {
457  return intersect(other);
458  }
459 
467  {
468  return intersect(value);
469  }
470 
477  Intervals& unite(const Intervals& other)
478  {
479  unite(other.constBegin(), other.constEnd());
480  return *this;
481  }
482 
490  {
491  unite(&i, &i + 1);
492  return *this;
493  }
494 
501  Intervals operator+(const Intervals& other) const
502  {
503  Intervals i(*this);
504  return i.unite(other);
505  }
506 
514  {
515  Intervals intrv(*this);
516  return intrv.unite(i);
517  }
518 
526  {
527  return unite(other);
528  }
529 
537  {
538  return unite(value);
539  }
540 
547  Intervals operator|(const Intervals& other) const
548  {
549  Intervals i(*this);
550  return i.unite(other);
551  }
552 
560  {
561  Intervals intrv(*this);
562  return intrv.unite(i);
563  }
564 
572  {
573  return unite(other);
574  }
575 
583  {
584  return unite(value);
585  }
586 
594  {
595  return unite(value);
596  }
597 
604  Intervals& subtract(const Intervals& other)
605  {
606  subtract(other.constBegin(), other.constEnd());
607  return *this;
608  }
609 
617  {
618  subtract(&i, &i + 1);
619  return *this;
620  }
621 
628  Intervals operator-(const Intervals& other) const
629  {
630  Intervals i(*this);
631  return i.subtract(other);
632  }
633 
641  {
642  Intervals intrv(*this);
643  return intrv.subtract(i);
644  }
645 
653  {
654  return subtract(other);
655  }
656 
664  {
665  return subtract(value);
666  }
667 
675  bool operator!=(const Intervals& other) const
676  {
677  return !(*this == other);
678  }
679 
687  bool operator==(const Intervals& other) const;
688 
695  bool valueIn(real v) const;
696 
697 protected:
701  void recomputeLength();
702 
711  template <class OtherIterator_t>
712  void intersect(OtherIterator_t otherBegin, OtherIterator_t otherEnd);
713 
721  template <class OtherIterator_t>
722  void unite(OtherIterator_t otherBegin, OtherIterator_t otherEnd);
723 
732  template <class OtherIterator_t>
733  void subtract(OtherIterator_t otherBegin, OtherIterator_t otherEnd);
734 
739 
746  QLinkedList<SimpleInterval> m_intervals;
747 };
748 
749 } // end namespace farsa
750 
751 #endif
This file contains the common type defitions used on the whole framework.
bool equals(const SimpleInterval &other) const
Returns true if other is equal to this interval.
Definition: intervals.h:136
SimpleInterval & operator=(const SimpleInterval &other)
Copy operator.
Definition: intervals.h:87
bool operator<(const SimpleInterval &other) const
Compares two intervals.
Definition: intervals.h:115
Intervals & unite(const SimpleInterval &i)
Unites intervals in this with the given simple interval.
Definition: intervals.h:489
QLinkedList< SimpleInterval > m_intervals
The list of simple intervals.
Definition: intervals.h:746
Intervals & subtract(const SimpleInterval &i)
Subtracts intervals in this with the given simple interval.
Definition: intervals.h:616
Intervals & operator+=(const Intervals &other)
Unites intervals in this with other.
Definition: intervals.h:525
real m_length
The total length of intervals.
Definition: intervals.h:738
real length() const
Returns the size of the intervals.
Definition: intervals.h:311
Intervals & operator-=(const Intervals &other)
Subtracts intervals in this with other.
Definition: intervals.h:652
const QLinkedList< SimpleInterval > & getSimpleIntervalList() const
Returns a const reference to the list of simple intervals.
Definition: intervals.h:371
Intervals & operator<<(const SimpleInterval &value)
Unites a simple interval with intervals in this one.
Definition: intervals.h:593
Intervals operator|(const Intervals &other) const
Returns the union of this and other.
Definition: intervals.h:547
~Intervals()
Destructor.
Definition: intervals.h:284
QString toString() const
Returns the string representation of the interval.
Definition: intervals.h:156
A macro to deprecate functions.
real length() const
Returns the length of the interval.
Definition: intervals.h:125
bool operator!=(const Intervals &other) const
Returns true if this and other are different.
Definition: intervals.h:675
Intervals operator-(const SimpleInterval &i) const
Returns the subtraction of this and the given simple interval.
Definition: intervals.h:640
Intervals operator-(const Intervals &other) const
Returns the subtraction of this and other.
Definition: intervals.h:628
Intervals operator&(const Intervals &other) const
Returns the intersection of this and other.
Definition: intervals.h:431
Intervals & operator|=(const Intervals &other)
Unites intervals in this with other.
Definition: intervals.h:571
Intervals operator+(const SimpleInterval &i) const
Returns the union of this and the given simple interval.
Definition: intervals.h:513
Intervals & operator&=(const SimpleInterval &value)
Intersects a simple interval with intervals in this one.
Definition: intervals.h:466
Intervals()
Constructor.
Definition: intervals.h:232
Intervals(const Intervals &other)
Copy Constructor.
Definition: intervals.h:267
const_iterator begin() const
Returns a const iterator to the beginning of the list of simple intervals.
Definition: intervals.h:324
A class modelling a range of real values.
Definition: intervals.h:44
bool isEmpty() const
Returns true if the list of intervals is empty.
Definition: intervals.h:381
Intervals & unite(const Intervals &other)
Unites intervals in this with other.
Definition: intervals.h:477
Intervals & operator+=(const SimpleInterval &value)
Unites a simple interval with intervals in this one.
Definition: intervals.h:536
Intervals & intersect(const SimpleInterval &i)
Intersects intervals in this with the given simple interval.
Definition: intervals.h:419
Intervals & operator&=(const Intervals &other)
Intersects intervals in this with other.
Definition: intervals.h:455
SimpleInterval()
Constructor.
Definition: intervals.h:52
Intervals(const List_t &list)
Constructor.
Definition: intervals.h:253
Intervals & operator|=(const SimpleInterval &value)
Unites a simple interval with intervals in this one.
Definition: intervals.h:582
float real
Abstraction on the type of real numbers.
Definition: mathutils.h:45
Intervals operator&(const SimpleInterval &i) const
Returns the intersection of this and the given simple interval.
Definition: intervals.h:443
QLinkedList< SimpleInterval >::const_iterator const_iterator
The const iterator on simple intervals.
Definition: intervals.h:224
const_iterator constBegin() const
Returns a const iterator to the beginning of the list of simple intervals.
Definition: intervals.h:337
real end
The ending value of the interval.
Definition: intervals.h:202
QString toString() const
Returns the string representation of the interval.
Definition: intervals.h:300
real start
The starting value of the interval.
Definition: intervals.h:197
The class modelling intervals of floating point values.
Definition: intervals.h:218
Intervals operator|(const SimpleInterval &i) const
Returns the union of this and the given simple interval.
Definition: intervals.h:559
SimpleInterval(real s, real e)
Builds and interval from start to end.
Definition: intervals.h:64
const_iterator constEnd() const
Returns a const iterator to the end of the list of simple intervals.
Definition: intervals.h:361
const_iterator end() const
Returns a const iterator to the end of the list of simple intervals.
Definition: intervals.h:349
~SimpleInterval()
Destructor.
Definition: intervals.h:102
SimpleInterval(const SimpleInterval &other)
Copy constructor.
Definition: intervals.h:75
Intervals operator+(const Intervals &other) const
Returns the union of this and other.
Definition: intervals.h:501
bool empty() const
Returns true if the list of intervals is empty.
Definition: intervals.h:391
Intervals & operator-=(const SimpleInterval &value)
Subtracts a simple interval with intervals in this one.
Definition: intervals.h:663
Intervals & subtract(const Intervals &other)
Subtracts intervals from other to these ones.
Definition: intervals.h:604
Intervals & intersect(const Intervals &other)
Intersects intervals in this with other.
Definition: intervals.h:407