]> git.donarmstrong.com Git - lilypond.git/blob - flower/interval.cc
46b4fc7e1603f2bb7346f5af0d6ec3e857eb5404
[lilypond.git] / flower / interval.cc
1 #include <assert.h> 
2 #include <math.h>
3 #include "interval.hh"
4 #include "string.hh"
5
6
7
8 template<class T>
9 int
10 Interval__compare(const Interval_t<T>&a,Interval_t<T> const&b)
11 {
12     if (a.left == b.left && a.right == b.right)
13         return 0;
14     
15     if (a.left <= b.left && a.right >= b.right)
16         return 1;
17
18     if (a.left >= b.left && a.right <= b.right)
19         return -1;
20
21     assert(false);              // not comparable
22
23     return 0;
24 }
25
26 const Real INFTY = HUGE;
27
28 template<class T>
29 void
30 Interval_t<T>::set_empty() {
31     left = INFTY;
32     right = -INFTY;
33 }
34
35 template<class T>
36 T
37 Interval_t<T>::length() const {
38     assert(right >= left);
39     return right-left;
40 }
41
42 template<class T>
43 void
44 Interval_t<T>::unite(Interval_t<T> h)
45 {
46     if (h.left<left)
47         left = h.left;
48     if (h.right>right)
49         right = h.right;
50 }
51
52 /**
53   smallest Interval which includes *this and #h#
54  */
55
56 template<class T>
57 void
58 Interval_t<T>::intersect(Interval_t<T> h)
59 {
60 #if defined (__GNUG__) && ! defined (__STRICT_ANSI__)
61     left = h.left >? left;
62     right = h.right <?right;
63 #else
64     left = max(h.left, left);
65     right = min(h.right, right);
66 #endif
67 }
68
69 template<class T>
70 Interval_t<T>
71 intersect(Interval_t<T> x, Interval_t<T> const &y)
72 {
73     x.intersect(y);
74     return x;
75 }
76
77 template<class T>
78 String
79 Interval_t<T>::str() const
80 {
81     if (empty())
82         return "[empty]";
83     String s("[");
84  
85     return s + left + "," + right +"]";
86 }
87
88 template<class T>
89 bool
90 Interval_t<T>::elt_q(T r)
91 {
92     return r >= left && r <= right;
93 }