]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/interval.tcc
release: 0.1.11
[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 {
67   if (right < left) 
68     return 0;
69   else 
70     return right-left;
71 }
72
73 template<class T>
74 void
75 Interval_t<T>::unite (Interval_t<T> h)
76 {
77   if (h.left<left)
78         left = h.left;
79   if (h.right>right)
80         right = h.right;
81 }
82
83 /**
84   smallest Interval which includes *this and #h#
85  */
86
87 template<class T>
88 void
89 Interval_t<T>::intersect (Interval_t<T> h)
90 {
91 #if defined (__GNUG__) && ! defined (__STRICT_ANSI__)
92   left = h.left >? left;
93   right = h.right <?right;
94 #else
95   left = max (h.left, left);
96   right = min (h.right, right);
97 #endif
98 }
99
100 template<class T>
101 Interval_t<T>
102 intersect (Interval_t<T> x, Interval_t<T> const &y)
103 {
104   x.intersect (y);
105   return x;
106 }
107
108 template<class T>
109 String
110 Interval_t<T>::str() const
111 {
112   if (empty_b())
113         return "[empty]";
114   String s ("[");
115  
116   return s + String (left) + String ("," ) + String (right ) + String ("]" );
117 }
118
119 template<class T>
120 bool
121 Interval_t<T>::elt_b (T r)
122 {
123   return r >= left && r <= right;
124 }
125
126
127 #define INTERVAL__INSTANTIATE(T) struct Interval_t<T>;\
128   template  int Interval__compare(const Interval_t<T>&,Interval_t<T> const&)
129
130 #endif // INTERVAL_TCC