2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1996--2015 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:
33 _Interval__compare (const Interval_t<T> &a, Interval_t<T> const &b)
35 if (a.at (LEFT) == b.at (LEFT) && a.at (RIGHT) == b.at (RIGHT))
38 if (a.at (LEFT) <= b.at (LEFT) && a.at (RIGHT) >= b.at (RIGHT))
41 if (a.at (LEFT) >= b.at (LEFT) && a.at (RIGHT) <= b.at (RIGHT))
49 Interval_t<T>::superset (Interval_t<T> const &a) const
51 int c_i = _Interval__compare (*this, a);
59 Interval__compare (Interval_t<T> const &a, Interval_t<T> const &b)
61 int i = _Interval__compare (a, b);
69 Interval_t<T>::set_empty ()
71 at (LEFT) = (T) infinity ();
72 at (RIGHT) = (T) - infinity ();
77 Interval_t<T>::set_full ()
79 at (LEFT) = (T) - infinity ();
80 at (RIGHT) = (T) infinity ();
85 Interval_t<T>::length () const
87 if (at (RIGHT) <= at (LEFT))
90 return at (RIGHT) - at (LEFT);
95 Interval_t<T>::delta () const
97 return at (RIGHT) - at (LEFT);
100 /* smallest Interval which includes *this and #h# */
103 Interval_t<T>::unite (Interval_t<T> h)
105 at (LEFT) = std::min (h.at (LEFT), at (LEFT));
106 at (RIGHT) = std::max (h.at (RIGHT), at (RIGHT));
109 /* Unites h and this interval, but in such a way
110 that h will lie in a particular direction from this
111 interval, with a minimum amount of space in between.
112 (That is, h will be translated before we unite, if
113 that is necessary to prevent overlap. */
116 Interval_t<T>::unite_disjoint (Interval_t<T> h, T padding, Direction d)
119 T translation = dir * (at (d) + dir * padding - h.at (-d));
120 if (translation > (T) 0)
121 h.translate (translation);
127 Interval_t<T>::union_disjoint (Interval_t<T> h, T padding, Direction d) const
129 Interval_t<T> iv = *this;
130 iv.unite_disjoint (h, padding, d);
136 Interval_t<T>::intersect (Interval_t<T> h)
138 at (LEFT) = std::max (h.at (LEFT), at (LEFT));
139 at (RIGHT) = std::min (h.at (RIGHT), at (RIGHT));
144 Interval_t<T>::to_string () const
150 return (s + T_to_string (at (LEFT)) + std::string (",")
151 + T_to_string (at (RIGHT)) + std::string ("]"));
156 Interval_t<T>::contains (T r) const
158 return r >= at (LEFT) && r <= at (RIGHT);
161 #define INTERVAL__INSTANTIATE(T) struct Interval_t<T>; \
162 template int Interval__compare (const Interval_t<T> &, Interval_t<T> const &)
164 #endif // INTERVAL_TCC