]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/interval.hh
* lily/system-start-delimiter.cc (after_line_breaking): retrieve
[lilypond.git] / flower / include / interval.hh
1 /*
2   interval.hh -- part of flowerlib
3   
4   (c)  1996--2003 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
57   /*
58     TODO: strip hungarian suffix.
59    */
60   bool empty_b () const { return elem (LEFT) > elem (RIGHT); }
61   bool contains_b (Interval_t<T> const&) const;
62   Interval_t () {
63     set_empty ();
64   }
65   Interval_t (T m, T M) : Drul_array<T> (m,M)
66     {
67     }
68   Interval_t<T> &operator -= (T r) {
69     *this += -r;
70     return *this;
71   }
72
73   Interval_t<T> &operator += (T r) {
74     elem (LEFT) += r;
75     elem (RIGHT) +=r;
76     return *this;
77   }
78   Interval_t<T> &operator *= (T r) {
79     if (!empty_b ())
80       {
81         elem (LEFT) *= r;
82         elem (RIGHT) *= r;
83         if (r < T (0))
84           swap();
85
86       }
87     return *this;
88   }
89
90   Real linear_combination (Real x) const {
91     return ((1.0 - x) * Real (elem (LEFT))  + (x + 1.0) * Real (elem (RIGHT))) * 0.5;
92   }
93   String to_string () const;    
94
95   bool elem_b (T r);
96   void negate () {
97     T r = -elem (LEFT);
98     T l = -elem (RIGHT);
99     elem (LEFT) = l;
100     elem (RIGHT) =r;
101   }
102   
103   void swap ()
104   {
105     T t = elem (LEFT);
106     elem (LEFT) = elem (RIGHT);
107     elem (RIGHT) = t;
108   }
109 };
110
111
112 /**
113   inclusion ordering. Crash if not  comparable.
114   */
115 template<class T>
116 int Interval__compare (const Interval_t<T>&,Interval_t<T> const&);
117
118 /**
119    Inclusion ordering.  return -2 if not comparable
120  */
121 template<class T>
122 int
123 _Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b);
124
125
126 /*
127   INLINE
128  */
129
130 #include "compare.hh"
131
132 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T>&, Interval__compare, template<class T>);
133
134
135 template<class T>
136 inline Interval_t<T>
137 intersection (Interval_t<T> a, Interval_t<T> const&b)
138 {
139   a.intersect (b);
140   return a;
141     
142 }
143
144 template<class T>
145 inline
146 Interval_t<T> operator + (T a,Interval_t<T> i)
147 {
148   i += a;
149   return i;
150 }
151
152 template<class T>
153 inline
154 Interval_t<T> operator - (Interval_t<T> i, T a)
155 {
156   i += -a;
157   return i;
158 }
159
160 template<class T>
161 inline
162 Interval_t<T> operator - (T a,Interval_t<T> i)
163 {
164   i.negate ();
165   i += a;
166   return i;
167 }
168
169 template<class T>
170 inline
171 Interval_t<T> operator + (Interval_t<T> i,T a){
172   return a+i;
173 }
174
175 template<class T>
176 inline
177 Interval_t<T> operator * (T a,Interval_t<T> i)
178 {
179   i *= a;
180   return i;
181 }
182
183 template<class T>
184 inline
185 Interval_t<T> operator * (Interval_t<T> i,T a){
186   return a*i;
187 }
188
189 // again? see flower-proto.hh
190 typedef Interval_t<Real> Interval;
191 typedef Interval_t<int> Slice;  // weird name
192
193
194 #endif // INTERVAL_HH
195