X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=flower%2Finclude%2Finterval.hh;h=20f5987e430b53568973814e5d21ecddf99053fe;hb=9efbad2d9487a05b04423e7e9f062968e8f8eaf4;hp=57f6a5607a8cd87f5cc8cc242f6d7876e4c7b29c;hpb=3f374e66968308461daa390a7aeed0b013bfe33d;p=lilypond.git diff --git a/flower/include/interval.hh b/flower/include/interval.hh index 57f6a5607a..20f5987e43 100644 --- a/flower/include/interval.hh +++ b/flower/include/interval.hh @@ -12,64 +12,101 @@ #include "real.hh" -/** a T interval. - this represents the closed interval [left,right]. - No invariants. T must be a totally ordered ring +/** 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; + T left, right; - /* ************** */ + /* ************** */ - 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; - } - T& operator[](int j) { - return idx(j); - } - T operator[](int j) const { - return ((Interval_t *)this)->idx(j); - } - T &max() { return right;} - T max()const { return right;} - T min()const{ return left; } - T &min(){ return left; } - /** - PRE - *this and h are comparable - */ - void unite(Interval_t h); - void intersect(Interval_t h); - - T length() const; - void set_empty() ; - bool empty_b() const { return left > right; } - Interval_t() { - set_empty(); - } - Interval_t(T m, T M) { - left =m; - right = M; - } - Interval_t &operator += (T r) { - left += r; - right +=r; - return *this; + static T infinity() ; + static String T_to_str (T arg); + + // ugh, egcs 1.02 ices on this +// T center() { return (left + right) / T(2);} + // and can't handle this either + // anyone want to make a bug report? + // better make one soon, egcs in rh5.1 barfs on this! + T center() { + T two (2); +// return (left + right) / two; + T result ((left + right) / two); + return result; + } + 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; + } + T& operator[](int j) { + return idx (j); + } + T operator[](int j) const { + return ((Interval_t *)this)->idx (j); + } + T &max() { return right;} + T max() const { return right;} + T min() const{ return left; } + T &min(){ return left; } + /** + PRE + *this and h are comparable + */ + void unite (Interval_t h); + void intersect (Interval_t h); + + 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) { + left =m; + right = M; + } + Interval_t &operator -= (T r) { + *this += -r; + return *this; + } + + Interval_t &operator += (T r) { + left += r; + right +=r; + return *this; + } + Interval_t &operator *=(T r) { + left *= r; + right *= r; + if (r < T(0)) { + T t = left; + left = right; + right = t; } - String str() const; - bool elt_b(T r); + return *this; + } + String str() const; + void print () const; + bool elem_b (T r); + void negate () { + T r = -left; + T l = -right; + left = l; + right =r; + } }; @@ -77,7 +114,7 @@ struct Interval_t { 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&); /* INLINE @@ -85,41 +122,67 @@ int Interval__compare(const Interval_t&,Interval_t const&); #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; + 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; + 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) +{ + i += -a; + return i; } -typedef Interval_t Interval; +template +inline +Interval_t operator - (T a,Interval_t i) +{ + i.negate (); + i += a; + return i; +} +template +inline +Interval_t operator +(Interval_t i,T a){ + return a+i; +} -#define Interval__instantiate(T) template struct Interval_t;\ - template int Interval__compare(const Interval_t&,Interval_t const&) +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; +} -#endif // INTERVAL_HH +// again? see fproto.hh +typedef Interval_t Interval; +typedef Interval_t Slice; +#endif // INTERVAL_HH