2 interval.hh -- part of flowerlib
4 (c) 1996--2006 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);
63 TODO: strip hungarian suffix.
65 bool is_empty () const
67 return at (LEFT) > at (RIGHT);
69 bool superset (Interval_t<T> const &) const;
74 Interval_t (Drul_array<T> const &src)
79 Interval_t (T m, T M) : Drul_array<T> (m, M)
82 Interval_t<T> &operator -= (T r)
88 Interval_t<T> &operator += (T r)
94 Interval_t<T> &operator *= (T r)
106 Real linear_combination (Real x) const
108 Drul_array<Real> da (at (LEFT), at (RIGHT));
109 return ::linear_combination (da, x);
111 string to_string () const;
113 bool contains (T r) const;
125 at (LEFT) = at (RIGHT);
129 static bool left_less (Interval_t<T> const &a, Interval_t<T> const &b)
131 return a[LEFT] < b[RIGHT];
136 inclusion ordering. Crash if not comparable.
139 int Interval__compare (const Interval_t<T> &, Interval_t<T> const &);
142 Inclusion ordering. return -2 if not comparable
146 _Interval__compare (const Interval_t<T> &a, Interval_t<T> const &b);
152 #include "compare.hh"
154 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T> &, Interval__compare, template<class T>);
158 intersection (Interval_t<T> a, Interval_t<T> const &b)
166 Interval_t<T> operator + (T a, Interval_t<T> i)
174 Interval_t<T> operator - (Interval_t<T> i, T a)
182 Interval_t<T> operator - (T a, Interval_t<T> i)
191 Interval_t<T> operator + (Interval_t<T> i, T a)
198 Interval_t<T> operator * (T a, Interval_t<T> i)
206 Interval_t<T> operator * (Interval_t<T> i, T a)
213 Interval_t<T>::center () const
215 assert (!is_empty ());
216 return (at (LEFT) + at (RIGHT)) / T (2);
219 typedef Interval_t<Real> Interval;
220 typedef Interval_t<int> Slice; // weird name
223 #endif // INTERVAL_HH