lilypond-1.3.7
[lilypond.git] / lily / misc.cc
blob3f1f2ad949fb052023c6a682e44e1a3cc4bf1d3a
1 /*
2 misc.cc -- implement various stuff
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
8 */
10 #include <math.h>
12 #include "misc.hh"
14 #ifndef STANDALONE
15 #include "item.hh"
16 #endif
18 int
19 intlog2(int d) {
20 int i=0;
21 while (!(d&1))
23 d/= 2;
24 i++;
26 assert (!(d/2));
27 return i;
30 double
31 log_2(double x) {
32 return log (x) /log (2.0);
35 #ifndef STANDALONE
36 Interval
37 itemlist_width (const Array<Item*> &its)
39 Interval iv ;
40 iv.set_empty();
42 for (int j =0; j < its.size(); j++)
44 iv.unite (its[j]->extent (X_AXIS));
47 return iv;
50 #endif
54 TODO
55 group in some Array_*
56 make more generic / templatise
58 int
59 get_lower_bound (Array<Real> const& positions, Real x)
61 if (x < positions[0])
62 return 0;
63 for (int i = 1; i < positions.size (); i++)
64 if (x < positions[i])
65 return i - 1;
66 return positions.size () - 1;
69 Slice
70 get_bounds_slice (Array<Real> const& positions, Real x)
72 int l = get_lower_bound (positions, x);
73 int u = positions.size () - 1 <? l + 1;
74 if (x < positions[l])
75 u = l;
76 return Slice (l, u);
79 Interval
80 get_bounds_iv (Array<Real> const& positions, Real x)
82 Slice slice = get_bounds_slice (positions, x);
83 return Interval (positions[slice[SMALLER]], positions[slice[BIGGER]]);
86 // silly name
87 Interval
88 quantise_iv (Array<Real> const& positions, Real period, Real x)
91 ugh
92 assume that
93 * positions are sorted,
94 * positions are nonnegative
95 * period starts at zero
98 int n = (int)(x / period);
99 Real frac = (x / period - n) * period;
100 if (frac < 0)
102 frac += period;
103 n--;
106 Slice slice = get_bounds_slice (positions, frac);
107 Interval iv(positions[slice[SMALLER]], positions[slice[BIGGER]]);
109 if (slice[SMALLER] == slice[BIGGER])
111 if (slice[SMALLER] == 0)
112 iv[SMALLER] = - period + positions.top ();
113 else
114 iv[BIGGER] = period + positions[0];
117 iv += period * n;
119 return iv;