]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/interval.tcc
release: 1.1.32
[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--1999 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 #include "flower-debug.hh"
18
19 template<class T>
20 void
21 Interval_t<T>::print () const
22 {
23   fdebug << str ();
24 }
25
26 template<class T>
27 int
28 _Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b)
29 {
30   if (a.elem (LEFT) == b.elem (LEFT) && a.elem (RIGHT) == b.elem (RIGHT))
31     return 0;
32   
33   if (a.elem (LEFT) <= b.elem (LEFT) && a.elem (RIGHT) >= b.elem (RIGHT))
34     return 1;
35
36   if (a.elem (LEFT) >= b.elem (LEFT) && a.elem (RIGHT) <= b.elem (RIGHT))
37     return -1;
38
39   return -2;
40 }
41
42 template<class T>
43 bool 
44 Interval_t<T>::contains_b (Interval_t<T> const& a) const
45 {
46   int c_i= _Interval__compare (*this, a);
47   if (c_i == -2)
48     return false;
49   return c_i >= 0;
50 }
51
52 template<class T>
53 int
54 Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b)
55 {
56   int i = _Interval__compare (a,b);
57   if (i < -1)
58     assert (false);
59   return i;
60 }
61
62 template<class T>
63 void
64 Interval_t<T>::set_empty()
65 {
66   elem (LEFT) = (T) infinity();
67   elem (RIGHT) = (T) -infinity();
68 }
69
70 template<class T>
71 T
72 Interval_t<T>::length() const 
73 {
74   if (elem (RIGHT) < elem (LEFT)) 
75     return 0;
76   else 
77     return elem (RIGHT)-elem (LEFT);
78 }
79
80 /**
81   smallest Interval which includes *this and #h#
82  */
83 template<class T>
84 void
85 Interval_t<T>::unite (Interval_t<T> h)
86 {
87   elem (LEFT) = h.elem (LEFT) <? elem (LEFT);
88   elem (RIGHT) = h.elem (RIGHT) >?elem (RIGHT);
89
90 #if 0
91   if (h.elem (LEFT)<elem (LEFT))
92     elem (LEFT) = h.elem (LEFT);
93   if (h.elem (RIGHT)>elem (RIGHT))
94   elem (RIGHT) = h.elem (RIGHT);
95 #endif
96 }
97
98
99 template<class T>
100 void
101 Interval_t<T>::intersect (Interval_t<T> h)
102 {
103 #if defined (__GNUG__) && ! defined (__STRICT_ANSI__)
104   elem (LEFT) = h.elem (LEFT) >? elem (LEFT);
105   elem (RIGHT) = h.elem (RIGHT) <?elem (RIGHT);
106 #else
107   elem (LEFT) = max (h.elem (LEFT), elem (LEFT));
108   elem (RIGHT) = min (h.elem (RIGHT), elem (RIGHT));
109 #endif
110 }
111
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
120 template<class T>
121 String
122 Interval_t<T>::str() const
123 {
124   if (empty_b())
125     return "[empty]";
126   String s ("[");
127  
128   return s + T_to_str (elem (LEFT)) + String ("," ) + T_to_str (elem (RIGHT) ) + String ("]" );
129 }
130
131 template<class T>
132 bool
133 Interval_t<T>::elem_b (T r)
134 {
135   return r >= elem (LEFT) && r <= elem (RIGHT);
136 }
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