]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/interval.tcc
release: 0.0.52
[lilypond.git] / flower / include / 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 template<class T>
24 bool 
25 Interval_t<T>::contains_b(Interval_t<T> const& a)const
26 {
27     int c_i= _Interval__compare( *this, a);
28     if (c_i == -2)
29         return false;
30     return c_i >= 0;
31 }
32
33 template<class T>
34 int
35 Interval__compare(const Interval_t<T>&a,Interval_t<T> const&b)
36 {
37     int i = _Interval__compare(a,b);
38     if (i < -1)
39         assert(false);
40     return i;
41 }
42
43 template<class T>
44 void
45 Interval_t<T>::set_empty()
46 {
47     left = (T) infinity();
48     right = (T) -infinity();
49 }
50
51 template<class T>
52 T
53 Interval_t<T>::length() const {
54     assert(right >= left);
55     return right-left;
56 }
57
58 template<class T>
59 void
60 Interval_t<T>::unite(Interval_t<T> h)
61 {
62     if (h.left<left)
63         left = h.left;
64     if (h.right>right)
65         right = h.right;
66 }
67
68 /**
69   smallest Interval which includes *this and #h#
70  */
71
72 template<class T>
73 void
74 Interval_t<T>::intersect(Interval_t<T> h)
75 {
76 #if defined (__GNUG__) && ! defined (__STRICT_ANSI__)
77     left = h.left >? left;
78     right = h.right <?right;
79 #else
80     left = max(h.left, left);
81     right = min(h.right, right);
82 #endif
83 }
84
85 template<class T>
86 Interval_t<T>
87 intersect(Interval_t<T> x, Interval_t<T> const &y)
88 {
89     x.intersect(y);
90     return x;
91 }
92
93 template<class T>
94 String
95 Interval_t<T>::str() const
96 {
97     if (empty_b())
98         return "[empty]";
99     String s("[");
100  
101     return s + left + "," + right +"]";
102 }
103
104 template<class T>
105 bool
106 Interval_t<T>::elt_b(T r)
107 {
108     return r >= left && r <= right;
109 }