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