]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/interval.hh
release: 0.0.46.jcn1
[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
14
15 /**  a T interval. 
16   this represents the closed interval [left,right].
17   No invariants. T must be a totally ordered ring
18   */
19 template<class T>
20 struct Interval_t {
21     T left, right;
22
23     /* ************** */
24     
25     T center() { return (left + right) / T(2);}
26     void translate(T t) {
27         left += t;
28         right += t;
29     }
30     T& idx(int j) {
31         if (j==-1)
32             return left;
33         else if (j==1)
34             return right;
35         else
36             assert(false);
37         return left;            
38     }
39     T& operator[](int j) {
40         return idx(j);
41     }
42     T operator[](int j) const {
43         return ((Interval_t<T> *)this)->idx(j);
44     }
45     T &max() { return right;}
46     T max()const { return right;}
47     T min()const{ return left; }
48     T &min(){ return left; }
49     /**
50       PRE
51       *this and h are comparable
52       */
53     void unite(Interval_t<T> h);
54     void intersect(Interval_t<T> h);
55
56     T length() const;
57     void set_empty() ;
58     bool empty_b() const { return left > right; }
59     Interval_t() {
60         set_empty();
61     }
62     Interval_t(T m, T M) {
63         left =m;
64         right = M;
65     }
66     Interval_t<T> &operator += (T r) {
67         left += r;
68         right +=r;
69         return *this;
70     }
71     String str() const;    
72     bool elt_b(T r);
73 };
74
75
76 /**
77   inclusion ordering. Crash if not comparable.
78   */
79 template<class T>
80 int Interval__compare(const Interval_t<T>&,Interval_t<T> const&);
81
82 /*
83   INLINE
84  */
85
86 #include "compare.hh"
87
88 template_instantiate_compare(Interval_t<T>&, Interval__compare, template<class T>);
89
90
91 template<class T>
92 inline Interval_t<T>
93 intersection(Interval_t<T> a, Interval_t<T> const&b)
94 {
95     a.intersect(b);
96     return a;
97     
98 }
99
100
101 template<class T>
102 inline
103 Interval_t<T> operator +(T a,Interval_t<T> i )
104 {
105     i += a;
106     return i;
107 }
108
109 template<class T>
110 inline
111 Interval_t<T> operator +(Interval_t<T> i,T a ){
112     return a+i;
113 }
114
115 typedef Interval_t<Real> Interval;
116
117
118 #define Interval__instantiate(T) template struct Interval_t<T>;\
119   template  int Interval__compare(const Interval_t<T>&,Interval_t<T> const&)
120
121
122 #endif // INTERVAL_HH
123
124
125