lilypond-0.1.12
[lilypond.git] / flower / include / interval.hh
blob2fd76090a327ec3187352016d1c7fc94718ed6dd
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 "fproto.hh"
12 #include "real.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 {
22 T left, right;
24 /* ************** */
26 static T infinity() ;
28 T center() { return (left + right) / T(2);}
29 void translate (T t) {
30 left += t;
31 right += t;
33 T& idx (int j) {
34 if (j==-1)
35 return left;
36 else if (j==1)
37 return right;
38 else
39 assert (false);
40 return left;
42 T& operator[](int j) {
43 return idx (j);
45 T operator[](int j) const {
46 return ((Interval_t<T> *)this)->idx (j);
48 T &max() { return right;}
49 T max() const { return right;}
50 T min() const{ return left; }
51 T &min(){ return left; }
52 /**
53 PRE
54 *this and h are comparable
56 void unite (Interval_t<T> h);
57 void intersect (Interval_t<T> h);
59 T length() const;
60 void set_empty() ;
61 bool empty_b() const { return left > right; }
62 bool contains_b (Interval_t<T> const&) const;
63 Interval_t() {
64 set_empty();
66 Interval_t (T m, T M) {
67 left =m;
68 right = M;
70 Interval_t<T> &operator += (T r) {
71 left += r;
72 right +=r;
73 return *this;
75 Interval_t<T> &operator *=(T r) {
76 left *= r;
77 right *= r;
78 if (r < T(0)) {
79 T t = left;
80 left = right;
81 right = t;
83 return *this;
85 String str() const;
86 bool elt_b (T r);
90 /**
91 inclusion ordering. Crash if not comparable.
93 template<class T>
94 int Interval__compare (const Interval_t<T>&,Interval_t<T> const&);
97 INLINE
100 #include "compare.hh"
102 TEMPLATE_INSTANTIATE_COMPARE(Interval_t<T>&, Interval__compare, template<class T>);
105 template<class T>
106 inline Interval_t<T>
107 intersection (Interval_t<T> a, Interval_t<T> const&b)
109 a.intersect (b);
110 return a;
114 template<class T>
115 inline
116 Interval_t<T> operator +(T a,Interval_t<T> i)
118 i += a;
119 return i;
122 template<class T>
123 inline
124 Interval_t<T> operator +(Interval_t<T> i,T a){
125 return a+i;
128 template<class T>
129 inline
130 Interval_t<T> operator *(T a,Interval_t<T> i)
132 i *= a;
133 return i;
136 template<class T>
137 inline
138 Interval_t<T> operator *(Interval_t<T> i,T a){
139 return a*i;
142 typedef Interval_t<Real> Interval;
147 #endif // INTERVAL_HH