2 interval.tcc -- implement Interval_t
4 source file of the Flower Library
6 (c) 1996--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
14 #include "interval.hh"
15 #include "std-string.hh"
17 // MacOS 10.3 problems:
23 _Interval__compare (const Interval_t<T> &a, Interval_t<T> const &b)
25 if (a.at (LEFT) == b.at (LEFT) && a.at (RIGHT) == b.at (RIGHT))
28 if (a.at (LEFT) <= b.at (LEFT) && a.at (RIGHT) >= b.at (RIGHT))
31 if (a.at (LEFT) >= b.at (LEFT) && a.at (RIGHT) <= b.at (RIGHT))
39 Interval_t<T>::superset (Interval_t<T> const &a) const
41 int c_i = _Interval__compare (*this, a);
49 Interval__compare (Interval_t<T> const &a, Interval_t<T> const &b)
51 int i = _Interval__compare (a, b);
59 Interval_t<T>::set_empty ()
61 at (LEFT) = (T) infinity ();
62 at (RIGHT) = (T) -infinity ();
67 Interval_t<T>::set_full ()
69 at (LEFT) = (T) -infinity ();
70 at (RIGHT) = (T) infinity ();
75 Interval_t<T>::length () const
77 if (at (RIGHT) <= at (LEFT))
80 return at (RIGHT) - at (LEFT);
85 Interval_t<T>::delta () const
87 return at (RIGHT) - at (LEFT);
90 /* smallest Interval which includes *this and #h# */
93 Interval_t<T>::unite (Interval_t<T> h)
95 at (LEFT) = min (h.at (LEFT), at (LEFT));
96 at (RIGHT) = max (h.at (RIGHT), at (RIGHT));
101 Interval_t<T>::intersect (Interval_t<T> h)
103 at (LEFT) = max (h.at (LEFT), at (LEFT));
104 at (RIGHT) = min (h.at (RIGHT), at (RIGHT));
109 Interval_t<T>::to_string () const
115 return (s + T_to_string (at (LEFT)) + string (",")
116 + T_to_string (at (RIGHT)) + string ("]"));
121 Interval_t<T>::contains (T r) const
123 return r >= at (LEFT) && r <= at (RIGHT);
126 #define INTERVAL__INSTANTIATE(T) struct Interval_t<T>; \
127 template int Interval__compare (const Interval_t<T> &, Interval_t<T> const &)
129 #endif // INTERVAL_TCC