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>
24 static String T_to_string (T arg);
29 elem_ref (RIGHT) += t;
34 elem_ref (RIGHT) += t;
39 *this and h are comparable
41 void unite (Interval_t<T> h);
42 void intersect (Interval_t<T> h);
45 elem_ref(LEFT) = elem (LEFT) <? p;
46 elem_ref(RIGHT) = elem (RIGHT) >? p;
54 TODO: strip hungarian suffix.
56 bool is_empty () const
58 return elem (LEFT) > elem (RIGHT);
60 bool superset (Interval_t<T> const&) const;
65 Interval_t (T m, T M) : Drul_array<T> (m,M)
68 Interval_t<T> &operator -= (T r) {
73 Interval_t<T> &operator += (T r) {
78 Interval_t<T> &operator *= (T r) {
82 elem_ref (RIGHT) *= r;
90 Real linear_combination (Real x) const
92 Drul_array<Real> da (elem (LEFT), elem (RIGHT));
93 return ::linear_combination (da, x);
95 String to_string () const;
109 elem_ref (LEFT) = elem (RIGHT);
110 elem_ref (RIGHT) = t;
116 inclusion ordering. Crash if not comparable.
119 int Interval__compare (const Interval_t<T>&,Interval_t<T> const&);
122 Inclusion ordering. return -2 if not comparable
126 _Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b);
133 #include "compare.hh"
135 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T>&, Interval__compare, template<class T>);
140 intersection (Interval_t<T> a, Interval_t<T> const&b)
149 Interval_t<T> operator + (T a,Interval_t<T> i)
157 Interval_t<T> operator - (Interval_t<T> i, T a)
165 Interval_t<T> operator - (T a,Interval_t<T> i)
174 Interval_t<T> operator + (Interval_t<T> i,T a){
180 Interval_t<T> operator * (T a,Interval_t<T> i)
188 Interval_t<T> operator * (Interval_t<T> i,T a){
195 Interval_t<T>::center () const
197 assert (!is_empty ());
198 return (elem (LEFT) + elem (RIGHT)) / T (2);
201 // again? see flower-proto.hh
202 typedef Interval_t<Real> Interval;
203 typedef Interval_t<int> Slice; // weird name
206 #endif // INTERVAL_HH