flower-1.1.5
[lilypond.git] / flower / interval.hh
blobaae16381dbef62ed5133b332d49714d8260b5070
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 this represents the closed interval [left,right].
17 No invariants. T must be a totally ordered ring
19 template<class T>
20 struct Interval_t {
21 T left, right;
23 /* ************** */
25 T center() { return (left + right) / T(2);}
26 void translate(T t) {
27 left += t;
28 right += t;
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;
39 T& operator[](int j) {
40 return idx(j);
42 T operator[](int j) const {
43 return ((Interval_t<T> *)this)->idx(j);
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
53 void unite(Interval_t<T> h);
54 void intersect(Interval_t<T> h);
56 T length() const;
57 void set_empty() ;
58 bool empty() const { return left > right; }
59 Interval_t() {
60 set_empty();
62 Interval_t(T m, T M) {
63 left =m;
64 right = M;
66 Interval_t<T> &operator += (T r) {
67 left += r;
68 right +=r;
69 return *this;
71 String str() const;
72 bool elt_q(T r);
76 /**
77 inclusion ordering. Crash if not comparable.
79 template<class T>
80 int Interval__compare(const Interval_t<T>&,Interval_t<T> const&);
83 INLINE
86 #include "compare.hh"
88 template_instantiate_compare(Interval_t<T>&, Interval__compare, template<class T>);
91 template<class T>
92 inline Interval_t<T>
93 intersection(Interval_t<T> a, Interval_t<T> const&b)
95 a.intersect(b);
96 return a;
101 template<class T>
102 inline
103 Interval_t<T> operator +(T a,Interval_t<T> i )
105 i += a;
106 return i;
109 template<class T>
110 inline
111 Interval_t<T> operator +(Interval_t<T> i,T a ){
112 return a+i;
115 typedef Interval_t<Real> Interval;
118 #define Interval__instantiate(T) template struct Interval_t<T>;\
119 template int Interval__compare(const Interval_t<T>&,Interval_t<T> const&)
122 #endif // INTERVAL_HH