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