]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/interval.tcc
* configure.in: Test for and accept lmodern if EC fonts not found.
[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 <cassert> 
14 #include <cmath>
15
16 #include "interval.hh"
17 #include "string.hh"
18
19
20 template<class T>
21 int
22 _Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b)
23 {
24   if (a.elem (LEFT) == b.elem (LEFT) && a.elem (RIGHT) == b.elem (RIGHT))
25     return 0;
26   
27   if (a.elem (LEFT) <= b.elem (LEFT) && a.elem (RIGHT) >= b.elem (RIGHT))
28     return 1;
29
30   if (a.elem (LEFT) >= b.elem (LEFT) && a.elem (RIGHT) <= b.elem (RIGHT))
31     return -1;
32
33   return -2;
34 }
35
36 template<class T>
37 bool 
38 Interval_t<T>::superset (Interval_t<T> const& a) const
39 {
40   int c_i = _Interval__compare (*this, a);
41   if (c_i == -2)
42     return false;
43   return c_i >= 0;
44 }
45
46 template<class T>
47 int
48 Interval__compare (Interval_t<T> const& a,Interval_t<T> const& b)
49 {
50   int i = _Interval__compare (a,b);
51   if (i < -1)
52     assert (false);
53   return i;
54 }
55
56 template<class T>
57 void
58 Interval_t<T>::set_empty ()
59 {
60   elem_ref (LEFT) = (T) infinity ();
61   elem_ref (RIGHT) = (T) -infinity ();
62 }
63
64 template<class T>
65 void
66 Interval_t<T>::set_full ()
67 {
68   elem_ref (LEFT) = (T) -infinity ();
69   elem_ref (RIGHT) = (T) infinity ();
70 }
71
72 template<class T>
73 T
74 Interval_t<T>::length () const 
75 {
76   if (elem (RIGHT) <= elem (LEFT)) 
77     return 0;
78   else 
79     return elem (RIGHT) - elem (LEFT);
80 }
81
82 template<class T>
83 T
84 Interval_t<T>::delta () const 
85 {
86   return elem (RIGHT) - elem (LEFT);
87 }
88
89 /* smallest Interval which includes *this and #h#  */
90 template<class T>
91 void
92 Interval_t<T>::unite (Interval_t<T> h)
93 {
94   elem_ref (LEFT) = h.elem (LEFT) <? elem (LEFT);
95   elem_ref (RIGHT) = h.elem (RIGHT) >? elem (RIGHT);
96 }
97
98 template<class T>
99 void
100 Interval_t<T>::intersect (Interval_t<T> h)
101 {
102 #if defined (__GNUG__) && !defined (__STRICT_ANSI__)
103   elem_ref (LEFT) = h.elem (LEFT) >? elem (LEFT);
104   elem_ref (RIGHT) = h.elem (RIGHT) <? elem (RIGHT);
105 #else
106   elem_ref (LEFT) = max (h.elem (LEFT), elem (LEFT));
107   elem_ref (RIGHT) = min (h.elem (RIGHT), elem (RIGHT));
108 #endif
109 }
110
111 #if 0 //this is called intersection
112 template<class T>
113 Interval_t<T>
114 intersect (Interval_t<T> x, Interval_t<T> const &y)
115 {
116   x.intersect (y);
117   return x;
118 }
119 #endif
120
121 template<class T>
122 String
123 Interval_t<T>::to_string () const
124 {
125   if (is_empty ())
126     return "[empty]";
127   String s ("[");
128  
129   return (s + T_to_string (elem (LEFT)) + String ("," )
130           + T_to_string (elem (RIGHT) ) + String ("]" ));
131 }
132
133 template<class T>
134 bool
135 Interval_t<T>::contains (T r)
136 {
137   return r >= elem (LEFT) && r <= elem (RIGHT);
138 }
139
140 #define INTERVAL__INSTANTIATE(T) struct Interval_t<T>;\
141 template int Interval__compare (const Interval_t<T>&,Interval_t<T> const&)
142
143 #endif // INTERVAL_TCC