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 
87  SimpleInterval& operator=(const SimpleInterval& other)
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 
407  Intervals& intersect(const Intervals& other)
408  {
409  intersect(other.constBegin(), other.constEnd());
410  return *this;
411  }
412 
419  Intervals& intersect(const SimpleInterval& i)
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 
443  Intervals operator&(const SimpleInterval& i) const
444  {
445  Intervals intrv(*this);
446  return intrv.intersect(i);
447  }
448 
455  Intervals& operator&=(const Intervals& other)
456  {
457  return intersect(other);
458  }
459 
466  Intervals& operator&=(const SimpleInterval& value)
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 
489  Intervals& unite(const SimpleInterval& i)
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 
513  Intervals operator+(const SimpleInterval& i) const
514  {
515  Intervals intrv(*this);
516  return intrv.unite(i);
517  }
518 
525  Intervals& operator+=(const Intervals& other)
526  {
527  return unite(other);
528  }
529 
536  Intervals& operator+=(const SimpleInterval& value)
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 
559  Intervals operator|(const SimpleInterval& i) const
560  {
561  Intervals intrv(*this);
562  return intrv.unite(i);
563  }
564 
571  Intervals& operator|=(const Intervals& other)
572  {
573  return unite(other);
574  }
575 
582  Intervals& operator|=(const SimpleInterval& value)
583  {
584  return unite(value);
585  }
586 
593  Intervals& operator<<(const SimpleInterval& value)
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 
616  Intervals& subtract(const SimpleInterval& i)
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 
640  Intervals operator-(const SimpleInterval& i) const
641  {
642  Intervals intrv(*this);
643  return intrv.subtract(i);
644  }
645 
652  Intervals& operator-=(const Intervals& other)
653  {
654  return subtract(other);
655  }
656 
663  Intervals& operator-=(const SimpleInterval& value)
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