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