X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=flower%2Finclude%2Finterval.hh;h=8160b37c2ca4b943cca8cbd4805642e3e0ca462a;hb=47db9a3883d726ca53e2133a3b2298f78dd6a32e;hp=c4e9d4e718de4322915e999db7b9d21eaa2a26d0;hpb=f93ba9a50c9da04f9ffd8944753f60e6513c5730;p=lilypond.git diff --git a/flower/include/interval.hh b/flower/include/interval.hh index c4e9d4e718..8160b37c2c 100644 --- a/flower/include/interval.hh +++ b/flower/include/interval.hh @@ -1,144 +1,180 @@ /* - interval.hh -- part of flowerlib - - (c) 1996 Han-Wen Nienhuys + This file is part of LilyPond, the GNU music typesetter. + + Copyright (C) 1996--2015 Han-Wen Nienhuys + + LilyPond is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + LilyPond is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LilyPond. If not, see . */ #ifndef INTERVAL_HH #define INTERVAL_HH -#include +#include + #include "flower-proto.hh" -#include "real.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 explicitly. */ template -struct Interval_t : public Drul_array { - - /* ************** */ - - static T infinity () ; - static String T_to_str (T arg); - T center () const { - assert (!empty_b ()); - return (elem (LEFT) + elem (RIGHT)) / T (2); - } +struct Interval_t : public Drul_array +{ + using Drul_array::at; + + static T infinity (); + static string T_to_string (T arg); + T center () const; void translate (T t) - { - elem (LEFT) += t; - elem (RIGHT) += t; - } + { + at (LEFT) += t; + at (RIGHT) += t; + } void widen (T t) { - elem (LEFT) -= t; - elem (RIGHT) += t; + at (LEFT) -= t; + at (RIGHT) += t; + } + + 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); } - /** - 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(LEFT) = elem (LEFT) ? p; + void add_point (T p) + { + at (LEFT) = min (at (LEFT), p); + at (RIGHT) = max (at (RIGHT), p); } T length () const; + + // Returns RIGHT - LEFT, even if the interval is empty. T delta () const; - void set_empty () ; - bool empty_b () const { return elem (LEFT) > elem (RIGHT); } - bool contains_b (Interval_t const&) const; - Interval_t () { + void set_empty (); + void set_full (); + + void unite_disjoint (Interval_t h, T padding, Direction d); + Interval_t union_disjoint (Interval_t h, T padding, Direction d) const; + + bool is_empty () const + { + return at (LEFT) > at (RIGHT); + } + 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 (LEFT) += r; - elem (RIGHT) +=r; + Interval_t &operator += (T r) + { + at (LEFT) += r; + at (RIGHT) += r; return *this; } - Interval_t &operator *= (T r) { - if (!empty_b ()) + Interval_t &operator *= (T r) + { + if (!is_empty ()) { - elem (LEFT) *= r; - elem (RIGHT) *= r; - if (r < T (0)) - swap(); - + at (LEFT) *= r; + at (RIGHT) *= r; + if (r < T (0)) + swap (); } return *this; } - Real linear_combination (Real x) const { - return ((1.0 - x) * Real (elem (LEFT)) + (x + 1.0) * Real (elem (RIGHT))) * 0.5; - } - String str () const; - - bool elem_b (T r); - void negate () { - T r = -elem (LEFT); - T l = -elem (RIGHT); - elem (LEFT) = l; - elem (RIGHT) =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 = elem (LEFT); - elem (LEFT) = elem (RIGHT); - elem (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[LEFT]; + } +}; /** - 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; @@ -154,7 +190,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; @@ -163,13 +199,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; @@ -177,13 +214,21 @@ 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 (at (LEFT) + at (RIGHT)) / T (2); } -// again? see flower-proto.hh typedef Interval_t Interval; -typedef Interval_t Slice; // weird name +typedef Interval_t Slice; // weird name #endif // INTERVAL_HH