00001 /*************************************************************************** 00002 ** 00003 ** This file is part of GeopsyCore. 00004 ** 00005 ** This library is free software; you can redistribute it and/or 00006 ** modify it under the terms of the GNU Lesser General Public 00007 ** License as published by the Free Software Foundation; either 00008 ** version 2.1 of the License, or (at your option) any later version. 00009 ** 00010 ** This file is distributed in the hope that it will be useful, but WITHOUT 00011 ** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 ** FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00013 ** License for more details. 00014 ** 00015 ** You should have received a copy of the GNU Lesser General Public 00016 ** License along with this library; if not, write to the Free Software 00017 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00018 ** 00019 ** See http://www.geopsy.org for more information. 00020 ** 00021 ** Created : 2009-06-01 00022 ** Authors : 00023 ** Marc Wathelet 00024 ** Marc Wathelet (LGIT, Grenoble, France) 00025 ** 00026 ***************************************************************************/ 00027 00028 #ifndef SPARSETIMERANGE_H 00029 #define SPARSETIMERANGE_H 00030 00031 #include <QGpCoreTools.h> 00032 #include "TimeRange.h" 00033 #include "GeopsyCoreDLLExport.h" 00034 00035 namespace GeopsyCore { 00036 00037 class GEOPSYCORE_EXPORT SparseTimeRange : private QVector<TimeRange> 00038 { 00039 TRANSLATIONS("SparseTimeRange") 00040 public: 00041 SparseTimeRange(); 00042 SparseTimeRange(const SparseTimeRange& r); 00043 SparseTimeRange(const TimeRange& r); 00044 ~SparseTimeRange(); 00045 00046 void add(const TimeRange& r, bool acceptOverlap=false); 00047 void add(const SparseTimeRange& r, bool acceptOverlap=false); 00048 void remove(const SparseTimeRange& r); 00049 inline void remove(const TimeRange& r); 00050 void clear(); 00051 void shift(double dt); 00052 void scale(double center, double factor); 00053 void removeGaps(double dt); 00054 void removeBlocks(double dt); 00055 00056 SparseTimeRange invert(bool infiniteEnds=false) const; 00057 SparseTimeRange invert(const TimeRange& r) const; 00058 inline SparseTimeRange intersection(const TimeRange& r) const; 00059 SparseTimeRange intersection(const SparseTimeRange& r) const; 00060 SparseTimeRange hit(const TimeRange& r) const; 00061 SparseTimeRange hit(const SparseTimeRange& r) const; 00062 inline bool intersects(const SparseTimeRange& r) const; 00063 inline bool intersects(const TimeRange& r) const; 00064 00065 bool isNull() const {return QVector<TimeRange>::isEmpty();} 00066 const QVector<TimeRange>& ranges() const {return *this;} 00067 inline TimeRange range() const; 00068 00069 void testIndex() const; 00070 void printDebug() const; 00071 protected: 00072 int index(double t) const; 00073 SparseTimeRange intersection(int index, const TimeRange& r) const; 00074 int remove(int& index, const TimeRange& r); 00075 private: 00076 void append(const TimeRange& r); 00077 void insert(int before, const TimeRange& r); 00078 void remove(int index); 00079 00080 int _n2; 00081 static const double _relPrec; 00082 }; 00083 00084 inline bool SparseTimeRange::intersects(const TimeRange& r) const 00085 { 00086 SparseTimeRange res=intersection(r); 00087 return !res.isEmpty(); 00088 } 00089 00090 inline bool SparseTimeRange::intersects(const SparseTimeRange& r) const 00091 { 00092 SparseTimeRange res=intersection(r); 00093 return !res.isEmpty(); 00094 } 00095 00096 inline SparseTimeRange SparseTimeRange::intersection(const TimeRange& r) const 00097 { 00098 if(isEmpty()) return *this; 00099 return intersection(index(r.start()), r); 00100 } 00101 00102 inline void SparseTimeRange::remove(const TimeRange& r) 00103 { 00104 if(!isEmpty()) { 00105 int i=index(r.start()); 00106 remove(i, r); 00107 } 00108 } 00109 00110 inline TimeRange SparseTimeRange::range() const 00111 { 00112 if(isEmpty()) 00113 return TimeRange(); 00114 else 00115 return TimeRange(first().start(), last().end()); 00116 } 00117 00118 } // namespace GeopsyCore 00119 00120 #endif // SPARSETIMERANGE_H