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