2 interval.hh -- part of flowerlib
4 (c) 1996--2005 Han-Wen Nienhuys
10 #include "flower-proto.hh"
11 #include "drul-array.hh"
13 /* A T interval. This represents the closed interval [left,right].
14 No invariants. T must be a totally ordered ring (with division, anyway ..)
15 At instantiation, the function infinity () has to be defined explicitely. */
17 struct Interval_t : public Drul_array<T>
20 Drul_array<T>::elem_ref;
23 static String T_to_string (T arg);
28 elem_ref (RIGHT) += t;
33 elem_ref (RIGHT) += t;
36 T distance (T t) const
39 return T (t - elem (RIGHT));
40 else if (t < elem (LEFT))
41 return T (elem (LEFT) - t);
47 *this and h are comparable
49 void unite (Interval_t<T> h);
50 void intersect (Interval_t<T> h);
53 elem_ref (LEFT) = min (elem (LEFT), p);
54 elem_ref (RIGHT) = max (elem (RIGHT), p);
62 TODO: strip hungarian suffix.
64 bool is_empty () const
66 return elem (LEFT) > elem (RIGHT);
68 bool superset (Interval_t<T> const &) const;
73 Interval_t (Drul_array<T> const &src)
78 Interval_t (T m, T M) : Drul_array<T> (m, M)
81 Interval_t<T> &operator -= (T r)
87 Interval_t<T> &operator += (T r)
90 elem_ref (RIGHT) += r;
93 Interval_t<T> &operator *= (T r)
98 elem_ref (RIGHT) *= r;
105 Real linear_combination (Real x) const
107 Drul_array<Real> da (elem (LEFT), elem (RIGHT));
108 return ::linear_combination (da, x);
110 String to_string () const;
112 bool contains (T r) const;
118 elem_ref (RIGHT) = r;
124 elem_ref (LEFT) = elem (RIGHT);
125 elem_ref (RIGHT) = t;
128 static int left_comparison (Interval_t<T> const &a, Interval_t<T> const &b)
130 return sign (a[LEFT] - b[RIGHT]);
135 inclusion ordering. Crash if not comparable.
138 int Interval__compare (const Interval_t<T> &, Interval_t<T> const &);
141 Inclusion ordering. return -2 if not comparable
145 _Interval__compare (const Interval_t<T> &a, Interval_t<T> const &b);
151 #include "compare.hh"
153 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T> &, Interval__compare, template<class T>);
157 intersection (Interval_t<T> a, Interval_t<T> const &b)
165 Interval_t<T> operator + (T a, Interval_t<T> i)
173 Interval_t<T> operator - (Interval_t<T> i, T a)
181 Interval_t<T> operator - (T a, Interval_t<T> i)
190 Interval_t<T> operator + (Interval_t<T> i, T a)
197 Interval_t<T> operator * (T a, Interval_t<T> i)
205 Interval_t<T> operator * (Interval_t<T> i, T a)
212 Interval_t<T>::center () const
214 assert (!is_empty ());
215 return (elem (LEFT) + elem (RIGHT)) / T (2);
218 typedef Interval_t<Real> Interval;
219 typedef Interval_t<int> Slice; // weird name
222 #endif // INTERVAL_HH