]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/interval.hh
patch::: 1.3.136.jcn3
[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     if (!empty_b ())
62       {
63         elem (LEFT) *= r;
64         elem (RIGHT) *= r;
65         if (r < T (0)) {
66           T t = elem (LEFT);
67           elem (LEFT) = elem (RIGHT);
68           elem (RIGHT) = t;
69         }
70       }
71     return *this;
72   }
73
74   Real linear_combination (Real x) const {
75     return ((1.0 - x) * Real (elem (LEFT))  + (x + 1.0) * Real (elem (RIGHT))) * 0.5;
76   }
77   String str () const;    
78   void print () const;
79   bool elem_b (T r);
80   void negate () {
81     T r = -elem (LEFT);
82     T l = -elem (RIGHT);
83     elem (LEFT) = l;
84     elem (RIGHT) =r;
85   }
86 };
87
88
89 /**
90   inclusion ordering. Crash if not  comparable.
91   */
92 template<class T>
93 int Interval__compare (const Interval_t<T>&,Interval_t<T> const&);
94
95 /**
96    Inclusion ordering.  return -2 if not comparable
97  */
98 template<class T>
99 int
100 _Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b);
101
102
103 /*
104   INLINE
105  */
106
107 #include "compare.hh"
108
109 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T>&, Interval__compare, template<class T>);
110
111
112 template<class T>
113 inline Interval_t<T>
114 intersection (Interval_t<T> a, Interval_t<T> const&b)
115 {
116   a.intersect (b);
117   return a;
118     
119 }
120
121 template<class T>
122 inline
123 Interval_t<T> operator + (T a,Interval_t<T> i)
124 {
125   i += a;
126   return i;
127 }
128
129 template<class T>
130 inline
131 Interval_t<T> operator - (Interval_t<T> i, T a)
132 {
133   i += -a;
134   return i;
135 }
136
137 template<class T>
138 inline
139 Interval_t<T> operator - (T a,Interval_t<T> i)
140 {
141   i.negate ();
142   i += a;
143   return i;
144 }
145
146 template<class T>
147 inline
148 Interval_t<T> operator + (Interval_t<T> i,T a){
149   return a+i;
150 }
151
152 template<class T>
153 inline
154 Interval_t<T> operator * (T a,Interval_t<T> i)
155 {
156   i *= a;
157   return i;
158 }
159
160 template<class T>
161 inline
162 Interval_t<T> operator * (Interval_t<T> i,T a){
163   return a*i;
164 }
165
166 // again? see flower-proto.hh
167 typedef Interval_t<Real> Interval;
168 typedef Interval_t<int> Slice;  // weird name
169
170
171 #endif // INTERVAL_HH
172