]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/interval.hh
*** empty log message ***
[lilypond.git] / flower / include / interval.hh
1 /*
2   interval.hh -- part of flowerlib
3   
4   (c) 1996--2004 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 template<class T>
19 struct Interval_t : public Drul_array<T>
20 {
21   Drul_array<T>::elem;
22   Drul_array<T>::elem_ref;
23
24   static T infinity ();
25   static String T_to_string (T arg);
26   T center () const;
27   void translate (T t)
28     {
29       elem_ref (LEFT) += t;
30       elem_ref (RIGHT) += t;
31     }
32   void widen (T t)
33   {
34     elem_ref (LEFT) -= t;
35     elem_ref (RIGHT) += t;    
36   }
37   
38   /**
39     PRE
40     *this and h are comparable
41     */
42   void unite (Interval_t<T> h);
43   void intersect (Interval_t<T> h);
44   void add_point (T p)
45   {
46     elem_ref(LEFT) = elem (LEFT) <? p;
47     elem_ref(RIGHT) = elem (RIGHT) >? p;
48   }
49   T length () const;
50   T delta () const;
51   void set_empty ();
52   void set_full ();
53
54   /*
55     TODO: strip hungarian suffix.
56    */
57   bool is_empty () const
58   {
59     return elem (LEFT) > elem (RIGHT);
60   }
61   bool superset (Interval_t<T> const&) const;
62   Interval_t ()
63   {
64     set_empty ();
65   }
66   Interval_t (T m, T M) : Drul_array<T> (m,M)
67     {
68     }
69   Interval_t<T> &operator -= (T r) {
70     *this += -r;
71     return *this;
72   }
73
74   Interval_t<T> &operator += (T r) {
75     elem_ref (LEFT) += r;
76     elem_ref (RIGHT) +=r;
77     return *this;
78   }
79   Interval_t<T> &operator *= (T r) {
80     if (!is_empty ())
81       {
82         elem_ref (LEFT) *= r;
83         elem_ref (RIGHT) *= r;
84         if (r < T (0))
85           swap();
86
87       }
88     return *this;
89   }
90
91   Real linear_combination (Real x) const
92   {
93     Drul_array<Real> da (elem (LEFT), elem (RIGHT));
94     return ::linear_combination (da, x);
95   }
96   String to_string () const;
97
98   bool contains (T r);
99   void negate ()
100   {
101     T r = -elem (LEFT);
102     T l = -elem (RIGHT);
103     elem_ref (LEFT) = l;
104     elem_ref (RIGHT) =r;
105   }
106
107   void swap ()
108   {
109     T t = elem (LEFT);
110     elem_ref (LEFT) = elem (RIGHT);
111     elem_ref (RIGHT) = t;
112   }
113 };
114
115
116 /**
117   inclusion ordering. Crash if not  comparable.
118   */
119 template<class T>
120 int Interval__compare (const Interval_t<T>&,Interval_t<T> const&);
121
122 /**
123    Inclusion ordering.  return -2 if not comparable
124  */
125 template<class T>
126 int
127 _Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b);
128
129
130 /*
131   INLINE
132  */
133
134 #include "compare.hh"
135
136 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T>&, Interval__compare, template<class T>);
137
138
139 template<class T>
140 inline Interval_t<T>
141 intersection (Interval_t<T> a, Interval_t<T> const&b)
142 {
143   a.intersect (b);
144   return a;
145     
146 }
147
148 template<class T>
149 inline
150 Interval_t<T> operator + (T a,Interval_t<T> i)
151 {
152   i += a;
153   return i;
154 }
155
156 template<class T>
157 inline
158 Interval_t<T> operator - (Interval_t<T> i, T a)
159 {
160   i += -a;
161   return i;
162 }
163
164 template<class T>
165 inline
166 Interval_t<T> operator - (T a,Interval_t<T> i)
167 {
168   i.negate ();
169   i += a;
170   return i;
171 }
172
173 template<class T>
174 inline
175 Interval_t<T> operator + (Interval_t<T> i,T a){
176   return a+i;
177 }
178
179 template<class T>
180 inline
181 Interval_t<T> operator * (T a,Interval_t<T> i)
182 {
183   i *= a;
184   return i;
185 }
186
187 template<class T>
188 inline
189 Interval_t<T> operator * (Interval_t<T> i,T a){
190   return a*i;
191 }
192
193
194 template<class T>
195 inline T
196 Interval_t<T>::center () const
197 {
198   assert (!is_empty ());
199   return (elem (LEFT) + elem (RIGHT)) / T (2);
200 }
201
202 // again? see flower-proto.hh
203 typedef Interval_t<Real> Interval;
204 typedef Interval_t<int> Slice;  // weird name
205
206
207 #endif // INTERVAL_HH
208