Typos: Lilypond -> LilyPond, explicite -> explicit, fix docstring
[lilypond/mpolesky.git] / flower / include / interval.hh
blob27e221af8c7d61a3d937a7971941adec7fb2364c
1 /*
2 interval.hh -- part of flowerlib
4 (c) 1996--2009 Han-Wen Nienhuys
5 */
7 #ifndef INTERVAL_HH
8 #define INTERVAL_HH
10 #include <math.h>
12 #include "flower-proto.hh"
13 #include "drul-array.hh"
15 /* A T interval. This represents the closed interval [left,right].
16 No invariants. T must be a totally ordered ring (with division, anyway ..)
17 At instantiation, the function infinity () has to be defined explicitly. */
18 template<class T>
19 struct Interval_t : public Drul_array<T>
21 Drul_array<T>::at;
23 static T infinity ();
24 static string T_to_string (T arg);
25 T center () const;
26 void translate (T t)
28 at (LEFT) += t;
29 at (RIGHT) += t;
31 void widen (T t)
33 at (LEFT) -= t;
34 at (RIGHT) += t;
37 T distance (T t) const
39 if (t > at (RIGHT))
40 return T (t - at (RIGHT));
41 else if (t < at (LEFT))
42 return T (at (LEFT) - t);
43 else
44 return T (0);
46 /**
47 PRE
48 *this and h are comparable
50 void unite (Interval_t<T> h);
51 void intersect (Interval_t<T> h);
52 void add_point (T p)
54 at (LEFT) = min (at (LEFT), p);
55 at (RIGHT) = max (at (RIGHT), p);
57 T length () const;
58 T delta () const;
59 void set_empty ();
60 void set_full ();
62 bool is_empty () const
64 return at (LEFT) > at (RIGHT);
66 bool superset (Interval_t<T> const &) const;
67 Interval_t ()
69 set_empty ();
71 Interval_t (Drul_array<T> const &src)
72 : Drul_array<T> (src)
76 Interval_t (T m, T M) : Drul_array<T> (m, M)
79 Interval_t<T> &operator -= (T r)
81 *this += -r;
82 return *this;
85 Interval_t<T> &operator += (T r)
87 at (LEFT) += r;
88 at (RIGHT) += r;
89 return *this;
91 Interval_t<T> &operator *= (T r)
93 if (!is_empty ())
95 at (LEFT) *= r;
96 at (RIGHT) *= r;
97 if (r < T (0))
98 swap ();
100 return *this;
103 Real linear_combination (Real x) const;
104 string to_string () const;
106 bool contains (T r) const;
107 void negate ()
109 T r = -at (LEFT);
110 T l = -at (RIGHT);
111 at (LEFT) = l;
112 at (RIGHT) = r;
115 void swap ()
117 T t = at (LEFT);
118 at (LEFT) = at (RIGHT);
119 at (RIGHT) = t;
122 static bool left_less (Interval_t<T> const &a, Interval_t<T> const &b)
124 return a[LEFT] < b[RIGHT];
129 inclusion ordering. Crash if not comparable.
131 template<class T>
132 int Interval__compare (const Interval_t<T> &, Interval_t<T> const &);
135 Inclusion ordering. return -2 if not comparable
137 template<class T>
139 _Interval__compare (const Interval_t<T> &a, Interval_t<T> const &b);
142 INLINE
145 #include "compare.hh"
147 TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T> &, Interval__compare, template<class T>);
149 template<class T>
150 inline Interval_t<T>
151 intersection (Interval_t<T> a, Interval_t<T> const &b)
153 a.intersect (b);
154 return a;
157 template<class T>
158 inline
159 Interval_t<T> operator + (T a, Interval_t<T> i)
161 i += a;
162 return i;
165 template<class T>
166 inline
167 Interval_t<T> operator - (Interval_t<T> i, T a)
169 i += -a;
170 return i;
173 template<class T>
174 inline
175 Interval_t<T> operator - (T a, Interval_t<T> i)
177 i.negate ();
178 i += a;
179 return i;
182 template<class T>
183 inline
184 Interval_t<T> operator + (Interval_t<T> i, T a)
186 return a + i;
189 template<class T>
190 inline
191 Interval_t<T> operator * (T a, Interval_t<T> i)
193 i *= a;
194 return i;
197 template<class T>
198 inline
199 Interval_t<T> operator * (Interval_t<T> i, T a)
201 return a * i;
204 template<class T>
205 inline T
206 Interval_t<T>::center () const
208 assert (!is_empty ());
209 return (at (LEFT) + at (RIGHT)) / T (2);
212 typedef Interval_t<Real> Interval;
213 typedef Interval_t<int> Slice; // weird name
216 #endif // INTERVAL_HH