2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1996--2011 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/>.
25 #include "interval.hh"
26 #include "std-string.hh"
28 // MacOS 10.3 problems:
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))
39 if (a.at (LEFT) <= b.at (LEFT) && a.at (RIGHT) >= b.at (RIGHT))
42 if (a.at (LEFT) >= b.at (LEFT) && a.at (RIGHT) <= b.at (RIGHT))
50 Interval_t<T>::superset (Interval_t<T> const &a) const
52 int c_i = _Interval__compare (*this, a);
60 Interval__compare (Interval_t<T> const &a, Interval_t<T> const &b)
62 int i = _Interval__compare (a, b);
70 Interval_t<T>::set_empty ()
72 at (LEFT) = (T) infinity ();
73 at (RIGHT) = (T) -infinity ();
78 Interval_t<T>::set_full ()
80 at (LEFT) = (T) -infinity ();
81 at (RIGHT) = (T) infinity ();
86 Interval_t<T>::length () const
88 if (at (RIGHT) <= at (LEFT))
91 return at (RIGHT) - at (LEFT);
96 Interval_t<T>::delta () const
98 return at (RIGHT) - at (LEFT);
101 /* smallest Interval which includes *this and #h# */
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 /* Unites h and this interval, but in such a way
111 that h will lie in a particular direction from this
112 interval, with a minimum amount of space in between.
113 (That is, h will be translated before we unite, if
114 that is necessary to prevent overlap. */
117 Interval_t<T>::unite_disjoint (Interval_t<T> h, T padding, Direction d)
120 T translation = dir * (at (d) + dir * padding - h.at (-d));
121 if (translation > (T) 0)
122 h.translate (translation);
128 Interval_t<T>::union_disjoint (Interval_t<T> h, T padding, Direction d) const
130 Interval_t<T> iv = *this;
131 iv.unite_disjoint (h, padding, d);
137 Interval_t<T>::intersect (Interval_t<T> h)
139 at (LEFT) = max (h.at (LEFT), at (LEFT));
140 at (RIGHT) = min (h.at (RIGHT), at (RIGHT));
145 Interval_t<T>::to_string () const
151 return (s + T_to_string (at (LEFT)) + string (",")
152 + T_to_string (at (RIGHT)) + string ("]"));
157 Interval_t<T>::contains (T r) const
159 return r >= at (LEFT) && r <= at (RIGHT);
162 #define INTERVAL__INSTANTIATE(T) struct Interval_t<T>; \
163 template int Interval__compare (const Interval_t<T> &, Interval_t<T> const &)
165 #endif // INTERVAL_TCC