]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/interval.hh
release: 1.0.1
[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 "fproto.hh"
12 #include "real.hh"
13
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 {
22   T left, right;
23
24   /* ************** */
25     
26   static T infinity() ;
27   static String T_to_str (T arg);
28     
29   // ugh, egcs 1.02 ices on this
30 //  T center() { return (left + right) / T(2);}
31   // and can't handle this either
32   // anyone want to make a bug report?
33   // better make one soon, egcs in rh5.1 barfs on this!
34   T center() {
35     T two (2);
36 //    return (left + right) / two;
37     T result ((left + right) / two);
38     return result;
39   }
40   void translate (T t) {
41     left += t;
42     right += t;
43   }
44   T& idx (int j) {
45     if (j==-1)
46       return left;
47     else if (j==1)
48       return right;
49     else
50       assert (false);
51     return left;                
52   }
53   T& operator[](int j) {
54     return idx (j);
55   }
56   T operator[](int j) const {
57     return ((Interval_t<T> *)this)->idx (j);
58   }
59   T &max() { return right;}
60   T max() const { return right;}
61   T min() const{ return left; }
62   T &min(){ return left; }
63   /**
64     PRE
65     *this and h are comparable
66     */
67   void unite (Interval_t<T> h);
68   void intersect (Interval_t<T> h);
69
70   T length() const;
71   void set_empty() ;
72   bool empty_b() const { return left > right; }
73   bool contains_b (Interval_t<T> const&) const;
74   Interval_t() {
75     set_empty();
76   }
77   Interval_t (T m, T M) {
78     left =m;
79     right = M;
80   }
81   Interval_t<T> &operator -= (T r) {
82     *this += -r;
83     return *this;
84   }
85
86   Interval_t<T> &operator += (T r) {
87     left += r;
88     right +=r;
89     return *this;
90   }
91   Interval_t<T> &operator *=(T r) {
92     left *= r;
93     right *= r;
94     if (r < T(0)) {
95       T t = left;
96       left = right;
97       right = t;
98     }
99     return *this;
100   }
101   String str() const;    
102   void print () const;
103   bool elt_b (T r);
104   void negate () {
105     T r = -left;
106     T l = -right;
107     left = l;
108     right =r;
109   }
110 };
111
112
113 /**
114   inclusion ordering. Crash if not comparable.
115   */
116 template<class T>
117 int Interval__compare (const Interval_t<T>&,Interval_t<T> const&);
118
119 /*
120   INLINE
121  */
122
123 #include "compare.hh"
124
125 TEMPLATE_INSTANTIATE_COMPARE(Interval_t<T>&, Interval__compare, template<class T>);
126
127
128 template<class T>
129 inline Interval_t<T>
130 intersection (Interval_t<T> a, Interval_t<T> const&b)
131 {
132   a.intersect (b);
133   return a;
134     
135 }
136
137 template<class T>
138 inline
139 Interval_t<T> operator +(T a,Interval_t<T> i)
140 {
141   i += a;
142   return i;
143 }
144
145 template<class T>
146 inline
147 Interval_t<T> operator - (Interval_t<T> i, T a)
148 {
149   i += -a;
150   return i;
151 }
152
153 template<class T>
154 inline
155 Interval_t<T> operator - (T a,Interval_t<T> i)
156 {
157   i.negate ();
158   i += a;
159   return i;
160 }
161
162 template<class T>
163 inline
164 Interval_t<T> operator +(Interval_t<T> i,T a){
165   return a+i;
166 }
167
168 template<class T>
169 inline
170 Interval_t<T> operator *(T a,Interval_t<T> i)
171 {
172   i *= a;
173   return i;
174 }
175
176 template<class T>
177 inline
178 Interval_t<T> operator *(Interval_t<T> i,T a){
179   return a*i;
180 }
181
182 // again? see fproto.hh
183 typedef Interval_t<Real> Interval;
184 typedef Interval_t<int> Slice;
185
186
187 #endif // INTERVAL_HH
188