]> git.donarmstrong.com Git - lilypond.git/blob - flower/interval.cc
release: 0.0.16
[lilypond.git] / flower / interval.cc
1 #include <assert.h> 
2 #include <math.h>
3 #include "interval.hh"
4 #include "string.hh"
5
6
7 const Real INFTY = HUGE;
8
9 void
10 Interval::set_empty() {
11     min = INFTY;
12     max = -INFTY;
13 }
14
15 Real
16 Interval::length() const {
17     assert(max >= min);
18     return max-min;
19 }
20
21 void
22 Interval::unite(Interval h)
23 {
24     compare(h, *this );
25     if (h.min<min)
26         min = h.min;
27     if (h.max>max)
28         max = h.max;
29 }
30
31 void
32 Interval::intersect(Interval h)
33 {
34     min = MAX(h.min, min);
35     max = MIN(h.max, max);
36 }
37
38 Interval
39 intersection(Interval a, Interval const&b)
40 {
41     a.intersect(b);
42     return a;
43     
44 }
45
46 int
47 Interval::compare(const Interval&a,Interval const&b)
48 {
49     if (a.min == b.min && a.max == b.max)
50         return 0;
51     
52     if (a.min <= b.min && a.max >= b.max)
53         return 1;
54
55     if (a.min >= b.min && a.max <= b.max)
56         return -1;
57
58     assert(false);              // not comparable
59
60     return 0;
61 }
62
63 Interval
64 intersect(Interval x, Interval const &y)
65 {
66     x.intersect(y);
67     return x;
68 }
69     
70 String
71 Interval::str() const
72 {
73     if (empty())
74         return "[empty]";
75     String s("[");
76  
77     return s + min + "," + max +"]";
78 }
79 bool
80 Interval::elt_q(Real r)
81 {
82     return r >= min && r <= max;
83 }