X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=flower%2Finclude%2Finterval.hh;h=27e221af8c7d61a3d937a7971941adec7fb2364c;hb=39c0e8adb498996f5e414b1d0bc3a20ac81aa619;hp=1875e7ce6620a5073dffa0c0e8a204c0c2ee0144;hpb=c659cb200486c2f908703696a1b2873e78c8160a;p=lilypond.git diff --git a/flower/include/interval.hh b/flower/include/interval.hh index 1875e7ce66..27e221af8c 100644 --- a/flower/include/interval.hh +++ b/flower/include/interval.hh @@ -1,160 +1,162 @@ /* interval.hh -- part of flowerlib - - (c) 1996--2004 Han-Wen Nienhuys + + (c) 1996--2009 Han-Wen Nienhuys */ #ifndef INTERVAL_HH #define INTERVAL_HH +#include + #include "flower-proto.hh" #include "drul-array.hh" /* A T interval. This represents the closed interval [left,right]. No invariants. T must be a totally ordered ring (with division, anyway ..) - At instantiation, the function infinity () has to be defined explicitely. */ + At instantiation, the function infinity () has to be defined explicitly. */ template struct Interval_t : public Drul_array { - Drul_array::elem; - Drul_array::elem_ref; + Drul_array::at; static T infinity (); - static String T_to_string (T arg); + static string T_to_string (T arg); T center () const; void translate (T t) - { - elem_ref (LEFT) += t; - elem_ref (RIGHT) += t; - } + { + at (LEFT) += t; + at (RIGHT) += t; + } void widen (T t) { - elem_ref (LEFT) -= t; - elem_ref (RIGHT) += t; + at (LEFT) -= t; + at (RIGHT) += t; } - + T distance (T t) const { - if (t > elem (RIGHT)) - return T (t - elem (RIGHT)); - else if (t < elem (LEFT)) - return T (elem (LEFT) - t); + if (t > at (RIGHT)) + return T (t - at (RIGHT)); + else if (t < at (LEFT)) + return T (at (LEFT) - t); else return T (0); } /** - PRE - *this and h are comparable - */ + PRE + *this and h are comparable + */ void unite (Interval_t h); void intersect (Interval_t h); void add_point (T p) { - elem_ref(LEFT) = elem (LEFT) ? p; + at (LEFT) = min (at (LEFT), p); + at (RIGHT) = max (at (RIGHT), p); } T length () const; T delta () const; void set_empty (); void set_full (); - /* - TODO: strip hungarian suffix. - */ bool is_empty () const { - return elem (LEFT) > elem (RIGHT); + return at (LEFT) > at (RIGHT); } - bool superset (Interval_t const&) const; + bool superset (Interval_t const &) const; Interval_t () { set_empty (); } - Interval_t (T m, T M) : Drul_array (m,M) - { - } - Interval_t &operator -= (T r) { + Interval_t (Drul_array const &src) + : Drul_array (src) + { + } + + Interval_t (T m, T M) : Drul_array (m, M) + { + } + Interval_t &operator -= (T r) + { *this += -r; return *this; } - Interval_t &operator += (T r) { - elem_ref (LEFT) += r; - elem_ref (RIGHT) +=r; + Interval_t &operator += (T r) + { + at (LEFT) += r; + at (RIGHT) += r; return *this; } - Interval_t &operator *= (T r) { + Interval_t &operator *= (T r) + { if (!is_empty ()) { - elem_ref (LEFT) *= r; - elem_ref (RIGHT) *= r; + at (LEFT) *= r; + at (RIGHT) *= r; if (r < T (0)) - swap(); - + swap (); } return *this; } - Real linear_combination (Real x) const - { - Drul_array da (elem (LEFT), elem (RIGHT)); - return ::linear_combination (da, x); - } - String to_string () const; + Real linear_combination (Real x) const; + string to_string () const; - bool contains (T r); + bool contains (T r) const; void negate () { - T r = -elem (LEFT); - T l = -elem (RIGHT); - elem_ref (LEFT) = l; - elem_ref (RIGHT) =r; + T r = -at (LEFT); + T l = -at (RIGHT); + at (LEFT) = l; + at (RIGHT) = r; } void swap () { - T t = elem (LEFT); - elem_ref (LEFT) = elem (RIGHT); - elem_ref (RIGHT) = t; + T t = at (LEFT); + at (LEFT) = at (RIGHT); + at (RIGHT) = t; } -}; + static bool left_less (Interval_t const &a, Interval_t const &b) + { + return a[LEFT] < b[RIGHT]; + } +}; /** - inclusion ordering. Crash if not comparable. - */ + inclusion ordering. Crash if not comparable. +*/ template -int Interval__compare (const Interval_t&,Interval_t const&); +int Interval__compare (const Interval_t &, Interval_t const &); /** Inclusion ordering. return -2 if not comparable - */ +*/ template int -_Interval__compare (const Interval_t&a,Interval_t const&b); - +_Interval__compare (const Interval_t &a, Interval_t const &b); /* INLINE - */ +*/ #include "compare.hh" -TEMPLATE_INSTANTIATE_COMPARE (Interval_t&, Interval__compare, template); - +TEMPLATE_INSTANTIATE_COMPARE (Interval_t &, Interval__compare, template); template inline Interval_t -intersection (Interval_t a, Interval_t const&b) +intersection (Interval_t a, Interval_t const &b) { a.intersect (b); return a; - } template inline -Interval_t operator + (T a,Interval_t i) +Interval_t operator + (T a, Interval_t i) { i += a; return i; @@ -170,7 +172,7 @@ Interval_t operator - (Interval_t i, T a) template inline -Interval_t operator - (T a,Interval_t i) +Interval_t operator - (T a, Interval_t i) { i.negate (); i += a; @@ -179,13 +181,14 @@ Interval_t operator - (T a,Interval_t i) template inline -Interval_t operator + (Interval_t i,T a){ - return a+i; +Interval_t operator + (Interval_t i, T a) +{ + return a + i; } template inline -Interval_t operator * (T a,Interval_t i) +Interval_t operator * (T a, Interval_t i) { i *= a; return i; @@ -193,20 +196,19 @@ Interval_t operator * (T a,Interval_t i) template inline -Interval_t operator * (Interval_t i,T a){ - return a*i; +Interval_t operator * (Interval_t i, T a) +{ + return a * i; } - template inline T Interval_t::center () const { assert (!is_empty ()); - return (elem (LEFT) + elem (RIGHT)) / T (2); + return (at (LEFT) + at (RIGHT)) / T (2); } -// again? see flower-proto.hh typedef Interval_t Interval; typedef Interval_t Slice; // weird name