]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/interval.tcc
* lily/tie-column.cc (set_manual_tie_configuration): new function.
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #ifndef INTERVAL_TCC
10 #define INTERVAL_TCC
11
12 #include <cassert>
13 #include <cmath>
14 using namespace std;
15
16 #include "interval.hh"
17 #include "string.hh"
18
19 template<class T>
20 int
21 _Interval__compare (const Interval_t<T> &a, Interval_t<T> const &b)
22 {
23   if (a.elem (LEFT) == b.elem (LEFT) && a.elem (RIGHT) == b.elem (RIGHT))
24     return 0;
25
26   if (a.elem (LEFT) <= b.elem (LEFT) && a.elem (RIGHT) >= b.elem (RIGHT))
27     return 1;
28
29   if (a.elem (LEFT) >= b.elem (LEFT) && a.elem (RIGHT) <= b.elem (RIGHT))
30     return -1;
31
32   return -2;
33 }
34
35 template<class T>
36 bool
37 Interval_t<T>::superset (Interval_t<T> const &a) const
38 {
39   int c_i = _Interval__compare (*this, a);
40   if (c_i == -2)
41     return false;
42   return c_i >= 0;
43 }
44
45 template<class T>
46 int
47 Interval__compare (Interval_t<T> const &a, Interval_t<T> const &b)
48 {
49   int i = _Interval__compare (a, b);
50   if (i < -1)
51     assert (false);
52   return i;
53 }
54
55 template<class T>
56 void
57 Interval_t<T>::set_empty ()
58 {
59   elem_ref (LEFT) = (T) infinity ();
60   elem_ref (RIGHT) = (T) -infinity ();
61 }
62
63 template<class T>
64 void
65 Interval_t<T>::set_full ()
66 {
67   elem_ref (LEFT) = (T) -infinity ();
68   elem_ref (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 template<class T>
82 T
83 Interval_t<T>::delta () const
84 {
85   return elem (RIGHT) - elem (LEFT);
86 }
87
88 /* smallest Interval which includes *this and #h#  */
89 template<class T>
90 void
91 Interval_t<T>::unite (Interval_t<T> h)
92 {
93   elem_ref (LEFT) = min (h.elem (LEFT), elem (LEFT));
94   elem_ref (RIGHT) = max (h.elem (RIGHT), elem (RIGHT));
95 }
96
97 template<class T>
98 void
99 Interval_t<T>::intersect (Interval_t<T> h)
100 {
101   elem_ref (LEFT) = max (h.elem (LEFT), elem (LEFT));
102   elem_ref (RIGHT) = min (h.elem (RIGHT), elem (RIGHT));
103 }
104
105 template<class T>
106 String
107 Interval_t<T>::to_string () const
108 {
109   if (is_empty ())
110     return "[empty]";
111   String s ("[");
112
113   return (s + T_to_string (elem (LEFT)) + String (",")
114           + T_to_string (elem (RIGHT)) + String ("]"));
115 }
116
117 template<class T>
118 bool
119 Interval_t<T>::contains (T r) const
120 {
121   return r >= elem (LEFT) && r <= elem (RIGHT);
122 }
123
124 #define INTERVAL__INSTANTIATE(T) struct Interval_t<T>;                  \
125   template int Interval__compare (const Interval_t<T> &, Interval_t<T> const &)
126
127 #endif // INTERVAL_TCC