2 interval.hh -- part of flowerlib
4 (c) 1996--2009 Han-Wen Nienhuys
12 #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);
37 T distance (T t) const
40 return T (t - at (RIGHT));
41 else if (t < at (LEFT))
42 return T (at (LEFT) - t);
48 *this and h are comparable
50 void unite (Interval_t<T> h);
51 void intersect (Interval_t<T> h);
54 at (LEFT) = min (at (LEFT), p);
55 at (RIGHT) = max (at (RIGHT), p);
62 bool is_empty () const
64 return at (LEFT) > at (RIGHT);
66 bool superset (Interval_t<T> const &) const;
71 Interval_t (Drul_array<T> const &src)
76 Interval_t (T m, T M) : Drul_array<T> (m, M)
79 Interval_t<T> &operator -= (T r)
85 Interval_t<T> &operator += (T r)
91 Interval_t<T> &operator *= (T r)
103 Real linear_combination (Real x) const;
104 string to_string () const;
106 bool contains (T r) const;
118 at (LEFT) = at (RIGHT);
122 static bool left_less (Interval_t<T> const &a, Interval_t<T> const &b)
124 return a[LEFT] < b[RIGHT];
129 inclusion ordering. Crash if not comparable.
132 int Interval__compare (const Interval_t<T> &, Interval_t<T> const &);
135 Inclusion ordering. return -2 if not comparable
139 _Interval__compare (const Interval_t<T> &a, Interval_t<T> const &b);
145 #include "compare.hh"
147 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T> &, Interval__compare, template<class T>);
151 intersection (Interval_t<T> a, Interval_t<T> const &b)
159 Interval_t<T> operator + (T a, Interval_t<T> i)
167 Interval_t<T> operator - (Interval_t<T> i, T a)
175 Interval_t<T> operator - (T a, Interval_t<T> i)
184 Interval_t<T> operator + (Interval_t<T> i, T a)
191 Interval_t<T> operator * (T a, Interval_t<T> i)
199 Interval_t<T> operator * (Interval_t<T> i, T a)
206 Interval_t<T>::center () const
208 assert (!is_empty ());
209 return (at (LEFT) + at (RIGHT)) / T (2);
212 typedef Interval_t<Real> Interval;
213 typedef Interval_t<int> Slice; // weird name
216 #endif // INTERVAL_HH