]> git.donarmstrong.com Git - lilypond.git/blob - flower/interval.tcc
release: 0.0.40
[lilypond.git] / flower / interval.tcc
1 #include <assert.h> 
2 #include <math.h>
3 #include "interval.hh"
4 #include "string.hh"
5
6
7 template<class T>
8 int
9 _Interval__compare(const Interval_t<T>&a,Interval_t<T> const&b)
10 {
11     if (a.left == b.left && a.right == b.right)
12         return 0;
13     
14     if (a.left <= b.left && a.right >= b.right)
15         return 1;
16
17     if (a.left >= b.left && a.right <= b.right)
18         return -1;
19
20     return -2;
21 }
22
23
24 template<class T>
25 int
26 Interval__compare(const Interval_t<T>&a,Interval_t<T> const&b)
27 {
28     int i = _Interval__compare(a,b);
29     if (i < -1)
30         assert(false);
31     return i;
32 }
33
34 #ifdef AIX
35 const Real INFTY = 1e8; // ARGh. AIX sucks
36 #else
37 const Real INFTY = HUGE_VAL;
38 #endif
39
40 template<class T>
41 void
42 Interval_t<T>::set_empty()
43 {
44     left = INFTY;
45     right = -INFTY;
46 }
47
48 template<class T>
49 T
50 Interval_t<T>::length() const {
51     assert(right >= left);
52     return right-left;
53 }
54
55 template<class T>
56 void
57 Interval_t<T>::unite(Interval_t<T> h)
58 {
59     if (h.left<left)
60         left = h.left;
61     if (h.right>right)
62         right = h.right;
63 }
64
65 /**
66   smallest Interval which includes *this and #h#
67  */
68
69 template<class T>
70 void
71 Interval_t<T>::intersect(Interval_t<T> h)
72 {
73 #if defined (__GNUG__) && ! defined (__STRICT_ANSI__)
74     left = h.left >? left;
75     right = h.right <?right;
76 #else
77     left = max(h.left, left);
78     right = min(h.right, right);
79 #endif
80 }
81
82 template<class T>
83 Interval_t<T>
84 intersect(Interval_t<T> x, Interval_t<T> const &y)
85 {
86     x.intersect(y);
87     return x;
88 }
89
90 template<class T>
91 String
92 Interval_t<T>::str() const
93 {
94     if (empty())
95         return "[empty]";
96     String s("[");
97  
98     return s + left + "," + right +"]";
99 }
100
101 template<class T>
102 bool
103 Interval_t<T>::elt_q(T r)
104 {
105     return r >= left && r <= right;
106 }