2 interval.hh -- part of flowerlib
4 (c) 1996--2005 Han-Wen Nienhuys
12 #include "flower-proto.hh"
13 #include "drul-array.hh"
15 /* A T interval. This represents the closed interval [left,right].
16 No invariants. T must be a totally ordered ring (with division, anyway ..)
17 At instantiation, the function infinity () has to be defined explicitely. */
19 struct Interval_t : public Drul_array<T>
22 Drul_array<T>::elem_ref;
25 static String T_to_string (T arg);
30 elem_ref (RIGHT) += t;
35 elem_ref (RIGHT) += t;
38 T distance (T t) const
41 return T (t - elem (RIGHT));
42 else if (t < elem (LEFT))
43 return T (elem (LEFT) - t);
49 *this and h are comparable
51 void unite (Interval_t<T> h);
52 void intersect (Interval_t<T> h);
55 elem_ref (LEFT) = min (elem (LEFT), p);
56 elem_ref (RIGHT) = max (elem (RIGHT), p);
64 TODO: strip hungarian suffix.
66 bool is_empty () const
68 return elem (LEFT) > elem (RIGHT);
70 bool superset (Interval_t<T> const &) const;
75 Interval_t (Drul_array<T> const &src)
80 Interval_t (T m, T M) : Drul_array<T> (m, M)
83 Interval_t<T> &operator -= (T r)
89 Interval_t<T> &operator += (T r)
92 elem_ref (RIGHT) += r;
95 Interval_t<T> &operator *= (T r)
100 elem_ref (RIGHT) *= r;
107 Real linear_combination (Real x) const
109 Drul_array<Real> da (elem (LEFT), elem (RIGHT));
110 return ::linear_combination (da, x);
112 String to_string () const;
114 bool contains (T r) const;
120 elem_ref (RIGHT) = r;
126 elem_ref (LEFT) = elem (RIGHT);
127 elem_ref (RIGHT) = t;
130 static int left_comparison (Interval_t<T> const &a, Interval_t<T> const &b)
132 return sign (a[LEFT] - b[RIGHT]);
137 inclusion ordering. Crash if not comparable.
140 int Interval__compare (const Interval_t<T> &, Interval_t<T> const &);
143 Inclusion ordering. return -2 if not comparable
147 _Interval__compare (const Interval_t<T> &a, Interval_t<T> const &b);
153 #include "compare.hh"
155 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T> &, Interval__compare, template<class T>);
159 intersection (Interval_t<T> a, Interval_t<T> const &b)
167 Interval_t<T> operator + (T a, Interval_t<T> i)
175 Interval_t<T> operator - (Interval_t<T> i, T a)
183 Interval_t<T> operator - (T a, Interval_t<T> i)
192 Interval_t<T> operator + (Interval_t<T> i, T a)
199 Interval_t<T> operator * (T a, Interval_t<T> i)
207 Interval_t<T> operator * (Interval_t<T> i, T a)
214 Interval_t<T>::center () const
216 assert (!is_empty ());
217 return (elem (LEFT) + elem (RIGHT)) / T (2);
220 typedef Interval_t<Real> Interval;
221 typedef Interval_t<int> Slice; // weird name
224 #endif // INTERVAL_HH