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