]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/break-substitution.cc
Run `make grand-replace'.
[lilypond.git] / lily / break-substitution.cc
index 31a887e20cafd39eaf2de6326fa60d170d9e49f9..8326cee5eeb8f045a3bf1cbc46158c6432c2848a 100644 (file)
@@ -1,11 +1,20 @@
-#include <stdio.h>
+/*
+  break-substitution.cc -- implement grob break substitution.
+
+  source file of the GNU LilyPond music typesetter
+
+  (c) 2001--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
+*/
+
+#include <cstdio>
+#include <cstdlib>
+using namespace std;
 
 
-#include  "grob.hh"
 #include "item.hh"
 #include "item.hh"
-#include  "spanner.hh"
-#include  "system.hh"
+#include "system.hh"
+#include "grob-array.hh"
 
 
-static SCM break_criterion; 
+static SCM break_criterion;
 void
 set_break_subsititution (SCM criterion)
 {
 void
 set_break_subsititution (SCM criterion)
 {
@@ -13,38 +22,35 @@ set_break_subsititution (SCM criterion)
 }
 
 /*
 }
 
 /*
-  Perform the substitution for a single grob.   
- */
-SCM
+  Perform the substitution for a single grob.
+*/
+Grob *
 substitute_grob (Grob *sc)
 {
 substitute_grob (Grob *sc)
 {
-  if (SCM_INUMP (break_criterion))
+  if (scm_is_integer (break_criterion))
     {
     {
-      Item * i = dynamic_cast<Item*> (sc);
+      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);
       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;
+         return br;
        }
     }
   else
     {
        }
     }
   else
     {
-      System * line
-       = dynamic_cast<System*> (unsmob_grob (break_criterion));
+      System *line
+       = dynamic_cast<System *> (unsmob_grob (break_criterion));
       if (sc->get_system () != line)
       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)
       /* now: !sc || (sc && sc->get_system () == line) */
       if (!sc)
-       return SCM_UNDEFINED;
+       return 0;
 
       /* now: sc && sc->get_system () == line */
       if (!line)
 
       /* now: sc && sc->get_system () == line */
       if (!line)
-       return sc->self_scm();
+       return sc;
 
       /*
        We don't return SCM_UNDEFINED for
 
       /*
        We don't return SCM_UNDEFINED for
@@ -54,30 +60,17 @@ substitute_grob (Grob *sc)
 
        - 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.
-       */
-
-      /*
-       This was introduced in 1.3.49 as a measure to prevent
-       programming errors. It looks rather expensive (?).
-
-       TODO:
-               
-       benchmark , document when (what kind of programming
-       errors) this happens.
       */
       */
+
       if (sc->common_refpoint (line, X_AXIS)
          && sc->common_refpoint (line, Y_AXIS))
       if (sc->common_refpoint (line, X_AXIS)
          && sc->common_refpoint (line, Y_AXIS))
-       {
-         return sc->self_scm ();
-       }
-      return SCM_UNDEFINED;
+       return sc;
+      return 0;
     }
 
     }
 
-  return sc->self_scm();
+  return sc;
 }
 
 }
 
-
-
 /*
   Do break substitution in S, using CRITERION. Return new value.
   CRITERION is either a SMOB pointer to the desired line, or a number
 /*
   Do break substitution in S, using CRITERION. Return new value.
   CRITERION is either a SMOB pointer to the desired line, or a number
@@ -96,41 +89,42 @@ SCM
 do_break_substitution (SCM src)
 {
  again:
 do_break_substitution (SCM src)
 {
  again:
+
   if (unsmob_grob (src))
     {
   if (unsmob_grob (src))
     {
-      return substitute_grob (unsmob_grob (src));
+      Grob *new_ptr = substitute_grob (unsmob_grob (src));
+      return new_ptr ? new_ptr->self_scm () : SCM_UNDEFINED;
     }
     }
-  else if (gh_vector_p (src))
+  else if (scm_is_vector (src))
     {
     {
-      int  l = SCM_VECTOR_LENGTH (src);
-      SCM nv = scm_c_make_vector (l, SCM_UNDEFINED);
-
-      for (int i  =0 ; i< l ; i++)
+      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 (ly_pair_p (src)) 
+  else if (scm_is_pair (src))
     {
       /*
        UGH! breaks on circular lists.
       */
     {
       /*
        UGH! breaks on circular lists.
       */
-      SCM newcar = do_break_substitution (ly_car (src));
-      SCM oldcdr = ly_cdr (src);
-      
+      SCM newcar = do_break_substitution (scm_car (src));
+      SCM oldcdr = scm_cdr (src);
+
       if (newcar == SCM_UNDEFINED
       if (newcar == SCM_UNDEFINED
-         && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
+         && (scm_is_pair (oldcdr) || oldcdr == SCM_EOL))
        {
          /*
        {
          /*
-           This is tail-recursion, ie. 
-           
+           This is tail-recursion, ie.
+
            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.  */
            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;
+         src = oldcdr;
          goto again;
        }
 
          goto again;
        }
 
@@ -142,150 +136,149 @@ do_break_substitution (SCM src)
   return src;
 }
 
   return src;
 }
 
-
 /*
   Perform substitution on GROB_LIST using a constant amount of stack.
 /*
   Perform substitution on GROB_LIST using a constant amount of stack.
- */
-SCM
-substitute_grob_list (SCM grob_list)
+*/
+vector<Grob*> temporary_substition_array;
+void
+substitute_grob_array (Grob_array *grob_arr, Grob_array *new_arr)
 {
 {
-  SCM l = SCM_EOL;
-  SCM * tail = &l;
-
-  for (SCM s = grob_list; gh_pair_p (s); s =  gh_cdr (s))
+  vector<Grob*> &old_grobs (grob_arr->array_reference ());
+  vector<Grob*> *new_grobs (new_arr == grob_arr
+                              ? & temporary_substition_array
+                              : &new_arr->array_reference ());
+
+  new_grobs->resize (old_grobs.size ());
+  Grob **array = (Grob **) new_grobs->data ();
+  Grob **ptr = array;
+  for (vsize i = 0; i < old_grobs.size (); i++)
     {
     {
-      SCM n= substitute_grob (unsmob_grob (gh_car (s)));
-
-      if (n != SCM_UNDEFINED)
-       {
-         *tail = gh_cons (n, SCM_EOL);
-         tail = SCM_CDRLOC(*tail);
-       }
+      Grob *orig = old_grobs[i];
+      Grob *new_grob = substitute_grob (orig);
+      if (new_grob)
+       *ptr++ = new_grob;
     }
 
     }
 
-  return l;
+  new_grobs->resize (ptr - array);
+  if (new_arr == grob_arr)
+    new_arr->set_array (*new_grobs);
 }
 
 /*
   We don't do
 
   forall b in broken-childs:
 }
 
 /*
   We don't do
 
   forall b in broken-childs:
-     forall p in properties:
-        forall g in p (if grob-list):
-         g := substitute (g)
+  forall p in properties:
+  forall g in p (if grob-list):
+  g := substitute (g)
 
 
-  for spanners since this is O(SYSTEMCOUNT * GROBCOUNT), and SYSTEMCOUNT =
-  O(GROBCOUNT), we have a quadratic algorithm. --for a single spanner
+  for spanners since this is O (SYSTEMCOUNT * GROBCOUNT), and SYSTEMCOUNT =
+  O (GROBCOUNT), we have a quadratic algorithm. --for a single spanner
 
   This is problematic: with large (long) scores, the costs can be
   significant; especially all-elements in System, can become huge. For
   a typical 50 page score, it requires running through a 100k list 50
   times.
 
   This is problematic: with large (long) scores, the costs can be
   significant; especially all-elements in System, can become huge. For
   a typical 50 page score, it requires running through a 100k list 50
   times.
-  
+
   Instead:
 
   forall p in properties:
   Instead:
 
   forall p in properties:
-     (if grob list)  
+  (if grob list)
 
 
-     put  grob list in array,
+  put  grob list in array,
 
 
-     reorder array so spanners are separate -- O(grobcount)
-     
-     find first and last indexes of grobs on a specific system
+  reorder array so spanners are separate -- O (grobcount)
 
 
-     for items this is O(itemcount)
+  find first and last indexes of grobs on a specific system
 
 
-     for spanners this is O(sum-of spanner-system-ranges)
+  for items this is O (itemcount)
 
 
-     perform the substitution O(sum-of spanner-system-ranges)
+  for spanners this is O (sum-of spanner-system-ranges)
+
+  perform the substitution O (sum-of spanner-system-ranges)
 
 
   The complexity is harder to determine, but should be subquadratic;
 
   For the situation above, we run through the entire 100k list once,
   and also (more or less) once through the item part of the 100k (say
 
 
   The complexity is harder to determine, but should be subquadratic;
 
   For the situation above, we run through the entire 100k list once,
   and also (more or less) once through the item part of the 100k (say
-  98k elements) of the list. 
+  98k elements) of the list.
 
 
 
 
-These timings were measured without -O2.
+  These timings were measured without -O2.
 
 
-  lehre, before 28.98 seconds, after: 27.91 seconds, 3.5 %. 
+  lehre, before 28.98 seconds, after: 27.91 seconds, 3.5 %.
 
   coriolan, before 2:30, after:  1:59. Increase of 20%.
 
   moz-k498-p1, before 24.10, after: 19.790s, Increase of 18%
 
   coriolan, before 2:30, after:  1:59. Increase of 20%.
 
   moz-k498-p1, before 24.10, after: 19.790s, Increase of 18%
-
-  
 */
 
 */
 
-
 Slice
 Slice
-spanner_system_range (Spannersp)
+spanner_system_range (Spanner *sp)
 {
   Slice rv;
 {
   Slice rv;
-  
-  if (System*st = sp->get_system())
-    {
-      rv = Slice (st->rank_, st->rank_);
-    }
-  else 
+
+  if (System *st = sp->get_system ())
+    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_);
+      if (sp->broken_intos_.size ())
+       rv = Slice (sp->broken_intos_[0]->get_system ()->get_rank (),
+                   sp->broken_intos_.back ()->get_system ()->get_rank ());
     }
   return rv;
 }
 
 Slice
     }
   return rv;
 }
 
 Slice
-item_system_range (Itemit)
+item_system_range (Item *it)
 {
 {
-  if (System*st= it->get_system())
-    return Slice (st->rank_, st->rank_);
+  if (System *st = it->get_system ())
+    return Slice (st->get_rank (), st->get_rank ());
 
   Slice sr;
   Direction d = LEFT;
   do
     {
       Item *bi = it->find_prebroken_piece (d);
 
   Slice sr;
   Direction d = LEFT;
   do
     {
       Item *bi = it->find_prebroken_piece (d);
-      if (bi && bi->get_system())
-       sr.add_point (bi->get_system()->rank_);
+      if (bi && bi->get_system ())
+       sr.add_point (bi->get_system ()->get_rank ());
     }
     }
-  while (flip(&d)!=LEFT);
-  
+  while (flip (&d) != LEFT);
+
   return sr;
 }
 
 Slice
 grob_system_range (Grob *g)
 {
   return sr;
 }
 
 Slice
 grob_system_range (Grob *g)
 {
if (Spanner*s = dynamic_cast<Spanner*>(g))
-   return spanner_system_range (s);
else if (Item* it = dynamic_cast<Item*> (g))
-   return item_system_range (it);
- else
-   return Slice();
 if (Spanner *s = dynamic_cast<Spanner *> (g))
+    return spanner_system_range (s);
 else if (Item *it = dynamic_cast<Item *> (g))
+    return item_system_range (it);
 else
+    return Slice ();
 }
 
 }
 
-
-
 struct Substitution_entry
 {
 struct Substitution_entry
 {
-  Grob * grob_;
+  Grob *grob_;
+
+  /* Assumption: we have less than 32k paper columns. */
   short left_;
   short right_;
   short left_;
   short right_;
-  
-  void set (Grob*g, Slice sr)
+
+  void set (Grob *g, Slice sr)
   {
     grob_ = g;
     /*
       duh, don't support scores with more than 32000 systems.
     */
   {
     grob_ = g;
     /*
       duh, don't support scores with more than 32000 systems.
     */
-    if (sr.empty_b())
+    if (sr.is_empty ())
       {
        /*
          overflow if we don't treat this specially.
       {
        /*
          overflow if we don't treat this specially.
-        */
+       */
        left_ = 1;
        right_ = -1;
       }
        left_ = 1;
        right_ = -1;
       }
@@ -295,174 +288,146 @@ struct Substitution_entry
        right_ = sr[RIGHT];
       }
   }
        right_ = sr[RIGHT];
       }
   }
-  Substitution_entry()
+  Substitution_entry ()
   {
   {
-    grob_ =0;
+    grob_ = 0;
     left_ = right_ = -2;
   }
     left_ = right_ = -2;
   }
-  
-  int length () { return right_ - left_ ; }
+
+  int length () { return right_ - left_; }
   static int
   static int
-  item_compare (void const * a , void const * b)
+  item_compare (void const *a, void const *b)
   {
   {
-    return ((Substitution_entry*)a)->left_ -
-      ((Substitution_entry*)b)->left_;
+    return ((Substitution_entry *)a)->left_
+      - ((Substitution_entry *)b)->left_;
   }
   }
+
   static int
   static int
-  spanner_compare (void const * a , void const * b)
+  spanner_compare (void const *a, void const *b)
   {
   {
-    return ((Substitution_entry*)a)->length() -
-      ((Substitution_entry*)b)->length ();
+    return ((Substitution_entry *)a)->length ()
+      - ((Substitution_entry *)b)->length ();
   }
 };
   }
 };
-  
 
 
-    
 bool
 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 100 grobs.)
-   */
-  
-  if (len < 100)
+  if (grob_array->ordered ())
     return false;
 
     return false;
 
+  if (len < 15)
+    return false;
 
   /*
 
   /*
-    TODO : should not reallocate every time? 
-   */
-  static Substitution_entry * vec;
+    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;
 
   if (vec_room < len)
     {
   static int vec_room;
 
   if (vec_room < len)
     {
-      vec = (Substitution_entry*) scm_realloc (vec, sizeof (Substitution_entry) * len);
+      vec = (Substitution_entry *) realloc (vec, sizeof (Substitution_entry) * len);
       vec_room = len;
     }
       vec_room = len;
     }
-  
+
   Slice system_range = spanner_system_range (this);
 
   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 sp_index = len;
-  int it_index = 0;
-  for (SCM s = grob_list; gh_pair_p (s); s = gh_cdr (s))
+  int spanner_index = len;
+  int item_index = 0;
+
+  for (vsize i = 0; i < grob_array->size (); i++)
     {
     {
-      Grob * g = unsmob_grob (gh_car(s));
+      Grob *g = grob_array->grob (i);
 
       Slice sr = grob_system_range (g);
       sr.intersect (system_range);
 
       int idx = 0;
 
       Slice sr = grob_system_range (g);
       sr.intersect (system_range);
 
       int idx = 0;
-      if (dynamic_cast<Spanner*>(g))
-       {
-         idx =--sp_index;
-       }
-      else if (dynamic_cast<Item*> (g))
-       {
-         idx = it_index++;
-       }
+      if (dynamic_cast<Spanner *> (g))
+       idx = --spanner_index;
+      else if (dynamic_cast<Item *> (g))
+       idx = item_index++;
 
       vec[idx].set (g, sr);
     }
 
 
       vec[idx].set (g, sr);
     }
 
-  qsort (vec, it_index,
+  qsort (vec, item_index,
         sizeof (Substitution_entry), &Substitution_entry::item_compare);
 
         sizeof (Substitution_entry), &Substitution_entry::item_compare);
 
- Array<Slice> *arrs[] = {
-       &it_indices, &sp_indices
- };
-        
- for (int i = 0; i < it_index ;i++)
-   {
-     for (int j = vec[i].left_; j <= vec[i].right_; j++)
-       {
-        it_indices[j - system_range[LEFT]].add_point (i);
-       }
-   }
-
-#if 0
-  qsort (vec + sp_index, len - sp_index,
-        sizeof (Substitution_entry), &Substitution_entry::spanner_compare);
- /*
-   This is a waste of time -- the staff-spanners screw up the
-   ordering, since they go across the entire score.
- */
- for (int i = sp_index; i < len ;i++)
-   {
-     
-     for (int j = vec[i].left_; j <= vec[i].right_; j++)
-       {
-        sp_indices[j - system_range[LEFT]].add_point (i);
-       }
-   }
-#else
- for (int i = sp_indices.size(); i--;)
-   sp_indices[i]= Slice (sp_index, len-1);
-#endif
+  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));
+    }
 
 
-  assert (it_index <= sp_index);
+  vector<Slice> *arrs[]
+    = {
+    &item_indices, &spanner_indices
+  };
 
 
-  assert (broken_intos_.size () == system_range.length () + 1); 
-  for (int i = 0; i < broken_intos_.size(); i++)
+  for (int i = 0; i < item_index;i++)
     {
     {
-      Grob * sc = broken_intos_[i];
-      System * l = sc->get_system ();
-      set_break_subsititution (l ? l->self_scm(): SCM_UNDEFINED);
+      for (int j = vec[i].left_; j <= vec[i].right_; j++)
+       item_indices[j - system_range[LEFT]].add_point (i);
+    }
+
+  /*
+    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 (vsize i = spanner_indices.size (); i--;)
+    spanner_indices[i] = Slice (spanner_index, len - 1);
+
+  assert (item_index <= spanner_index);
+
+  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;
+      SCM newval = sc->internal_get_object (sym);
+      if (!unsmob_grob_array (newval))
+       {
+         newval = Grob_array::make_array ();
+         sc->set_object (sym, newval);
+       }
 
 
-     for (int k = 0; k < 2;k++)
+      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++)
          {
        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);
-             }
-
+           Grob *substituted = substitute_grob (vec[j].grob_);
+           if (substituted)
+             new_array->add (substituted);
          }
          }
-             
+
 #ifdef PARANOIA
 #ifdef PARANOIA
-     
       printf ("%d (%d), sp %d (%d)\n",
       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);
+       SCM l1 = substitute_grob_list (grob_list);
        assert (scm_ilength (l1) == scm_ilength (newval));
       }
 #endif
        assert (scm_ilength (l1) == scm_ilength (newval));
       }
 #endif
-
-      sc->mutable_property_alist_ = scm_acons (sym, newval,
-                                              sc->mutable_property_alist_);
     }
 
   return true;
 }
 
     }
 
   return true;
 }
 
-
-SCM grob_list_p; 
-
 /*
   Although the substitution can be written as
 
 /*
   Although the substitution can be written as
 
@@ -470,63 +435,90 @@ SCM grob_list_p;
 
   we have a special function here: we want to invoke a special
   function for lists of grobs. These can be very long for large
 
   we have a special function here: we want to invoke a special
   function for lists of grobs. These can be very long for large
-  orchestral scores (eg. 1M elements). do_break_substitution() can
+  orchestral scores (eg. 1M elements). do_break_substitution () can
   recurse many levels, taking lots of stack space.
 
   This becomes a problem if lily is linked against guile with
   pthreads. pthreads impose small limits on the stack size.
   recurse many levels, taking lots of stack space.
 
   This becomes a problem if lily is linked against guile with
   pthreads. pthreads impose small limits on the stack size.
- */
+*/
 SCM
 SCM
-substitute_mutable_property_alist (SCM alist)
+substitute_object_alist (SCM alist, SCM dest)
 {
 {
-  if (!grob_list_p)
-    grob_list_p = scm_c_eval_string ("grob-list?");
-
   SCM l = SCM_EOL;
   SCM *tail = &l;
   SCM l = SCM_EOL;
   SCM *tail = &l;
-  for (SCM s = alist; gh_pair_p (s); s = gh_cdr (s))
+  for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
     {
     {
-      SCM sym = gh_caar(s);
-      SCM val = gh_cdar(s);
-      SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
+      SCM sym = scm_caar (s);
+      SCM val = scm_cdar (s);
+
+      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);
 
 
-      if (type == grob_list_p)
-       val = substitute_grob_list (val);
+         substitute_grob_array (orig, new_arr);
+         val = newval;
+       }
       else
        val = do_break_substitution (val);
 
       else
        val = do_break_substitution (val);
 
-      *tail = gh_cons (gh_cons (sym, val), SCM_EOL);
-      tail = SCM_CDRLOC (*tail);
+      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);
+       }
     }
     }
-
   return l;
 }
 
   return l;
 }
 
-
 void
 Spanner::substitute_one_mutable_property (SCM sym,
                                          SCM val)
 {
 void
 Spanner::substitute_one_mutable_property (SCM sym,
                                          SCM val)
 {
-  SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
-  Spanner*s = this;
-  
+  Spanner *s = this;
+
   bool fast_done = false;
   bool fast_done = false;
-  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++)
+  if (!fast_done)
+    for (vsize i = 0; i < s->broken_intos_.size (); i++)
       {
       {
-       Grob * sc = s->broken_intos_[i];
-       System * l = sc->get_system ();
+       Grob *sc = s->broken_intos_[i];
+       System *l = sc->get_system ();
        set_break_subsititution (l ? l->self_scm () : SCM_UNDEFINED);
 
        set_break_subsititution (l ? l->self_scm () : SCM_UNDEFINED);
 
-       SCM newval = (type == grob_list_p)
-         ? substitute_grob_list (val)
-         : do_break_substitution(val);
-
-       sc->mutable_property_alist_ = scm_cons (scm_cons (sym, newval),
-                                               sc->mutable_property_alist_);
+       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);
+             }
+           substitute_grob_array (grob_array, unsmob_grob_array (newval));
+         }
+       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_);
+}