flower-1.0.23
[lilypond.git] / flower / interval.tcc
blob6e7b0b1279c228982edacbf11eee9775bc07dce6
1 #include <assert.h> 
2 #include <math.h>
3 #include "interval.hh"
4 #include "string.hh"
7 template<class T>
8 int
9 _Interval__compare(const Interval_t<T>&a,Interval_t<T> const&b)
11     if (a.left == b.left && a.right == b.right)
12         return 0;
13     
14     if (a.left <= b.left && a.right >= b.right)
15         return 1;
17     if (a.left >= b.left && a.right <= b.right)
18         return -1;
20     return -2;
24 template<class T>
25 int
26 Interval__compare(const Interval_t<T>&a,Interval_t<T> const&b)
28     int i = _Interval__compare(a,b);
29     if (i < -1)
30         assert(false);
31     return i;
34 #ifdef AIX
35 const Real INFTY = 1e8; // ARGh. AIX sucks
36 #else
37 const Real INFTY = HUGE_VAL;
38 #endif
40 template<class T>
41 void
42 Interval_t<T>::set_empty()
44     left = INFTY;
45     right = -INFTY;
48 template<class T>
50 Interval_t<T>::length() const {
51     assert(right >= left);
52     return right-left;
55 template<class T>
56 void
57 Interval_t<T>::unite(Interval_t<T> h)
59     if (h.left<left)
60         left = h.left;
61     if (h.right>right)
62         right = h.right;
65 /**
66   smallest Interval which includes *this and #h#
67  */
69 template<class T>
70 void
71 Interval_t<T>::intersect(Interval_t<T> h)
73 #if defined (__GNUG__) && ! defined (__STRICT_ANSI__)
74     left = h.left >? left;
75     right = h.right <?right;
76 #else
77     left = max(h.left, left);
78     right = min(h.right, right);
79 #endif
82 template<class T>
83 Interval_t<T>
84 intersect(Interval_t<T> x, Interval_t<T> const &y)
86     x.intersect(y);
87     return x;
90 template<class T>
91 String
92 Interval_t<T>::str() const
94     if (empty())
95         return "[empty]";
96     String s("[");
98     return s + left + "," + right +"]";
101 template<class T>
102 bool
103 Interval_t<T>::elt_q(T r)
105     return r >= left && r <= right;