]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/interval.tcc
* lily/ledger-line-engraver.cc: new file.
[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
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 (const Interval_t<T>&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 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
119 template<class T>
120 String
121 Interval_t<T>::to_string () const
122 {
123   if (is_empty ())
124     return "[empty]";
125   String s ("[");
126  
127   return (s + T_to_string (elem (LEFT)) + String ("," )
128           + T_to_string (elem (RIGHT) ) + String ("]" ));
129 }
130
131 template<class T>
132 bool
133 Interval_t<T>::contains (T r)
134 {
135   return r >= elem (LEFT) && r <= elem (RIGHT);
136 }
137
138 #define INTERVAL__INSTANTIATE(T) struct Interval_t<T>;\
139 template int Interval__compare (const Interval_t<T>&,Interval_t<T> const&)
140
141 #endif // INTERVAL_TCC