00001 #ifndef VRS_RANGE_H
00002 #define VRS_RANGE_H
00003
00004 #include <vrs/sharedobj.h>
00005 #include <utility>
00006 #include <list>
00007
00008 namespace VRS
00009 {
00010
00013 class SimpleRange
00014 {
00015 public:
00016 enum RangeMode
00017 {
00018 COMPACT = 0,
00019 RIGHT_EXCLUDED = 1,
00020 LEFT_EXCLUDED = 2,
00021 EXCLUDED = 3
00022 };
00023
00024 struct Bound
00025 {
00026 double value;
00027 unsigned int mode;
00028 };
00029
00030 SimpleRange(double left = 0.0, double right = 0.0, unsigned int mode = COMPACT);
00031 SimpleRange(double value);
00032 SimpleRange(const SimpleRange& other);
00033
00034 bool operator==(const SimpleRange& other) const;
00035 bool operator!=(const SimpleRange& other) const;
00036 SimpleRange& operator=(const SimpleRange& other);
00037
00040 bool isEmpty() const;
00041
00044 SimpleRange operator&(const SimpleRange& other) const;
00045 SimpleRange& operator&=(const SimpleRange& other);
00046
00049 SimpleRange operator|(const SimpleRange& other) const;
00050 SimpleRange& operator|=(const SimpleRange& other);
00051
00053 bool isRightExcluded() const;
00054 bool isLeftExcluded() const;
00055
00057 bool contains(double value) const;
00058 bool contains(const SimpleRange& other) const;
00059 bool isContained(const SimpleRange& other) const;
00060
00063 bool overlaps(const SimpleRange& other) const;
00064
00066 unsigned int mode() const { return m_mode; }
00067
00070 Bound lowerBound() const;
00071 Bound upperBound() const;
00072
00079 bool shareBounds(const SimpleRange& other) const;
00080
00081 double length() const { return m_range.second - m_range.first; }
00082
00084 inline const double& first() const { return m_range.first; }
00085 inline const double& second() const { return m_range.second; }
00086
00088 bool expandTo(const Bound& b);
00089
00091 bool hasSmallerLeft(const SimpleRange& other) const;
00092
00094 bool hasLargerLeft(const SimpleRange& other) const;
00095
00097 bool hasSmallerRight(const SimpleRange& other) const;
00098
00100 bool hasLargerRight(const SimpleRange& other) const;
00101
00106 SimpleRange operator+(double value) const;
00107 SimpleRange operator-(double value) const;
00108 SimpleRange& operator+=(double value);
00109 SimpleRange& operator-=(double value);
00110
00111 SimpleRange operator+(const SimpleRange& range) const;
00112 SimpleRange operator-(const SimpleRange& range) const;
00113 SimpleRange& operator+=(const SimpleRange& range);
00114 SimpleRange& operator-=(const SimpleRange& range);
00115
00116
00117 VRS_SERIALIZABLE_NO_SO_CLASS(SimpleRange);
00118
00119
00120 static SimpleRange empty;
00121
00122 private:
00123 std::pair<double, double> m_range;
00124 unsigned int m_mode;
00125 };
00126
00127
00128
00129 class MultiRange
00130 {
00131 public:
00132 typedef std::list<SimpleRange> RangeList;
00133
00137 MultiRange();
00138 MultiRange(const SimpleRange& range);
00139
00144 MultiRange operator+(const SimpleRange& range) const;
00145 MultiRange operator-(const SimpleRange& range) const;
00146 MultiRange& operator+=(const SimpleRange& range);
00147 MultiRange& operator-=(const SimpleRange& range);
00148
00149 const RangeList& getRanges() const;
00150
00151 bool isEmpty() const;
00152
00154 MultiRange operator&(const MultiRange& range) const;
00155 MultiRange operator|(const MultiRange& range) const;
00156 MultiRange& operator&=(const MultiRange& range);
00157 MultiRange& operator|=(const MultiRange& range);
00158
00160 MultiRange operator&(const SimpleRange& range) const;
00161 MultiRange operator|(const SimpleRange& range) const;
00162 MultiRange& operator&=(const SimpleRange& range);
00163 MultiRange& operator|=(const SimpleRange& range);
00164
00165 bool contains(double value) const;
00166 bool contains(const SimpleRange& range) const;
00167
00169 void clear();
00170
00171 VRS_SERIALIZABLE_NO_SO_CLASS(MultiRange);
00172
00173 private:
00174 void removeEmptyRanges() const;
00175
00176 RangeList m_ranges;
00177 };
00178
00179
00184 class CircularRange : public MultiRange
00185 {
00186 public:
00187 CircularRange(double lower = 0.0, double upper = 360.0);
00188 CircularRange(const CircularRange& other);
00189
00190 CircularRange operator+(const SimpleRange& range) const;
00191 CircularRange operator-(const SimpleRange& range) const;
00192 CircularRange& operator+=(const SimpleRange& range);
00193 CircularRange& operator-=(const SimpleRange& range);
00194
00195 bool contains(double value) const;
00196 bool contains(const SimpleRange& range) const;
00197
00199 bool isFilled() const;
00200
00201 VRS_SERIALIZABLE_NO_SO_CLASS(CircularRange);
00202
00203 private:
00204 double mod(double v) const;
00205
00206 SimpleRange m_range;
00207 };
00208
00209
00210 std::ostream& operator<<(std::ostream& s, const SimpleRange& r);
00211 std::ostream& operator<<(std::ostream& s, const MultiRange& r);
00212 std::ostream& operator<<(std::ostream& s, const CircularRange& r);
00213
00214
00215 }
00216
00217 #endif // VRS_RANGE_H