Doc -- compile fix
[lilypond/mpolesky.git] / flower / include / interval.tcc
blob3920d7628152df61cb3692aaa738b6ac9af1dc3d
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
4   Copyright (C) 1996--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
20 #ifndef INTERVAL_TCC
21 #define INTERVAL_TCC
23 #include <cassert>
25 #include "interval.hh"
26 #include "std-string.hh"
28 // MacOS 10.3 problems:
29 // #include <cmath>
30 using namespace std;
32 template<class T>
33 int
34 _Interval__compare (const Interval_t<T> &a, Interval_t<T> const &b)
36   if (a.at (LEFT) == b.at (LEFT) && a.at (RIGHT) == b.at (RIGHT))
37     return 0;
39   if (a.at (LEFT) <= b.at (LEFT) && a.at (RIGHT) >= b.at (RIGHT))
40     return 1;
42   if (a.at (LEFT) >= b.at (LEFT) && a.at (RIGHT) <= b.at (RIGHT))
43     return -1;
45   return -2;
48 template<class T>
49 bool
50 Interval_t<T>::superset (Interval_t<T> const &a) const
52   int c_i = _Interval__compare (*this, a);
53   if (c_i == -2)
54     return false;
55   return c_i >= 0;
58 template<class T>
59 int
60 Interval__compare (Interval_t<T> const &a, Interval_t<T> const &b)
62   int i = _Interval__compare (a, b);
63   if (i < -1)
64     assert (false);
65   return i;
68 template<class T>
69 void
70 Interval_t<T>::set_empty ()
72   at (LEFT) = (T) infinity ();
73   at (RIGHT) = (T) -infinity ();
76 template<class T>
77 void
78 Interval_t<T>::set_full ()
80   at (LEFT) = (T) -infinity ();
81   at (RIGHT) = (T) infinity ();
84 template<class T>
86 Interval_t<T>::length () const
88   if (at (RIGHT) <= at (LEFT))
89     return 0;
90   else
91     return at (RIGHT) - at (LEFT);
94 template<class T>
96 Interval_t<T>::delta () const
98   return at (RIGHT) - at (LEFT);
101 /* smallest Interval which includes *this and #h#  */
102 template<class T>
103 void
104 Interval_t<T>::unite (Interval_t<T> h)
106   at (LEFT) = min (h.at (LEFT), at (LEFT));
107   at (RIGHT) = max (h.at (RIGHT), at (RIGHT));
110 template<class T>
111 void
112 Interval_t<T>::intersect (Interval_t<T> h)
114   at (LEFT) = max (h.at (LEFT), at (LEFT));
115   at (RIGHT) = min (h.at (RIGHT), at (RIGHT));
118 template<class T>
119 string
120 Interval_t<T>::to_string () const
122   if (is_empty ())
123     return "[empty]";
124   string s ("[");
126   return (s + T_to_string (at (LEFT)) + string (",")
127           + T_to_string (at (RIGHT)) + string ("]"));
130 template<class T>
131 bool
132 Interval_t<T>::contains (T r) const
134   return r >= at (LEFT) && r <= at (RIGHT);
137 #define INTERVAL__INSTANTIATE(T) struct Interval_t<T>;                  \
138   template int Interval__compare (const Interval_t<T> &, Interval_t<T> const &)
140 #endif // INTERVAL_TCC