]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/interval.tcc
2ad1fa606b7add18165099cde00bd34263af4b93
[lilypond.git] / flower / include / interval.tcc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1996--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #ifndef INTERVAL_TCC
21 #define INTERVAL_TCC
22
23 #include <cassert>
24
25 #include "interval.hh"
26 #include "std-string.hh"
27
28 // MacOS 10.3 problems:
29 // #include <cmath>
30 using namespace std;
31
32 template<class T>
33 int
34 _Interval__compare (const Interval_t<T> &a, Interval_t<T> const &b)
35 {
36   if (a.at (LEFT) == b.at (LEFT) && a.at (RIGHT) == b.at (RIGHT))
37     return 0;
38
39   if (a.at (LEFT) <= b.at (LEFT) && a.at (RIGHT) >= b.at (RIGHT))
40     return 1;
41
42   if (a.at (LEFT) >= b.at (LEFT) && a.at (RIGHT) <= b.at (RIGHT))
43     return -1;
44
45   return -2;
46 }
47
48 template<class T>
49 bool
50 Interval_t<T>::superset (Interval_t<T> const &a) const
51 {
52   int c_i = _Interval__compare (*this, a);
53   if (c_i == -2)
54     return false;
55   return c_i >= 0;
56 }
57
58 template<class T>
59 int
60 Interval__compare (Interval_t<T> const &a, Interval_t<T> const &b)
61 {
62   int i = _Interval__compare (a, b);
63   if (i < -1)
64     assert (false);
65   return i;
66 }
67
68 template<class T>
69 void
70 Interval_t<T>::set_empty ()
71 {
72   at (LEFT) = (T) infinity ();
73   at (RIGHT) = (T) - infinity ();
74 }
75
76 template<class T>
77 void
78 Interval_t<T>::set_full ()
79 {
80   at (LEFT) = (T) - infinity ();
81   at (RIGHT) = (T) infinity ();
82 }
83
84 template<class T>
85 T
86 Interval_t<T>::length () const
87 {
88   if (at (RIGHT) <= at (LEFT))
89     return 0;
90   else
91     return at (RIGHT) - at (LEFT);
92 }
93
94 template<class T>
95 T
96 Interval_t<T>::delta () const
97 {
98   return at (RIGHT) - at (LEFT);
99 }
100
101 /* smallest Interval which includes *this and #h#  */
102 template<class T>
103 void
104 Interval_t<T>::unite (Interval_t<T> h)
105 {
106   at (LEFT) = min (h.at (LEFT), at (LEFT));
107   at (RIGHT) = max (h.at (RIGHT), at (RIGHT));
108 }
109
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)
118 {
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);
124 }
125
126 template<class T>
127 Interval_t<T>
128 Interval_t<T>::union_disjoint (Interval_t<T> h, T padding, Direction d) const
129 {
130   Interval_t<T> iv = *this;
131   iv.unite_disjoint (h, padding, d);
132   return iv;
133 }
134
135 template<class T>
136 void
137 Interval_t<T>::intersect (Interval_t<T> h)
138 {
139   at (LEFT) = max (h.at (LEFT), at (LEFT));
140   at (RIGHT) = min (h.at (RIGHT), at (RIGHT));
141 }
142
143 template<class T>
144 string
145 Interval_t<T>::to_string () const
146 {
147   if (is_empty ())
148     return "[empty]";
149   string s ("[");
150
151   return (s + T_to_string (at (LEFT)) + string (",")
152           + T_to_string (at (RIGHT)) + string ("]"));
153 }
154
155 template<class T>
156 bool
157 Interval_t<T>::contains (T r) const
158 {
159   return r >= at (LEFT) && r <= at (RIGHT);
160 }
161
162 #define INTERVAL__INSTANTIATE(T) struct Interval_t<T>;                  \
163   template int Interval__compare (const Interval_t<T> &, Interval_t<T> const &)
164
165 #endif // INTERVAL_TCC