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