Fixes Issue 1504, allowing feather beam line breaking.
[lilypond/patrick.git] / flower / include / interval.tcc
blob2f002a7e008e24e438d306299268e2aeb8290013
1 /*
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/>.
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 /* 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. */
115 template<class T>
116 void
117 Interval_t<T>::unite_disjoint (Interval_t<T> h, T padding, Direction d)
119   T dir = d;
120   T translation = dir * (at (d) + dir * padding - h.at (-d));
121   if (translation > (T) 0)
122     h.translate (translation);
123   unite (h);
126 template<class T>
127 Interval_t<T>
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);
132   return iv;
135 template<class T>
136 void
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));
143 template<class T>
144 string
145 Interval_t<T>::to_string () const
147   if (is_empty ())
148     return "[empty]";
149   string s ("[");
151   return (s + T_to_string (at (LEFT)) + string (",")
152           + T_to_string (at (RIGHT)) + string ("]"));
155 template<class T>
156 bool
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