+/*
+ This file is part of LilyPond, the GNU music typesetter.
+
+ Copyright (C) 2001--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
+
+ LilyPond is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ LilyPond is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
+*/
+
#include <cstdio>
#include <cstdlib>
+using namespace std;
#include "item.hh"
#include "system.hh"
+#include "grob-array.hh"
static SCM break_criterion;
void
/*
Perform the substitution for a single grob.
*/
-SCM
+Grob *
substitute_grob (Grob *sc)
{
if (scm_is_integer (break_criterion))
Item *i = dynamic_cast<Item *> (sc);
Direction d = to_dir (break_criterion);
if (i && i->break_status_dir () != d)
- {
- Item *br = i->find_prebroken_piece (d);
- return (br) ? br->self_scm () : SCM_UNDEFINED;
- }
+ {
+ Item *br = i->find_prebroken_piece (d);
+ return br;
+ }
}
else
{
System *line
- = dynamic_cast<System *> (unsmob_grob (break_criterion));
+ = unsmob<System> (break_criterion);
if (sc->get_system () != line)
- {
- sc = sc->find_broken_piece (line);
-
- }
+ sc = sc->find_broken_piece (line);
/* now: !sc || (sc && sc->get_system () == line) */
if (!sc)
- return SCM_UNDEFINED;
+ return 0;
/* now: sc && sc->get_system () == line */
if (!line)
- return sc->self_scm ();
+ return sc;
/*
- We don't return SCM_UNDEFINED for
- suicided grobs, for two reasons
+ We don't return SCM_UNDEFINED for
+ suicided grobs, for two reasons
- - it doesn't work (strange disappearing objects)
+ - it doesn't work (strange disappearing objects)
- - it forces us to mark the parents of a grob, leading to
- a huge recursion in the GC routine.
+ - it forces us to mark the parents of a grob, leading to
+ a huge recursion in the GC routine.
*/
if (sc->common_refpoint (line, X_AXIS)
- && sc->common_refpoint (line, Y_AXIS))
- {
- return sc->self_scm ();
- }
- return SCM_UNDEFINED;
+ && sc->common_refpoint (line, Y_AXIS))
+ return sc;
+ return 0;
}
- return sc->self_scm ();
+ return sc;
}
/*
SCM
do_break_substitution (SCM src)
{
- again:
+again:
- if (unsmob_grob (src))
- return substitute_grob (unsmob_grob (src));
+ if (unsmob<Grob> (src))
+ {
+ Grob *new_ptr = substitute_grob (unsmob<Grob> (src));
+ return new_ptr ? new_ptr->self_scm () : SCM_UNDEFINED;
+ }
else if (scm_is_vector (src))
{
int len = scm_c_vector_length (src);
SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
for (int i = 0; i < len; i++)
- {
- SCM si = scm_int2num (i);
- scm_vector_set_x (nv, si,
- do_break_substitution (scm_vector_ref (src, si)));
- }
+ {
+ SCM si = scm_from_int (i);
+ scm_vector_set_x (nv, si,
+ do_break_substitution (scm_vector_ref (src, si)));
+ }
}
else if (scm_is_pair (src))
{
/*
- UGH! breaks on circular lists.
+ UGH! breaks on circular lists.
*/
SCM newcar = do_break_substitution (scm_car (src));
SCM oldcdr = scm_cdr (src);
- if (newcar == SCM_UNDEFINED
- && (scm_is_pair (oldcdr) || oldcdr == SCM_EOL))
- {
- /*
- This is tail-recursion, ie.
+ if (SCM_UNBNDP (newcar)
+ && (scm_is_pair (oldcdr) || scm_is_null (oldcdr)))
+ {
+ /*
+ This is tail-recursion, ie.
- return do_break_substution (cdr);
+ return do_break_substution (cdr);
- We don't want to rely on the compiler to do this. Without
- tail-recursion, this easily crashes with a stack overflow. */
- src = oldcdr;
- goto again;
- }
+ We don't want to rely on the compiler to do this. Without
+ tail-recursion, this easily crashes with a stack overflow. */
+ src = oldcdr;
+ goto again;
+ }
return scm_cons (newcar, do_break_substitution (oldcdr));
}
return src;
}
-/*
- Perform substitution on GROB_LIST using a constant amount of stack.
-*/
-SCM
-substitute_grob_list (SCM grob_list)
-{
- SCM l = SCM_EOL;
- SCM *tail = &l;
-
- for (SCM s = grob_list; scm_is_pair (s); s = scm_cdr (s))
- {
- SCM n = substitute_grob (unsmob_grob (scm_car (s)));
-
- if (n != SCM_UNDEFINED)
- {
- *tail = scm_cons (n, SCM_EOL);
- tail = SCM_CDRLOC (*tail);
- }
- }
-
- return l;
-}
-
/*
We don't do
Slice rv;
if (System *st = sp->get_system ())
- {
- rv = Slice (st->rank_, st->rank_);
- }
+ rv = Slice (st->get_rank (), st->get_rank ());
else
{
if (sp->broken_intos_.size ())
- rv = Slice (sp->broken_intos_[0]->get_system ()->rank_,
- sp->broken_intos_.top ()->get_system ()->rank_);
+ rv = Slice (sp->broken_intos_[0]->get_system ()->get_rank (),
+ sp->broken_intos_.back ()->get_system ()->get_rank ());
}
return rv;
}
item_system_range (Item *it)
{
if (System *st = it->get_system ())
- return Slice (st->rank_, st->rank_);
+ return Slice (st->get_rank (), st->get_rank ());
Slice sr;
- Direction d = LEFT;
- do
+ for (LEFT_and_RIGHT (d))
{
Item *bi = it->find_prebroken_piece (d);
if (bi && bi->get_system ())
- sr.add_point (bi->get_system ()->rank_);
+ sr.add_point (bi->get_system ()->get_rank ());
}
- while (flip (&d)!= LEFT);
return sr;
}
struct Substitution_entry
{
Grob *grob_;
+
+ /* Assumption: we have less than 32k paper columns. */
short left_;
short right_;
*/
if (sr.is_empty ())
{
- /*
- overflow if we don't treat this specially.
- */
- left_ = 1;
- right_ = -1;
+ /*
+ overflow if we don't treat this specially.
+ */
+ left_ = 1;
+ right_ = -1;
}
else
{
- left_ = sr[LEFT];
- right_ = sr[RIGHT];
+ left_ = (short) sr[LEFT];
+ right_ = (short) sr[RIGHT];
}
}
Substitution_entry ()
item_compare (void const *a, void const *b)
{
return ((Substitution_entry *)a)->left_
- - ((Substitution_entry *)b)->left_;
+ - ((Substitution_entry *)b)->left_;
}
static int
spanner_compare (void const *a, void const *b)
{
return ((Substitution_entry *)a)->length ()
- - ((Substitution_entry *)b)->length ();
+ - ((Substitution_entry *)b)->length ();
}
};
bool
-Spanner::fast_fubstitute_grob_list (SCM sym,
- SCM grob_list)
+Spanner::fast_substitute_grob_array (SCM sym,
+ Grob_array *grob_array)
{
- int len = scm_ilength (grob_list);
+ int len = grob_array->size ();
- /*
- Only do this complicated thing for large lists. This has the added
- advantage that we won't screw up the ordering for elements in
- alignments (which typically don't have more than 10 grobs.)
- */
+ if (grob_array->ordered ())
+ return false;
- if (len < 300)
+ if (len < 15)
return false;
/*
- TODO : should not free it some time?
+ We store items on the left, spanners on the right in this vector.
+
+ FIXME: will not multithread.
*/
static Substitution_entry *vec;
static int vec_room;
Slice system_range = spanner_system_range (this);
- Array<Slice> it_indices;
- Array<Slice> sp_indices;
- for (int i = 0; i <= system_range.length (); i++)
- {
- it_indices.push (Slice (len, 0));
- sp_indices.push (Slice (len, 0));
- }
+ int spanner_index = len;
+ int item_index = 0;
- int sp_index = len;
- int it_index = 0;
- for (SCM s = grob_list; scm_is_pair (s); s = scm_cdr (s))
+ for (vsize i = 0; i < grob_array->size (); i++)
{
- Grob *g = unsmob_grob (scm_car (s));
+ Grob *g = grob_array->grob (i);
Slice sr = grob_system_range (g);
sr.intersect (system_range);
int idx = 0;
if (dynamic_cast<Spanner *> (g))
- {
- idx =--sp_index;
- }
+ idx = --spanner_index;
else if (dynamic_cast<Item *> (g))
- {
- idx = it_index++;
- }
+ idx = item_index++;
vec[idx].set (g, sr);
}
- qsort (vec, it_index,
- sizeof (Substitution_entry), &Substitution_entry::item_compare);
+ qsort (vec, item_index,
+ sizeof (Substitution_entry), &Substitution_entry::item_compare);
+
+ vector<Slice> item_indices;
+ vector<Slice> spanner_indices;
+ for (int i = 0; i <= system_range.length (); i++)
+ {
+ item_indices.push_back (Slice (len, 0));
+ spanner_indices.push_back (Slice (len, 0));
+ }
- Array<Slice> *arrs[]
- = {
- &it_indices, &sp_indices
+ vector<Slice> *arrs[]
+ =
+ {
+ &item_indices, &spanner_indices
};
- for (int i = 0; i < it_index;i++)
+ for (int i = 0; i < item_index; i++)
{
for (int j = vec[i].left_; j <= vec[i].right_; j++)
- {
- it_indices[j - system_range[LEFT]].add_point (i);
- }
+ item_indices[j - system_range[LEFT]].add_point (i);
}
/*
- sorting vec[sp_index.. len]
+ sorting vec[spanner_index.. len]
is a waste of time -- the staff-spanners screw up the
ordering, since they go across the entire score.
*/
- for (int i = sp_indices.size (); i--;)
- sp_indices[i]= Slice (sp_index, len - 1);
+ for (vsize i = spanner_indices.size (); i--;)
+ spanner_indices[i] = Slice (spanner_index, len - 1);
- assert (it_index <= sp_index);
+ assert (item_index <= spanner_index);
- assert (broken_intos_.size () == system_range.length () + 1);
- for (int i = 0; i < broken_intos_.size (); i++)
+ assert ((broken_intos_.size () == (vsize)system_range.length () + 1)
+ || (broken_intos_.empty () && system_range.length () == 0));
+ for (vsize i = 0; i < broken_intos_.size (); i++)
{
Grob *sc = broken_intos_[i];
System *l = sc->get_system ();
- set_break_subsititution (l ? l->self_scm (): SCM_UNDEFINED);
-
- SCM newval = SCM_EOL;
- SCM *tail = &newval;
-
- for (int k = 0; k < 2;k++)
- for (int j = (*arrs[k])[i][LEFT]; j <= (*arrs[k])[i][RIGHT]; j++)
- {
- SCM subs = substitute_grob (vec[j].grob_);
- if (subs!= SCM_UNDEFINED)
- {
- *tail = scm_cons (subs, SCM_EOL);
-
- tail = SCM_CDRLOC (*tail);
- }
-
- }
+ set_break_subsititution (l ? l->self_scm () : SCM_UNDEFINED);
+
+ SCM newval = sc->internal_get_object (sym);
+ if (!unsmob<Grob_array> (newval))
+ {
+ newval = Grob_array::make_array ();
+ sc->set_object (sym, newval);
+ }
+
+ Grob_array *new_array = unsmob<Grob_array> (newval);
+ for (int k = 0; k < 2; k++)
+ for (int j = (*arrs[k])[i][LEFT]; j <= (*arrs[k])[i][RIGHT]; j++)
+ {
+ Grob *substituted = substitute_grob (vec[j].grob_);
+ if (substituted)
+ new_array->add (substituted);
+ }
#ifdef PARANOIA
-
printf ("%d (%d), sp %d (%d)\n",
- it_indices [i].length (), it_index,
- sp_indices[i].length (), len -sp_index);
+ item_indices [i].length (), item_index,
+ spanner_indices[i].length (), len - spanner_index);
{
- SCM l1 = substitute_grob_list (grob_list);
- assert (scm_ilength (l1) == scm_ilength (newval));
+ SCM l1 = substitute_grob_list (grob_list);
+ assert (scm_ilength (l1) == scm_ilength (newval));
}
#endif
-
- /*
- see below.
- */
- if (sym == ly_symbol2scm ("all-elements"))
- sc->mutable_property_alist_
- = scm_assq_remove_x (sc->mutable_property_alist_,
- ly_symbol2scm ("all-elements"));
-
- sc->mutable_property_alist_ = scm_acons (sym, newval,
- sc->mutable_property_alist_);
}
return true;
pthreads. pthreads impose small limits on the stack size.
*/
SCM
-substitute_mutable_property_alist (SCM alist)
+substitute_object_alist (SCM alist, SCM dest)
{
- SCM grob_list_p = ly_lily_module_constant ("grob-list?");
-
SCM l = SCM_EOL;
SCM *tail = &l;
for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
{
SCM sym = scm_caar (s);
SCM val = scm_cdar (s);
- SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
- if (type == grob_list_p)
- val = substitute_grob_list (val);
+ if (Grob_array *orig = unsmob<Grob_array> (val))
+ {
+ SCM handle = scm_assq (sym, dest);
+ SCM newval
+ = (scm_is_pair (handle))
+ ? scm_cdr (handle)
+ : Grob_array::make_array ();
+
+ Grob_array *new_arr = unsmob<Grob_array> (newval);
+ // TODO: What if new_arr is null?
+ new_arr->filter_map_assign (*orig, substitute_grob);
+ val = newval;
+ }
else
- val = do_break_substitution (val);
-
- if (val != SCM_UNDEFINED)
- {
- /*
- for ly:grob? properties, SCM_UNDEFINED could leak out
- through ly:grob-property
- */
- *tail = scm_cons (scm_cons (sym, val), SCM_EOL);
- tail = SCM_CDRLOC (*tail);
- }
+ val = do_break_substitution (val);
+
+ if (!SCM_UNBNDP (val))
+ {
+ /*
+ for ly:grob? properties, SCM_UNDEFINED could leak out
+ through ly:grob-property
+ */
+ *tail = scm_cons (scm_cons (sym, val), SCM_EOL);
+ tail = SCM_CDRLOC (*tail);
+ }
}
return l;
}
void
Spanner::substitute_one_mutable_property (SCM sym,
- SCM val)
+ SCM val)
{
- SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
Spanner *s = this;
bool fast_done = false;
- SCM grob_list_p = ly_lily_module_constant ("grob-list?");
- if (type == grob_list_p)
- fast_done = s->fast_fubstitute_grob_list (sym, val);
+ Grob_array *grob_array = unsmob<Grob_array> (val);
+ if (grob_array)
+ fast_done = s->fast_substitute_grob_array (sym, grob_array);
if (!fast_done)
- for (int i = 0; i < s->broken_intos_ .size (); i++)
+ for (vsize i = 0; i < s->broken_intos_.size (); i++)
{
- Grob *sc = s->broken_intos_[i];
- System *l = sc->get_system ();
- set_break_subsititution (l ? l->self_scm () : SCM_UNDEFINED);
-
- SCM newval = (type == grob_list_p)
- ? substitute_grob_list (val)
- : do_break_substitution (val);
-
- /*
- For the substitution of a single property, we tack the result onto
- mutable_property_alist_ ; mutable_property_alist_ is empty after
- Grob::Grob (Grob const&), except that System has all-elements set,
- as a side product of typeset_grob () on newly copied spanners.
-
- Here we clear that list explicitly to free some memory and
- counter some of the confusion I encountered while debugging
- another problem
-
- (hwn 4/2/04)
- */
- if (sym == ly_symbol2scm ("all-elements"))
- sc->mutable_property_alist_
- = scm_assq_remove_x (sc->mutable_property_alist_,
- ly_symbol2scm ("all-elements"));
-
- sc->mutable_property_alist_ = scm_cons (scm_cons (sym, newval),
- sc->mutable_property_alist_);
+ Grob *sc = s->broken_intos_[i];
+ System *l = sc->get_system ();
+ set_break_subsititution (l ? l->self_scm () : SCM_UNDEFINED);
+
+ if (grob_array)
+ {
+ SCM newval = sc->internal_get_object (sym);
+ if (!unsmob<Grob_array> (newval))
+ {
+ newval = Grob_array::make_array ();
+ sc->set_object (sym, newval);
+ }
+ Grob_array *new_arr = unsmob<Grob_array> (newval);
+ new_arr->filter_map_assign (*grob_array, substitute_grob);
+ }
+ else
+ {
+ SCM newval = do_break_substitution (val);
+ sc->set_object (sym, newval);
+ }
}
}
+void
+Grob::substitute_object_links (SCM crit, SCM orig)
+{
+ set_break_subsititution (crit);
+ object_alist_ = substitute_object_alist (orig, object_alist_);
+}