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