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.
21 struct Interval_t : public Drul_array<T> {
25 static T infinity () ;
26 static String T_to_string (T arg);
28 assert (!is_empty ());
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;
58 TODO: strip hungarian suffix.
60 bool is_empty () const { return elem (LEFT) > elem (RIGHT); }
61 bool contains_b (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) {
90 Real linear_combination (Real x) const {
91 Drul_array<Real> da (elem (LEFT),
93 return ::linear_combination (da, x);
95 String to_string () const;
108 elem (LEFT) = elem (RIGHT);
115 inclusion ordering. Crash if not comparable.
118 int Interval__compare (const Interval_t<T>&,Interval_t<T> const&);
121 Inclusion ordering. return -2 if not comparable
125 _Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b);
132 #include "compare.hh"
134 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T>&, Interval__compare, template<class T>);
139 intersection (Interval_t<T> a, Interval_t<T> const&b)
148 Interval_t<T> operator + (T a,Interval_t<T> i)
156 Interval_t<T> operator - (Interval_t<T> i, T a)
164 Interval_t<T> operator - (T a,Interval_t<T> i)
173 Interval_t<T> operator + (Interval_t<T> i,T a){
179 Interval_t<T> operator * (T a,Interval_t<T> i)
187 Interval_t<T> operator * (Interval_t<T> i,T a){
191 // again? see flower-proto.hh
192 typedef Interval_t<Real> Interval;
193 typedef Interval_t<int> Slice; // weird name
196 #endif // INTERVAL_HH