2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1996--2015 Han-Wen Nienhuys
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 "flower-proto.hh"
26 #include "drul-array.hh"
28 /* A T interval. This represents the closed interval [left,right].
29 No invariants. T must be a totally ordered ring (with division, anyway ..)
30 At instantiation, the function infinity () has to be defined explicitly. */
32 struct Interval_t : public Drul_array<T>
34 using Drul_array<T>::at;
37 static string T_to_string (T arg);
50 T distance (T t) const
53 return T (t - at (RIGHT));
54 else if (t < at (LEFT))
55 return T (at (LEFT) - t);
61 *this and h are comparable
63 void unite (Interval_t<T> h);
64 void intersect (Interval_t<T> h);
67 at (LEFT) = min (at (LEFT), p);
68 at (RIGHT) = max (at (RIGHT), p);
72 // Returns RIGHT - LEFT, even if the interval is empty.
77 void unite_disjoint (Interval_t<T> h, T padding, Direction d);
78 Interval_t<T> union_disjoint (Interval_t<T> h, T padding, Direction d) const;
80 bool is_empty () const
82 return at (LEFT) > at (RIGHT);
84 bool superset (Interval_t<T> const &) const;
89 Interval_t (Drul_array<T> const &src)
94 Interval_t (T m, T M) : Drul_array<T> (m, M)
97 Interval_t<T> &operator -= (T r)
103 Interval_t<T> &operator += (T r)
109 Interval_t<T> &operator *= (T r)
121 Real linear_combination (Real x) const;
122 string to_string () const;
124 bool contains (T r) const;
136 at (LEFT) = at (RIGHT);
140 static bool left_less (Interval_t<T> const &a, Interval_t<T> const &b)
142 return a[LEFT] < b[LEFT];
147 inclusion ordering. Crash if not comparable.
150 int Interval__compare (const Interval_t<T> &, Interval_t<T> const &);
153 Inclusion ordering. return -2 if not comparable
157 _Interval__compare (const Interval_t<T> &a, Interval_t<T> const &b);
163 #include "compare.hh"
165 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T> &, Interval__compare, template<class T>);
169 intersection (Interval_t<T> a, Interval_t<T> const &b)
177 Interval_t<T> operator + (T a, Interval_t<T> i)
185 Interval_t<T> operator - (Interval_t<T> i, T a)
193 Interval_t<T> operator - (T a, Interval_t<T> i)
202 Interval_t<T> operator + (Interval_t<T> i, T a)
209 Interval_t<T> operator * (T a, Interval_t<T> i)
217 Interval_t<T> operator * (Interval_t<T> i, T a)
224 Interval_t<T>::center () const
226 assert (!is_empty ());
227 return (at (LEFT) + at (RIGHT)) / T (2);
230 typedef Interval_t<Real> Interval;
231 typedef Interval_t<int> Slice; // weird name
234 #endif // INTERVAL_HH