*** empty log message ***
[lilypond.git] / flower / include / interval.hh
blobcbee57fbc9e2131fd1378580c7c08a78aa4a45b4
1 /*
2 interval.hh -- part of flowerlib
4 (c) 1996 Han-Wen Nienhuys
5 */
7 #ifndef INTERVAL_HH
8 #define INTERVAL_HH
10 #include <assert.h>
11 #include "flower-proto.hh"
12 #include "real.hh"
13 #include "drul-array.hh"
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.
20 template<class T>
21 struct Interval_t : public Drul_array<T> {
23 /* ************** */
25 static T infinity () ;
26 static String T_to_string (T arg);
27 T center () const {
28 assert (!empty_b ());
29 return (elem (LEFT) + elem (RIGHT)) / T (2);
31 void translate (T t)
33 elem (LEFT) += t;
34 elem (RIGHT) += t;
36 void widen (T t)
38 elem (LEFT) -= t;
39 elem (RIGHT) += t;
42 /**
43 PRE
44 *this and h are comparable
46 void unite (Interval_t<T> h);
47 void intersect (Interval_t<T> h);
48 void add_point (T p) {
49 elem(LEFT) = elem (LEFT) <? p;
50 elem(RIGHT) = elem (RIGHT) >? p;
52 T length () const;
53 T delta () const;
54 void set_empty () ;
55 void set_full ();
56 bool empty_b () const { return elem (LEFT) > elem (RIGHT); }
57 bool contains_b (Interval_t<T> const&) const;
58 Interval_t () {
59 set_empty ();
61 Interval_t (T m, T M) : Drul_array<T> (m,M)
64 Interval_t<T> &operator -= (T r) {
65 *this += -r;
66 return *this;
69 Interval_t<T> &operator += (T r) {
70 elem (LEFT) += r;
71 elem (RIGHT) +=r;
72 return *this;
74 Interval_t<T> &operator *= (T r) {
75 if (!empty_b ())
77 elem (LEFT) *= r;
78 elem (RIGHT) *= r;
79 if (r < T (0))
80 swap();
83 return *this;
86 Real linear_combination (Real x) const {
87 return ((1.0 - x) * Real (elem (LEFT)) + (x + 1.0) * Real (elem (RIGHT))) * 0.5;
89 String to_string () const;
91 bool elem_b (T r);
92 void negate () {
93 T r = -elem (LEFT);
94 T l = -elem (RIGHT);
95 elem (LEFT) = l;
96 elem (RIGHT) =r;
99 void swap ()
101 T t = elem (LEFT);
102 elem (LEFT) = elem (RIGHT);
103 elem (RIGHT) = t;
109 inclusion ordering. Crash if not comparable.
111 template<class T>
112 int Interval__compare (const Interval_t<T>&,Interval_t<T> const&);
115 Inclusion ordering. return -2 if not comparable
117 template<class T>
119 _Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b);
123 INLINE
126 #include "compare.hh"
128 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T>&, Interval__compare, template<class T>);
131 template<class T>
132 inline Interval_t<T>
133 intersection (Interval_t<T> a, Interval_t<T> const&b)
135 a.intersect (b);
136 return a;
140 template<class T>
141 inline
142 Interval_t<T> operator + (T a,Interval_t<T> i)
144 i += a;
145 return i;
148 template<class T>
149 inline
150 Interval_t<T> operator - (Interval_t<T> i, T a)
152 i += -a;
153 return i;
156 template<class T>
157 inline
158 Interval_t<T> operator - (T a,Interval_t<T> i)
160 i.negate ();
161 i += a;
162 return i;
165 template<class T>
166 inline
167 Interval_t<T> operator + (Interval_t<T> i,T a){
168 return a+i;
171 template<class T>
172 inline
173 Interval_t<T> operator * (T a,Interval_t<T> i)
175 i *= a;
176 return i;
179 template<class T>
180 inline
181 Interval_t<T> operator * (Interval_t<T> i,T a){
182 return a*i;
185 // again? see flower-proto.hh
186 typedef Interval_t<Real> Interval;
187 typedef Interval_t<int> Slice; // weird name
190 #endif // INTERVAL_HH