lilypond-1.4.3
[lilypond.git] / flower / include / interval.hh
blob5a1d3c49af90579f95c01de586f5b757a96d9a19
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_str (T arg);
27 T center () {
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;
37 /**
38 PRE
39 *this and h are comparable
41 void unite (Interval_t<T> h);
42 void intersect (Interval_t<T> h);
44 T length () const;
45 void set_empty () ;
46 bool empty_b () const { return elem (LEFT) > elem (RIGHT); }
47 bool contains_b (Interval_t<T> const&) const;
48 Interval_t () {
49 set_empty ();
51 Interval_t (T m, T M) : Drul_array<T> (m,M)
53 Interval_t<T> &operator -= (T r) {
54 *this += -r;
55 return *this;
58 Interval_t<T> &operator += (T r) {
59 elem (LEFT) += r;
60 elem (RIGHT) +=r;
61 return *this;
63 Interval_t<T> &operator *= (T r) {
64 if (!empty_b ())
66 elem (LEFT) *= r;
67 elem (RIGHT) *= r;
68 if (r < T (0)) {
69 T t = elem (LEFT);
70 elem (LEFT) = elem (RIGHT);
71 elem (RIGHT) = t;
74 return *this;
77 Real linear_combination (Real x) const {
78 return ((1.0 - x) * Real (elem (LEFT)) + (x + 1.0) * Real (elem (RIGHT))) * 0.5;
80 String str () const;
82 bool elem_b (T r);
83 void negate () {
84 T r = -elem (LEFT);
85 T l = -elem (RIGHT);
86 elem (LEFT) = l;
87 elem (RIGHT) =r;
92 /**
93 inclusion ordering. Crash if not comparable.
95 template<class T>
96 int Interval__compare (const Interval_t<T>&,Interval_t<T> const&);
98 /**
99 Inclusion ordering. return -2 if not comparable
101 template<class T>
103 _Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b);
107 INLINE
110 #include "compare.hh"
112 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T>&, Interval__compare, template<class T>);
115 template<class T>
116 inline Interval_t<T>
117 intersection (Interval_t<T> a, Interval_t<T> const&b)
119 a.intersect (b);
120 return a;
124 template<class T>
125 inline
126 Interval_t<T> operator + (T a,Interval_t<T> i)
128 i += a;
129 return i;
132 template<class T>
133 inline
134 Interval_t<T> operator - (Interval_t<T> i, T a)
136 i += -a;
137 return i;
140 template<class T>
141 inline
142 Interval_t<T> operator - (T a,Interval_t<T> i)
144 i.negate ();
145 i += a;
146 return i;
149 template<class T>
150 inline
151 Interval_t<T> operator + (Interval_t<T> i,T a){
152 return a+i;
155 template<class T>
156 inline
157 Interval_t<T> operator * (T a,Interval_t<T> i)
159 i *= a;
160 return i;
163 template<class T>
164 inline
165 Interval_t<T> operator * (Interval_t<T> i,T a){
166 return a*i;
169 // again? see flower-proto.hh
170 typedef Interval_t<Real> Interval;
171 typedef Interval_t<int> Slice; // weird name
174 #endif // INTERVAL_HH