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