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 (T m, T M) : Drul_array<T> (m, M)
76 Interval_t<T> &operator -= (T r)
82 Interval_t<T> &operator += (T r)
85 elem_ref (RIGHT) += r;
88 Interval_t<T> &operator *= (T r)
93 elem_ref (RIGHT) *= r;
100 Real linear_combination (Real x) const
102 Drul_array<Real> da (elem (LEFT), elem (RIGHT));
103 return ::linear_combination (da, x);
105 String to_string () const;
113 elem_ref (RIGHT) = r;
119 elem_ref (LEFT) = elem (RIGHT);
120 elem_ref (RIGHT) = t;
123 static int left_comparison (Interval_t<T> const &a, Interval_t<T> const &b)
125 return sign (a[LEFT] - b[RIGHT]);
130 inclusion ordering. Crash if not comparable.
133 int Interval__compare (const Interval_t<T> &, Interval_t<T> const &);
136 Inclusion ordering. return -2 if not comparable
140 _Interval__compare (const Interval_t<T> &a, Interval_t<T> const &b);
146 #include "compare.hh"
148 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T> &, Interval__compare, template<class T>);
152 intersection (Interval_t<T> a, Interval_t<T> const &b)
160 Interval_t<T> operator + (T a, Interval_t<T> i)
168 Interval_t<T> operator - (Interval_t<T> i, T a)
176 Interval_t<T> operator - (T a, Interval_t<T> i)
185 Interval_t<T> operator + (Interval_t<T> i, T a)
192 Interval_t<T> operator * (T a, Interval_t<T> i)
200 Interval_t<T> operator * (Interval_t<T> i, T a)
207 Interval_t<T>::center () const
209 assert (!is_empty ());
210 return (elem (LEFT) + elem (RIGHT)) / T (2);
213 typedef Interval_t<Real> Interval;
214 typedef Interval_t<int> Slice; // weird name
217 #endif // INTERVAL_HH