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 "std-string.hh"
27 #include "drul-array.hh"
29 /* A T interval. This represents the closed interval [left,right].
30 No invariants. T must be a totally ordered ring (with division, anyway ..)
31 At instantiation, the function infinity () has to be defined explicitly. */
33 struct Interval_t : public Drul_array<T>
35 using Drul_array<T>::at;
38 static string T_to_string (T arg);
51 T distance (T t) const
54 return T (t - at (RIGHT));
55 else if (t < at (LEFT))
56 return T (at (LEFT) - t);
62 *this and h are comparable
64 void unite (Interval_t<T> h);
65 void intersect (Interval_t<T> h);
68 at (LEFT) = min (at (LEFT), p);
69 at (RIGHT) = max (at (RIGHT), p);
73 // Returns RIGHT - LEFT, even if the interval is empty.
78 void unite_disjoint (Interval_t<T> h, T padding, Direction d);
79 Interval_t<T> union_disjoint (Interval_t<T> h, T padding, Direction d) const;
81 bool is_empty () const
83 return at (LEFT) > at (RIGHT);
85 bool superset (Interval_t<T> const &) const;
90 Interval_t (Drul_array<T> const &src)
95 Interval_t (T m, T M) : Drul_array<T> (m, M)
98 Interval_t<T> &operator -= (T r)
104 Interval_t<T> &operator += (T r)
110 Interval_t<T> &operator *= (T r)
122 Real linear_combination (Real x) const;
123 string to_string () const;
125 bool contains (T r) const;
137 at (LEFT) = at (RIGHT);
141 static bool left_less (Interval_t<T> const &a, Interval_t<T> const &b)
143 return a[LEFT] < b[LEFT];
148 inclusion ordering. Crash if not comparable.
151 int Interval__compare (const Interval_t<T> &, Interval_t<T> const &);
154 Inclusion ordering. return -2 if not comparable
158 _Interval__compare (const Interval_t<T> &a, Interval_t<T> const &b);
164 #include "compare.hh"
166 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T> &, Interval__compare, template<class T>);
170 intersection (Interval_t<T> a, Interval_t<T> const &b)
178 Interval_t<T> operator + (T a, Interval_t<T> i)
186 Interval_t<T> operator - (Interval_t<T> i, T a)
194 Interval_t<T> operator - (T a, Interval_t<T> i)
203 Interval_t<T> operator + (Interval_t<T> i, T a)
210 Interval_t<T> operator * (T a, Interval_t<T> i)
218 Interval_t<T> operator * (Interval_t<T> i, T a)
225 Interval_t<T>::center () const
227 assert (!is_empty ());
228 return (at (LEFT) + at (RIGHT)) / T (2);
231 typedef Interval_t<Real> Interval;
232 typedef Interval_t<int> Slice; // weird name
235 #endif // INTERVAL_HH