]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/interval.tcc
release: 0.1.8
[lilypond.git] / flower / include / interval.tcc
1 /*
2   interval.tcc -- implement Interval_t
3
4   source file of the Flower Library
5
6   (c) 1996,1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9
10 #ifndef INTERVAL_TCC
11 #define INTERVAL_TCC
12
13 #include <assert.h> 
14 #include <math.h>
15 #include "interval.hh"
16 #include "string.hh"
17
18
19 template<class T>
20 int
21 _Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b)
22 {
23     if (a.left == b.left && a.right == b.right)
24         return 0;
25     
26     if (a.left <= b.left && a.right >= b.right)
27         return 1;
28
29     if (a.left >= b.left && a.right <= b.right)
30         return -1;
31
32     return -2;
33 }
34
35 template<class T>
36 bool 
37 Interval_t<T>::contains_b (Interval_t<T> const& a)const
38 {
39     int c_i= _Interval__compare (*this, a);
40     if (c_i == -2)
41         return false;
42     return c_i >= 0;
43 }
44
45 template<class T>
46 int
47 Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b)
48 {
49     int i = _Interval__compare (a,b);
50     if (i < -1)
51         assert (false);
52     return i;
53 }
54
55 template<class T>
56 void
57 Interval_t<T>::set_empty()
58 {
59     left = (T) infinity();
60     right = (T) -infinity();
61 }
62
63 template<class T>
64 T
65 Interval_t<T>::length() const {
66     assert (right >= left);
67     return right-left;
68 }
69
70 template<class T>
71 void
72 Interval_t<T>::unite (Interval_t<T> h)
73 {
74     if (h.left<left)
75         left = h.left;
76     if (h.right>right)
77         right = h.right;
78 }
79
80 /**
81   smallest Interval which includes *this and #h#
82  */
83
84 template<class T>
85 void
86 Interval_t<T>::intersect (Interval_t<T> h)
87 {
88 #if defined (__GNUG__) && ! defined (__STRICT_ANSI__)
89     left = h.left >? left;
90     right = h.right <?right;
91 #else
92     left = max (h.left, left);
93     right = min (h.right, right);
94 #endif
95 }
96
97 template<class T>
98 Interval_t<T>
99 intersect (Interval_t<T> x, Interval_t<T> const &y)
100 {
101     x.intersect (y);
102     return x;
103 }
104
105 template<class T>
106 String
107 Interval_t<T>::str() const
108 {
109     if (empty_b())
110         return "[empty]";
111     String s ("[");
112  
113     return s + String (left) + String ("," ) + String (right ) + String ("]" );
114 }
115
116 template<class T>
117 bool
118 Interval_t<T>::elt_b (T r)
119 {
120     return r >= left && r <= right;
121 }
122
123
124 #define INTERVAL__INSTANTIATE(T) struct Interval_t<T>;\
125   template  int Interval__compare(const Interval_t<T>&,Interval_t<T> const&)
126
127 #endif // INTERVAL_TCC