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