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