2 interval.tcc -- implement Interval_t
4 source file of the Flower Library
6 (c) 1996--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
15 #include "interval.hh"
21 _Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b)
23 if (a.elem (LEFT) == b.elem (LEFT) && a.elem (RIGHT) == b.elem (RIGHT))
26 if (a.elem (LEFT) <= b.elem (LEFT) && a.elem (RIGHT) >= b.elem (RIGHT))
29 if (a.elem (LEFT) >= b.elem (LEFT) && a.elem (RIGHT) <= b.elem (RIGHT))
37 Interval_t<T>::superset (Interval_t<T> const& a) const
39 int c_i = _Interval__compare (*this, a);
47 Interval__compare (Interval_t<T> const& a,Interval_t<T> const& b)
49 int i = _Interval__compare (a,b);
57 Interval_t<T>::set_empty ()
59 elem_ref (LEFT) = (T) infinity ();
60 elem_ref (RIGHT) = (T) -infinity ();
65 Interval_t<T>::set_full ()
67 elem_ref (LEFT) = (T) -infinity ();
68 elem_ref (RIGHT) = (T) infinity ();
73 Interval_t<T>::length () const
75 if (elem (RIGHT) <= elem (LEFT))
78 return elem (RIGHT) - elem (LEFT);
83 Interval_t<T>::delta () const
85 return elem (RIGHT) - elem (LEFT);
88 /* smallest Interval which includes *this and #h# */
91 Interval_t<T>::unite (Interval_t<T> h)
93 elem_ref (LEFT) = h.elem (LEFT) <? elem (LEFT);
94 elem_ref (RIGHT) = h.elem (RIGHT) >? elem (RIGHT);
99 Interval_t<T>::intersect (Interval_t<T> h)
101 #if defined (__GNUG__) && !defined (__STRICT_ANSI__)
102 elem_ref (LEFT) = h.elem (LEFT) >? elem (LEFT);
103 elem_ref (RIGHT) = h.elem (RIGHT) <? elem (RIGHT);
105 elem_ref (LEFT) = max (h.elem (LEFT), elem (LEFT));
106 elem_ref (RIGHT) = min (h.elem (RIGHT), elem (RIGHT));
110 #if 0 //this is called intersection
113 intersect (Interval_t<T> x, Interval_t<T> const &y)
122 Interval_t<T>::to_string () const
128 return (s + T_to_string (elem (LEFT)) + String ("," )
129 + T_to_string (elem (RIGHT) ) + String ("]" ));
134 Interval_t<T>::contains (T r)
136 return r >= elem (LEFT) && r <= elem (RIGHT);
139 #define INTERVAL__INSTANTIATE(T) struct Interval_t<T>;\
140 template int Interval__compare (const Interval_t<T>&,Interval_t<T> const&)
142 #endif // INTERVAL_TCC