lilypond-0.0.27
[lilypond.git] / flower / interval.hh
blobacf437ad0ac98b7ff4da2b37e87c5b1099641e75
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
16 template<class T>
17 struct Interval_t {
18 T left, right;
20 /****************/
22 T center() { return (left + right) / T(2);}
23 void translate(T t) {
24 left += t;
25 right += t;
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;
36 T& operator[](int j) {
37 return idx(j);
39 T operator[](int j) const {
40 return ((Interval_t<T> *)this)->idx(j);
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
51 void intersect(Interval_t<T> h);
53 T length() const;
54 void set_empty() ;
55 bool empty() const { return left > right; }
56 Interval_t() {
57 set_empty();
59 Interval_t(T m, T M) {
60 left =m;
61 right = M;
63 Interval_t<T> &operator += (T r) {
64 left += r;
65 right +=r;
66 return *this;
68 String str() const;
69 bool elt_q(T r);
71 /**
72 this represents the closed interval [left,right].
73 No invariants. T must be a totally ordered ring
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.
83 /****************************************************************
84 INLINE
85 ****************************************************************/
87 #include "compare.hh"
89 template_instantiate_compare(Interval_t<T>&, Interval__compare, template<class T>);
92 template<class T>
93 inline Interval_t<T>
94 intersection(Interval_t<T> a, Interval_t<T> const&b)
96 a.intersect(b);
97 return a;
102 template<class T>
103 inline
104 Interval_t<T> operator +(T a,Interval_t<T> i )
106 i += a;
107 return i;
110 template<class T>
111 inline
112 Interval_t<T> operator +(Interval_t<T> i,T a ){
113 return a+i;
116 typedef Interval_t<Real> Interval;
119 #define Interval__instantiate(T) template struct Interval_t<T>;\
120 template int Interval__compare(const Interval_t<T>&,Interval_t<T> const&)
123 #endif // INTERVAL_HH