]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/interval.tcc
(spanned_rank_iv): Bugfix.
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.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.elem (LEFT) == b.elem (LEFT) && a.elem (RIGHT) == b.elem (RIGHT))
24     return 0;
25   
26   if (a.elem (LEFT) <= b.elem (LEFT) && a.elem (RIGHT) >= b.elem (RIGHT))
27     return 1;
28
29   if (a.elem (LEFT) >= b.elem (LEFT) && a.elem (RIGHT) <= b.elem (RIGHT))
30     return -1;
31
32   return -2;
33 }
34
35 template<class T>
36 bool 
37 Interval_t<T>::superset (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 (Interval_t<T> const& 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   elem_ref (LEFT) = (T) infinity ();
60   elem_ref (RIGHT) = (T) -infinity ();
61 }
62
63 template<class T>
64 void
65 Interval_t<T>::set_full ()
66 {
67   elem_ref (LEFT) = (T) -infinity ();
68   elem_ref (RIGHT) = (T) infinity ();
69 }
70
71 template<class T>
72 T
73 Interval_t<T>::length () const 
74 {
75   if (elem (RIGHT) <= elem (LEFT)) 
76     return 0;
77   else 
78     return elem (RIGHT) - elem (LEFT);
79 }
80
81 template<class T>
82 T
83 Interval_t<T>::delta () const 
84 {
85   return elem (RIGHT) - elem (LEFT);
86 }
87
88 /* smallest Interval which includes *this and #h#  */
89 template<class T>
90 void
91 Interval_t<T>::unite (Interval_t<T> h)
92 {
93   elem_ref (LEFT) = h.elem (LEFT) <? elem (LEFT);
94   elem_ref (RIGHT) = h.elem (RIGHT) >? elem (RIGHT);
95 }
96
97 template<class T>
98 void
99 Interval_t<T>::intersect (Interval_t<T> h)
100 {
101 #if defined (__GNUG__) && !defined (__STRICT_ANSI__)
102   elem_ref (LEFT) = h.elem (LEFT) >? elem (LEFT);
103   elem_ref (RIGHT) = h.elem (RIGHT) <? elem (RIGHT);
104 #else
105   elem_ref (LEFT) = max (h.elem (LEFT), elem (LEFT));
106   elem_ref (RIGHT) = min (h.elem (RIGHT), elem (RIGHT));
107 #endif
108 }
109
110 #if 0 //this is called intersection
111 template<class T>
112 Interval_t<T>
113 intersect (Interval_t<T> x, Interval_t<T> const &y)
114 {
115   x.intersect (y);
116   return x;
117 }
118 #endif
119
120 template<class T>
121 String
122 Interval_t<T>::to_string () const
123 {
124   if (is_empty ())
125     return "[empty]";
126   String s ("[");
127  
128   return (s + T_to_string (elem (LEFT)) + String ("," )
129           + T_to_string (elem (RIGHT) ) + String ("]" ));
130 }
131
132 template<class T>
133 bool
134 Interval_t<T>::contains (T r)
135 {
136   return r >= elem (LEFT) && r <= elem (RIGHT);
137 }
138
139 #define INTERVAL__INSTANTIATE(T) struct Interval_t<T>;\
140 template int Interval__compare (const Interval_t<T>&,Interval_t<T> const&)
141
142 #endif // INTERVAL_TCC