2 interval.hh -- part of flowerlib
4 (c) 1996--2006 Han-Wen Nienhuys
12 #include "std-string.hh"
14 #include "flower-proto.hh"
15 #include "drul-array.hh"
17 /* A T interval. This represents the closed interval [left,right].
18 No invariants. T must be a totally ordered ring (with division, anyway ..)
19 At instantiation, the function infinity () has to be defined explicitely. */
21 struct Interval_t : public Drul_array<T>
26 static string T_to_string (T arg);
39 T distance (T t) const
42 return T (t - at (RIGHT));
43 else if (t < at (LEFT))
44 return T (at (LEFT) - t);
50 *this and h are comparable
52 void unite (Interval_t<T> h);
53 void intersect (Interval_t<T> h);
56 at (LEFT) = min (at (LEFT), p);
57 at (RIGHT) = max (at (RIGHT), p);
65 TODO: strip hungarian suffix.
67 bool is_empty () const
69 return at (LEFT) > at (RIGHT);
71 bool superset (Interval_t<T> const &) const;
76 Interval_t (Drul_array<T> const &src)
81 Interval_t (T m, T M) : Drul_array<T> (m, M)
84 Interval_t<T> &operator -= (T r)
90 Interval_t<T> &operator += (T r)
96 Interval_t<T> &operator *= (T r)
108 Real linear_combination (Real x) const
110 Drul_array<Real> da (at (LEFT), at (RIGHT));
111 return ::linear_combination (da, x);
113 string to_string () const;
115 bool contains (T r) const;
127 at (LEFT) = at (RIGHT);
131 static int left_comparison (Interval_t<T> const &a, Interval_t<T> const &b)
133 return sign (a[LEFT] - b[RIGHT]);
138 inclusion ordering. Crash if not comparable.
141 int Interval__compare (const Interval_t<T> &, Interval_t<T> const &);
144 Inclusion ordering. return -2 if not comparable
148 _Interval__compare (const Interval_t<T> &a, Interval_t<T> const &b);
154 #include "compare.hh"
156 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T> &, Interval__compare, template<class T>);
160 intersection (Interval_t<T> a, Interval_t<T> const &b)
168 Interval_t<T> operator + (T a, Interval_t<T> i)
176 Interval_t<T> operator - (Interval_t<T> i, T a)
184 Interval_t<T> operator - (T a, Interval_t<T> i)
193 Interval_t<T> operator + (Interval_t<T> i, T a)
200 Interval_t<T> operator * (T a, Interval_t<T> i)
208 Interval_t<T> operator * (Interval_t<T> i, T a)
215 Interval_t<T>::center () const
217 assert (!is_empty ());
218 return (at (LEFT) + at (RIGHT)) / T (2);
221 typedef Interval_t<Real> Interval;
222 typedef Interval_t<int> Slice; // weird name
225 #endif // INTERVAL_HH