X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=flower%2Finclude%2Finterval.hh;h=9808bfaef652768c93d8bb97dd0b1f02716fd030;hb=5b4b0d6e9a197e8f9eb085b7c2ad78b8be3e5cfc;hp=d236759219e1f66887b988b4111cfb1739bda840;hpb=6a1295eaacbc3e6b5a7ce19f7f942b23f8d7f2ff;p=lilypond.git diff --git a/flower/include/interval.hh b/flower/include/interval.hh index d236759219..9808bfaef6 100644 --- a/flower/include/interval.hh +++ b/flower/include/interval.hh @@ -1,120 +1,162 @@ /* interval.hh -- part of flowerlib - - (c) 1996 Han-Wen Nienhuys + + (c) 1996--2008 Han-Wen Nienhuys */ #ifndef INTERVAL_HH #define INTERVAL_HH -#include -#include "fproto.hh" -#include "real.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. - - */ +/* 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. */ template -struct Interval_t { - T left, right; - - /* ************** */ - - static T infinity() ; - - T center() { return (left + right) / T(2);} - void translate (T t) { - left += t; - right += t; - } - T& idx (int j) { - if (j==-1) - return left; - else if (j==1) - return right; - else - assert (false); - return left; +struct Interval_t : public Drul_array +{ + Drul_array::at; + + static T infinity (); + static string T_to_string (T arg); + T center () const; + void translate (T t) + { + at (LEFT) += t; + at (RIGHT) += t; } - T& operator[](int j) { - return idx (j); + void widen (T t) + { + at (LEFT) -= t; + at (RIGHT) += t; } - T operator[](int j) const { - return ((Interval_t *)this)->idx (j); + + T distance (T t) const + { + if (t > at (RIGHT)) + return T (t - at (RIGHT)); + else if (t < at (LEFT)) + return T (at (LEFT) - t); + else + return T (0); } - T &max() { return right;} - T max() const { return right;} - T min() const{ return left; } - T &min(){ return left; } /** - 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) + { + at (LEFT) = min (at (LEFT), p); + at (RIGHT) = max (at (RIGHT), p); + } + T length () const; + T delta () const; + void set_empty (); + void set_full (); + + bool is_empty () const + { + return at (LEFT) > at (RIGHT); + } + bool superset (Interval_t const &) const; + Interval_t () + { + set_empty (); + } + Interval_t (Drul_array const &src) + : Drul_array (src) + { + } - T length() const; - void set_empty() ; - bool empty_b() const { return left > right; } - bool contains_b (Interval_t const&) const; - Interval_t() { - set_empty(); + Interval_t (T m, T M) : Drul_array (m, M) + { } - Interval_t (T m, T M) { - left =m; - right = M; + Interval_t &operator -= (T r) + { + *this += -r; + return *this; } - Interval_t &operator += (T r) { - left += r; - right +=r; + + Interval_t &operator += (T r) + { + at (LEFT) += r; + at (RIGHT) += r; return *this; } - Interval_t &operator *=(T r) { - left *= r; - right *= r; - if (r < T(0)) { - T t = left; - left = right; - right = t; - } + Interval_t &operator *= (T r) + { + if (!is_empty ()) + { + at (LEFT) *= r; + at (RIGHT) *= r; + if (r < T (0)) + swap (); + } return *this; } - String str() const; - void print () const; - bool elt_b (T r); + + Real linear_combination (Real x) const; + string to_string () const; + + bool contains (T r) const; + void negate () + { + T r = -at (LEFT); + T l = -at (RIGHT); + at (LEFT) = l; + at (RIGHT) = r; + } + + void swap () + { + 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. +*/ +template +int Interval__compare (const Interval_t &, Interval_t const &); /** - inclusion ordering. Crash if not comparable. - */ + Inclusion ordering. return -2 if not comparable +*/ template -int Interval__compare (const Interval_t&,Interval_t const&); +int +_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; @@ -122,30 +164,54 @@ 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) +{ + i += -a; + return i; } template inline -Interval_t operator *(T a,Interval_t i) +Interval_t operator - (T a, Interval_t i) { - i *= a; + i.negate (); + i += a; return 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; } -typedef Interval_t Interval; - +template +inline +Interval_t operator * (T a, Interval_t i) +{ + i *= a; + return i; +} +template +inline +Interval_t operator * (Interval_t i, T a) +{ + return a * i; +} +template +inline T +Interval_t::center () const +{ + assert (!is_empty ()); + return (at (LEFT) + at (RIGHT)) / T (2); +} -#endif // INTERVAL_HH +typedef Interval_t Interval; +typedef Interval_t Slice; // weird name +#endif // INTERVAL_HH