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