2 break-substitution.cc -- implement grob break substitution.
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
13 #include "grob-array.hh"
17 static SCM break_criterion;
19 set_break_subsititution (SCM criterion)
21 break_criterion = criterion;
25 Perform the substitution for a single grob.
28 substitute_grob (Grob *sc)
30 if (scm_is_integer (break_criterion))
32 Item *i = dynamic_cast<Item *> (sc);
33 Direction d = to_dir (break_criterion);
34 if (i && i->break_status_dir () != d)
36 Item *br = i->find_prebroken_piece (d);
43 = dynamic_cast<System *> (unsmob_grob (break_criterion));
44 if (sc->get_system () != line)
45 sc = sc->find_broken_piece (line);
47 /* now: !sc || (sc && sc->get_system () == line) */
51 /* now: sc && sc->get_system () == line */
56 We don't return SCM_UNDEFINED for
57 suicided grobs, for two reasons
59 - it doesn't work (strange disappearing objects)
61 - it forces us to mark the parents of a grob, leading to
62 a huge recursion in the GC routine.
65 if (sc->common_refpoint (line, X_AXIS)
66 && sc->common_refpoint (line, Y_AXIS))
75 Do break substitution in S, using CRITERION. Return new value.
76 CRITERION is either a SMOB pointer to the desired line, or a number
77 representing the break direction. Do not modify SRC.
79 It is rather tightly coded, since it takes a lot of time; it is
80 one of the top functions in the profile.
82 We don't pass break_criterion as a parameter, since it is
83 `constant', but takes up stack space.
85 It would be nice if we could do this in-place partially. We now
86 generate a lot of garbage.
89 do_break_substitution (SCM src)
93 if (unsmob_grob (src))
95 Grob *new_ptr = substitute_grob (unsmob_grob (src));
96 return new_ptr ? new_ptr->self_scm () : SCM_UNDEFINED;
98 else if (scm_is_vector (src))
100 int len = scm_c_vector_length (src);
101 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
102 for (int i = 0; i < len; i++)
104 SCM si = scm_from_int (i);
105 scm_vector_set_x (nv, si,
106 do_break_substitution (scm_vector_ref (src, si)));
109 else if (scm_is_pair (src))
112 UGH! breaks on circular lists.
114 SCM newcar = do_break_substitution (scm_car (src));
115 SCM oldcdr = scm_cdr (src);
117 if (newcar == SCM_UNDEFINED
118 && (scm_is_pair (oldcdr) || oldcdr == SCM_EOL))
121 This is tail-recursion, ie.
123 return do_break_substution (cdr);
125 We don't want to rely on the compiler to do this. Without
126 tail-recursion, this easily crashes with a stack overflow. */
131 return scm_cons (newcar, do_break_substitution (oldcdr));
140 Perform substitution on GROB_LIST using a constant amount of stack.
142 vector<Grob*> temporary_substition_array;
144 substitute_grob_array (Grob_array *grob_arr, Grob_array *new_arr)
146 vector<Grob*> &old_grobs (grob_arr->array_reference ());
147 vector<Grob*> *new_grobs (new_arr == grob_arr
148 ? & temporary_substition_array
149 : &new_arr->array_reference ());
151 new_grobs->resize (old_grobs.size ());
152 Grob **array = (Grob **) new_grobs->data ();
154 for (vsize i = 0; i < old_grobs.size (); i++)
156 Grob *orig = old_grobs[i];
157 Grob *new_grob = substitute_grob (orig);
162 new_grobs->resize (ptr - array);
163 if (new_arr == grob_arr)
164 new_arr->set_array (*new_grobs);
170 forall b in broken-childs:
171 forall p in properties:
172 forall g in p (if grob-list):
175 for spanners since this is O (SYSTEMCOUNT * GROBCOUNT), and SYSTEMCOUNT =
176 O (GROBCOUNT), we have a quadratic algorithm. --for a single spanner
178 This is problematic: with large (long) scores, the costs can be
179 significant; especially all-elements in System, can become huge. For
180 a typical 50 page score, it requires running through a 100k list 50
185 forall p in properties:
188 put grob list in array,
190 reorder array so spanners are separate -- O (grobcount)
192 find first and last indexes of grobs on a specific system
194 for items this is O (itemcount)
196 for spanners this is O (sum-of spanner-system-ranges)
198 perform the substitution O (sum-of spanner-system-ranges)
201 The complexity is harder to determine, but should be subquadratic;
203 For the situation above, we run through the entire 100k list once,
204 and also (more or less) once through the item part of the 100k (say
205 98k elements) of the list.
208 These timings were measured without -O2.
210 lehre, before 28.98 seconds, after: 27.91 seconds, 3.5 %.
212 coriolan, before 2:30, after: 1:59. Increase of 20%.
214 moz-k498-p1, before 24.10, after: 19.790s, Increase of 18%
218 spanner_system_range (Spanner *sp)
222 if (System *st = sp->get_system ())
223 rv = Slice (st->get_rank (), st->get_rank ());
226 if (sp->broken_intos_.size ())
227 rv = Slice (sp->broken_intos_[0]->get_system ()->get_rank (),
228 sp->broken_intos_.back ()->get_system ()->get_rank ());
234 item_system_range (Item *it)
236 if (System *st = it->get_system ())
237 return Slice (st->get_rank (), st->get_rank ());
243 Item *bi = it->find_prebroken_piece (d);
244 if (bi && bi->get_system ())
245 sr.add_point (bi->get_system ()->get_rank ());
247 while (flip (&d) != LEFT);
253 grob_system_range (Grob *g)
255 if (Spanner *s = dynamic_cast<Spanner *> (g))
256 return spanner_system_range (s);
257 else if (Item *it = dynamic_cast<Item *> (g))
258 return item_system_range (it);
263 struct Substitution_entry
269 void set (Grob *g, Slice sr)
273 duh, don't support scores with more than 32000 systems.
278 overflow if we don't treat this specially.
289 Substitution_entry ()
295 int length () { return right_ - left_; }
297 item_compare (void const *a, void const *b)
299 return ((Substitution_entry *)a)->left_
300 - ((Substitution_entry *)b)->left_;
304 spanner_compare (void const *a, void const *b)
306 return ((Substitution_entry *)a)->length ()
307 - ((Substitution_entry *)b)->length ();
312 Spanner::fast_substitute_grob_array (SCM sym,
313 Grob_array *grob_array)
315 int len = grob_array->size ();
317 if (grob_array->ordered ())
324 We store items on the left, spanners on the right in this vector.
326 static Substitution_entry *vec;
331 vec = (Substitution_entry *) realloc (vec, sizeof (Substitution_entry) * len);
335 Slice system_range = spanner_system_range (this);
337 int spanner_index = len;
340 for (vsize i = 0; i < grob_array->size (); i++)
342 Grob *g = grob_array->grob (i);
344 Slice sr = grob_system_range (g);
345 sr.intersect (system_range);
348 if (dynamic_cast<Spanner *> (g))
349 idx = --spanner_index;
350 else if (dynamic_cast<Item *> (g))
353 vec[idx].set (g, sr);
356 qsort (vec, item_index,
357 sizeof (Substitution_entry), &Substitution_entry::item_compare);
359 vector<Slice> item_indices;
360 vector<Slice> spanner_indices;
361 for (int i = 0; i <= system_range.length (); i++)
363 item_indices.push_back (Slice (len, 0));
364 spanner_indices.push_back (Slice (len, 0));
367 vector<Slice> *arrs[]
369 &item_indices, &spanner_indices
372 for (int i = 0; i < item_index;i++)
374 for (int j = vec[i].left_; j <= vec[i].right_; j++)
375 item_indices[j - system_range[LEFT]].add_point (i);
379 sorting vec[spanner_index.. len]
380 is a waste of time -- the staff-spanners screw up the
381 ordering, since they go across the entire score.
383 for (vsize i = spanner_indices.size (); i--;)
384 spanner_indices[i] = Slice (spanner_index, len - 1);
386 assert (item_index <= spanner_index);
388 assert ((broken_intos_.size () == (vsize)system_range.length () + 1)
389 || (broken_intos_.empty () && system_range.length () == 0));
390 for (vsize i = 0; i < broken_intos_.size (); i++)
392 Grob *sc = broken_intos_[i];
393 System *l = sc->get_system ();
394 set_break_subsititution (l ? l->self_scm () : SCM_UNDEFINED);
396 SCM newval = sc->internal_get_object (sym);
397 if (!unsmob_grob_array (newval))
399 newval = Grob_array::make_array ();
400 sc->set_object (sym, newval);
403 Grob_array *new_array = unsmob_grob_array (newval);
404 for (int k = 0; k < 2;k++)
405 for (int j = (*arrs[k])[i][LEFT]; j <= (*arrs[k])[i][RIGHT]; j++)
407 Grob *substituted = substitute_grob (vec[j].grob_);
409 new_array->add (substituted);
413 printf ("%d (%d), sp %d (%d)\n",
414 item_indices [i].length (), item_index,
415 spanner_indices[i].length (), len -spanner_index);
418 SCM l1 = substitute_grob_list (grob_list);
419 assert (scm_ilength (l1) == scm_ilength (newval));
428 Although the substitution can be written as
430 property_alist = do_substitution (other_property_alist),
432 we have a special function here: we want to invoke a special
433 function for lists of grobs. These can be very long for large
434 orchestral scores (eg. 1M elements). do_break_substitution () can
435 recurse many levels, taking lots of stack space.
437 This becomes a problem if lily is linked against guile with
438 pthreads. pthreads impose small limits on the stack size.
441 substitute_object_alist (SCM alist, SCM dest)
445 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
447 SCM sym = scm_caar (s);
448 SCM val = scm_cdar (s);
450 if (Grob_array *orig = unsmob_grob_array (val))
452 SCM handle = scm_assq (sym, dest);
454 = (scm_is_pair (handle))
456 : Grob_array::make_array ();
458 Grob_array *new_arr = unsmob_grob_array (newval);
460 substitute_grob_array (orig, new_arr);
464 val = do_break_substitution (val);
466 if (val != SCM_UNDEFINED)
469 for ly:grob? properties, SCM_UNDEFINED could leak out
470 through ly:grob-property
472 *tail = scm_cons (scm_cons (sym, val), SCM_EOL);
473 tail = SCM_CDRLOC (*tail);
480 Spanner::substitute_one_mutable_property (SCM sym,
485 bool fast_done = false;
486 Grob_array *grob_array = unsmob_grob_array (val);
488 fast_done = s->fast_substitute_grob_array (sym, grob_array);
491 for (vsize i = 0; i < s->broken_intos_.size (); i++)
493 Grob *sc = s->broken_intos_[i];
494 System *l = sc->get_system ();
495 set_break_subsititution (l ? l->self_scm () : SCM_UNDEFINED);
499 SCM newval = sc->internal_get_object (sym);
500 if (!unsmob_grob_array (newval))
502 newval = Grob_array::make_array ();
503 sc->set_object (sym, newval);
505 substitute_grob_array (grob_array, unsmob_grob_array (newval));
509 SCM newval = do_break_substitution (val);
510 sc->set_object (sym, newval);
516 Grob::substitute_object_links (SCM crit, SCM orig)
518 set_break_subsititution (crit);
519 object_alist_ = substitute_object_alist (orig, object_alist_);