]> git.donarmstrong.com Git - lilypond.git/blob - flower/interval.hh
5331c1d12534f78da11277ba627743905f366f20
[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 Real interval
16 struct Interval {
17     Real min, max;
18
19     /****************/
20     
21     Real center() { return (min + max) /2;}
22     void translate(Real t) {
23         min += t;
24         max += t;
25     }
26     Real operator[](int j) {
27         if (j==-1)
28             return min;
29         else if (j==1)
30             return max;
31         else
32             assert(false);
33             return 0.0;
34                 
35     }
36     void unite(Interval h) ;
37     /**
38       PRE
39       *this and h are comparable
40       */
41     void intersect(Interval h);
42
43     Real length() const;
44     void set_empty() ;
45     bool empty() const { return min > max; }
46     Interval() {
47         set_empty();
48     }
49     Interval(Real m, Real M) {
50         min =m;
51         max = M;
52     }
53     Interval &operator += (Real r) {
54         min += r;
55         max +=r;
56         return *this;
57     }
58     bool elt_q(Real r);
59     String str() const;
60
61     /// partial ordering
62     static compare(const Interval&,Interval const&);
63     /**
64       inclusion ordering. Crash if not comparable.
65       */
66 };
67 /**
68   this represents the closed interval [min,max].
69   No invariants
70   */
71
72 Interval intersection(Interval, Interval const&);
73
74 #include "compare.hh"
75
76 instantiate_compare(Interval&, Interval::compare);
77
78
79 inline
80 Interval operator +(double a,Interval i )
81 {
82     i += a;
83     return i;
84 }
85
86 inline
87 Interval operator +(Interval i,double a ){
88     return a+i;
89 }
90
91 #endif // INTERVAL_HH
92
93
94