/* interval.tcc -- implement Interval_t source file of the Flower Library (c) 1996--2005 Han-Wen Nienhuys */ #ifndef INTERVAL_TCC #define INTERVAL_TCC #include #include #include "interval.hh" #include "string.hh" template int _Interval__compare (const Interval_t&a, Interval_t const &b) { if (a.elem (LEFT) == b.elem (LEFT) && a.elem (RIGHT) == b.elem (RIGHT)) return 0; if (a.elem (LEFT) <= b.elem (LEFT) && a.elem (RIGHT) >= b.elem (RIGHT)) return 1; if (a.elem (LEFT) >= b.elem (LEFT) && a.elem (RIGHT) <= b.elem (RIGHT)) return -1; return -2; } template bool Interval_t::superset (Interval_t const &a) const { int c_i = _Interval__compare (*this, a); if (c_i == -2) return false; return c_i >= 0; } template int Interval__compare (Interval_t const &a, Interval_t const &b) { int i = _Interval__compare (a, b); if (i < -1) assert (false); return i; } template void Interval_t::set_empty () { elem_ref (LEFT) = (T) infinity (); elem_ref (RIGHT) = (T) -infinity (); } template void Interval_t::set_full () { elem_ref (LEFT) = (T) -infinity (); elem_ref (RIGHT) = (T) infinity (); } template T Interval_t::length () const { if (elem (RIGHT) <= elem (LEFT)) return 0; else return elem (RIGHT) - elem (LEFT); } template T Interval_t::delta () const { return elem (RIGHT) - elem (LEFT); } /* smallest Interval which includes *this and #h# */ template void Interval_t::unite (Interval_t h) { elem_ref (LEFT) = h.elem (LEFT) ? elem (RIGHT); } template void Interval_t::intersect (Interval_t h) { #if defined (__GNUG__) && !defined (__STRICT_ANSI__) elem_ref (LEFT) = h.elem (LEFT) >? elem (LEFT); elem_ref (RIGHT) = h.elem (RIGHT) Interval_t intersect (Interval_t x, Interval_t const &y) { x.intersect (y); return x; } #endif template String Interval_t::to_string () const { if (is_empty ()) return "[empty]"; String s ("["); return (s + T_to_string (elem (LEFT)) + String (",") + T_to_string (elem (RIGHT)) + String ("]")); } template bool Interval_t::contains (T r) { return r >= elem (LEFT) && r <= elem (RIGHT); } #define INTERVAL__INSTANTIATE(T) struct Interval_t; \ template int Interval__compare (const Interval_t&, Interval_t const &) #endif // INTERVAL_TCC