]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/interval.hh
release: 0.1.48
[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 };
90
91
92 /**
93   inclusion ordering. Crash if not comparable.
94   */
95 template<class T>
96 int Interval__compare (const Interval_t<T>&,Interval_t<T> const&);
97
98 /*
99   INLINE
100  */
101
102 #include "compare.hh"
103
104 TEMPLATE_INSTANTIATE_COMPARE(Interval_t<T>&, Interval__compare, template<class T>);
105
106
107 template<class T>
108 inline Interval_t<T>
109 intersection (Interval_t<T> a, Interval_t<T> const&b)
110 {
111   a.intersect (b);
112   return a;
113     
114 }
115
116 template<class T>
117 inline
118 Interval_t<T> operator +(T a,Interval_t<T> i)
119 {
120   i += a;
121   return i;
122 }
123
124 template<class T>
125 inline
126 Interval_t<T> operator +(Interval_t<T> i,T a){
127   return a+i;
128 }
129
130 template<class T>
131 inline
132 Interval_t<T> operator *(T a,Interval_t<T> i)
133 {
134   i *= a;
135   return i;
136 }
137
138 template<class T>
139 inline
140 Interval_t<T> operator *(Interval_t<T> i,T a){
141   return a*i;
142 }
143
144 typedef Interval_t<Real> Interval;
145
146
147
148
149 #endif // INTERVAL_HH
150
151
152