2 interval.hh -- part of flowerlib
4 (c) 1996 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.
21 struct Interval_t : public Drul_array<T> {
25 static T infinity () ;
26 static String T_to_str (T arg);
29 return (elem (LEFT) + elem (RIGHT)) / T (2);
44 *this and h are comparable
46 void unite (Interval_t<T> h);
47 void intersect (Interval_t<T> h);
48 void add_point (T p) {
49 elem(LEFT) = elem (LEFT) <? p;
50 elem(RIGHT) = elem (RIGHT) >? p;
55 bool empty_b () const { return elem (LEFT) > elem (RIGHT); }
56 bool contains_b (Interval_t<T> const&) const;
60 Interval_t (T m, T M) : Drul_array<T> (m,M)
63 Interval_t<T> &operator -= (T r) {
68 Interval_t<T> &operator += (T r) {
73 Interval_t<T> &operator *= (T r) {
85 Real linear_combination (Real x) const {
86 return ((1.0 - x) * Real (elem (LEFT)) + (x + 1.0) * Real (elem (RIGHT))) * 0.5;
101 elem (LEFT) = elem (RIGHT);
108 inclusion ordering. Crash if not comparable.
111 int Interval__compare (const Interval_t<T>&,Interval_t<T> const&);
114 Inclusion ordering. return -2 if not comparable
118 _Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b);
125 #include "compare.hh"
127 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T>&, Interval__compare, template<class T>);
132 intersection (Interval_t<T> a, Interval_t<T> const&b)
141 Interval_t<T> operator + (T a,Interval_t<T> i)
149 Interval_t<T> operator - (Interval_t<T> i, T a)
157 Interval_t<T> operator - (T a,Interval_t<T> i)
166 Interval_t<T> operator + (Interval_t<T> i,T a){
172 Interval_t<T> operator * (T a,Interval_t<T> i)
180 Interval_t<T> operator * (Interval_t<T> i,T a){
184 // again? see flower-proto.hh
185 typedef Interval_t<Real> Interval;
186 typedef Interval_t<int> Slice; // weird name
189 #endif // INTERVAL_HH