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