2 interval.hh -- part of flowerlib
4 (c) 1996--2004 Han-Wen Nienhuys
11 #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;
40 *this and h are comparable
42 void unite (Interval_t<T> h);
43 void intersect (Interval_t<T> h);
46 elem_ref(LEFT) = elem (LEFT) <? p;
47 elem_ref(RIGHT) = elem (RIGHT) >? p;
55 TODO: strip hungarian suffix.
57 bool is_empty () const
59 return elem (LEFT) > elem (RIGHT);
61 bool superset (Interval_t<T> const&) const;
66 Interval_t (T m, T M) : Drul_array<T> (m,M)
69 Interval_t<T> &operator -= (T r) {
74 Interval_t<T> &operator += (T r) {
79 Interval_t<T> &operator *= (T r) {
83 elem_ref (RIGHT) *= r;
91 Real linear_combination (Real x) const
93 Drul_array<Real> da (elem (LEFT), elem (RIGHT));
94 return ::linear_combination (da, x);
96 String to_string () const;
110 elem_ref (LEFT) = elem (RIGHT);
111 elem_ref (RIGHT) = t;
117 inclusion ordering. Crash if not comparable.
120 int Interval__compare (const Interval_t<T>&,Interval_t<T> const&);
123 Inclusion ordering. return -2 if not comparable
127 _Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b);
134 #include "compare.hh"
136 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T>&, Interval__compare, template<class T>);
141 intersection (Interval_t<T> a, Interval_t<T> const&b)
150 Interval_t<T> operator + (T a,Interval_t<T> i)
158 Interval_t<T> operator - (Interval_t<T> i, T a)
166 Interval_t<T> operator - (T a,Interval_t<T> i)
175 Interval_t<T> operator + (Interval_t<T> i,T a){
181 Interval_t<T> operator * (T a,Interval_t<T> i)
189 Interval_t<T> operator * (Interval_t<T> i,T a){
196 Interval_t<T>::center () const
198 assert (!is_empty ());
199 return (elem (LEFT) + elem (RIGHT)) / T (2);
202 // again? see flower-proto.hh
203 typedef Interval_t<Real> Interval;
204 typedef Interval_t<int> Slice; // weird name
207 #endif // INTERVAL_HH