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