]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/interval.hh
string() -> to_string()
[lilypond.git] / flower / include / interval.hh
1 /*
2   interval.hh -- part of flowerlib
3   
4   (c) 1996 Han-Wen Nienhuys
5 */
6
7 #ifndef INTERVAL_HH
8 #define INTERVAL_HH
9
10 #include <assert.h> 
11 #include "flower-proto.hh"
12 #include "real.hh"
13 #include "drul-array.hh"
14
15 /** a T interval.  this represents the closed interval [left,right].
16   No invariants. T must be a totally ordered ring (with division, anyway ..)
17   At instantiation, the function infinity () has to be defined explicitely.
18   
19   */
20 template<class T>
21 struct Interval_t : public Drul_array<T> {
22
23   /* ************** */
24     
25   static T infinity () ;
26   static String T_to_string (T arg);
27   T center () const {
28     assert (!empty_b ());
29     return (elem (LEFT) + elem (RIGHT)) / T (2);
30   }
31   void translate (T t)
32     {
33       elem (LEFT) += t;
34       elem (RIGHT) += t;
35     }
36   void widen (T t)
37   {
38     elem (LEFT) -= t;
39     elem (RIGHT) += t;    
40   }
41   
42   /**
43     PRE
44     *this and h are comparable
45     */
46   void unite (Interval_t<T> h);
47   void intersect (Interval_t<T> h);
48   void add_point (T p) {
49     elem(LEFT) = elem (LEFT) <? p;
50     elem(RIGHT) = elem (RIGHT) >? p;
51   }
52   T length () const;
53   T delta () const;
54   void set_empty () ;
55   void set_full ();
56   bool empty_b () const { return elem (LEFT) > elem (RIGHT); }
57   bool contains_b (Interval_t<T> const&) const;
58   Interval_t () {
59     set_empty ();
60   }
61   Interval_t (T m, T M) : Drul_array<T> (m,M)
62     {
63     }
64   Interval_t<T> &operator -= (T r) {
65     *this += -r;
66     return *this;
67   }
68
69   Interval_t<T> &operator += (T r) {
70     elem (LEFT) += r;
71     elem (RIGHT) +=r;
72     return *this;
73   }
74   Interval_t<T> &operator *= (T r) {
75     if (!empty_b ())
76       {
77         elem (LEFT) *= r;
78         elem (RIGHT) *= r;
79         if (r < T (0))
80           swap();
81
82       }
83     return *this;
84   }
85
86   Real linear_combination (Real x) const {
87     return ((1.0 - x) * Real (elem (LEFT))  + (x + 1.0) * Real (elem (RIGHT))) * 0.5;
88   }
89   String to_string () const;    
90
91   bool elem_b (T r);
92   void negate () {
93     T r = -elem (LEFT);
94     T l = -elem (RIGHT);
95     elem (LEFT) = l;
96     elem (RIGHT) =r;
97   }
98   
99   void swap ()
100   {
101     T t = elem (LEFT);
102     elem (LEFT) = elem (RIGHT);
103     elem (RIGHT) = t;
104   }
105 };
106
107
108 /**
109   inclusion ordering. Crash if not  comparable.
110   */
111 template<class T>
112 int Interval__compare (const Interval_t<T>&,Interval_t<T> const&);
113
114 /**
115    Inclusion ordering.  return -2 if not comparable
116  */
117 template<class T>
118 int
119 _Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b);
120
121
122 /*
123   INLINE
124  */
125
126 #include "compare.hh"
127
128 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T>&, Interval__compare, template<class T>);
129
130
131 template<class T>
132 inline Interval_t<T>
133 intersection (Interval_t<T> a, Interval_t<T> const&b)
134 {
135   a.intersect (b);
136   return a;
137     
138 }
139
140 template<class T>
141 inline
142 Interval_t<T> operator + (T a,Interval_t<T> i)
143 {
144   i += a;
145   return i;
146 }
147
148 template<class T>
149 inline
150 Interval_t<T> operator - (Interval_t<T> i, T a)
151 {
152   i += -a;
153   return i;
154 }
155
156 template<class T>
157 inline
158 Interval_t<T> operator - (T a,Interval_t<T> i)
159 {
160   i.negate ();
161   i += a;
162   return i;
163 }
164
165 template<class T>
166 inline
167 Interval_t<T> operator + (Interval_t<T> i,T a){
168   return a+i;
169 }
170
171 template<class T>
172 inline
173 Interval_t<T> operator * (T a,Interval_t<T> i)
174 {
175   i *= a;
176   return i;
177 }
178
179 template<class T>
180 inline
181 Interval_t<T> operator * (Interval_t<T> i,T a){
182   return a*i;
183 }
184
185 // again? see flower-proto.hh
186 typedef Interval_t<Real> Interval;
187 typedef Interval_t<int> Slice;  // weird name
188
189
190 #endif // INTERVAL_HH
191