]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/break-substitution.cc
* scripts/abc2ly.py (dump_slyrics): add ord().
[lilypond.git] / lily / break-substitution.cc
index fe3b1ea182cbbdc17c37604bb0d8cbbd2e530dc0..d97bdb5a51da0778bf11c840ce6b992fc6e13899 100644 (file)
@@ -1,10 +1,8 @@
-#include <stdio.h>
-#include <stdlib.h>
+#include <cstdio>
+#include <cstdlib>
 
-#include  "grob.hh"
 #include "item.hh"
-#include  "spanner.hh"
-#include  "system.hh"
+#include "system.hh"
 
 static SCM break_criterion; 
 void
@@ -19,7 +17,7 @@ set_break_subsititution (SCM criterion)
 SCM
 substitute_grob (Grob *sc)
 {
-  if (SCM_INUMP (break_criterion))
+  if (scm_is_integer (break_criterion))
     {
       Item * i = dynamic_cast<Item*> (sc);
       Direction d = to_dir (break_criterion);
@@ -45,7 +43,7 @@ substitute_grob (Grob *sc)
 
       /* now: sc && sc->get_system () == line */
       if (!line)
-       return sc->self_scm();
+       return sc->self_scm ();
 
       /*
        We don't return SCM_UNDEFINED for
@@ -57,15 +55,6 @@ substitute_grob (Grob *sc)
        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))
        {
@@ -74,7 +63,7 @@ substitute_grob (Grob *sc)
       return SCM_UNDEFINED;
     }
 
-  return sc->self_scm();
+  return sc->self_scm ();
 }
 
 
@@ -99,30 +88,28 @@ do_break_substitution (SCM src)
  again:
  
   if (unsmob_grob (src))
+    return substitute_grob (unsmob_grob (src));
+  else if (ly_c_vector_p (src))
     {
-      return substitute_grob (unsmob_grob (src));
-    }
-  else if (gh_vector_p (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_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_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.
       */
-      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
-         && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
+         && (scm_is_pair (oldcdr) || oldcdr == SCM_EOL))
        {
          /*
            This is tail-recursion, ie. 
@@ -153,14 +140,14 @@ substitute_grob_list (SCM grob_list)
   SCM l = SCM_EOL;
   SCM * tail = &l;
 
-  for (SCM s = grob_list; gh_pair_p (s); s =  gh_cdr (s))
+  for (SCM s = grob_list; scm_is_pair (s); s =  scm_cdr (s))
     {
-      SCM n= substitute_grob (unsmob_grob (gh_car (s)));
+      SCM n= substitute_grob (unsmob_grob (scm_car (s)));
 
       if (n != SCM_UNDEFINED)
        {
-         *tail = gh_cons (n, SCM_EOL);
-         tail = SCM_CDRLOC(*tail);
+         *tail = scm_cons (n, SCM_EOL);
+         tail = SCM_CDRLOC (*tail);
        }
     }
 
@@ -175,8 +162,8 @@ substitute_grob_list (SCM grob_list)
         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
@@ -190,15 +177,15 @@ substitute_grob_list (SCM grob_list)
 
      put  grob list in array,
 
-     reorder array so spanners are separate -- O(grobcount)
+     reorder array so spanners are separate -- O (grobcount)
      
      find first and last indexes of grobs on a specific system
 
-     for items this is O(itemcount)
+     for items this is O (itemcount)
 
-     for spanners this is 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)
+     perform the substitution O (sum-of spanner-system-ranges)
 
 
   The complexity is harder to determine, but should be subquadratic;
@@ -225,15 +212,15 @@ spanner_system_range (Spanner* sp)
 {
   Slice rv;
   
-  if (System*st = sp->get_system())
+  if (System*st = sp->get_system ())
     {
       rv = Slice (st->rank_, st->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 ()->rank_,
+                   sp->broken_intos_.top ()->get_system ()->rank_);
     }
   return rv;
 }
@@ -241,7 +228,7 @@ spanner_system_range (Spanner* sp)
 Slice
 item_system_range (Item* it)
 {
-  if (System*st= it->get_system())
+  if (System*st= it->get_system ())
     return Slice (st->rank_, st->rank_);
 
   Slice sr;
@@ -249,10 +236,10 @@ item_system_range (Item* it)
   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 ()->rank_);
     }
-  while (flip(&d)!=LEFT);
+  while (flip (&d)!=LEFT);
   
   return sr;
 }
@@ -265,7 +252,7 @@ grob_system_range (Grob *g)
  else if (Item* it = dynamic_cast<Item*> (g))
    return item_system_range (it);
  else
-   return Slice();
+   return Slice ();
 }
 
 
@@ -282,7 +269,7 @@ struct Substitution_entry
     /*
       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.
@@ -296,7 +283,7 @@ struct Substitution_entry
        right_ = sr[RIGHT];
       }
   }
-  Substitution_entry()
+  Substitution_entry ()
   {
     grob_ =0;
     left_ = right_ = -2;
@@ -313,7 +300,7 @@ struct Substitution_entry
   static int
   spanner_compare (void const * a , void const * b)
   {
-    return ((Substitution_entry*)a)->length() -
+    return ((Substitution_entry*)a)->length () -
       ((Substitution_entry*)b)->length ();
   }
 };
@@ -361,9 +348,9 @@ Spanner::fast_fubstitute_grob_list (SCM sym,
   
   int sp_index = len;
   int it_index = 0;
-  for (SCM s = grob_list; gh_pair_p (s); s = gh_cdr (s))
+  for (SCM s = grob_list; scm_is_pair (s); s = scm_cdr (s))
     {
-      Grob * g = unsmob_grob (gh_car(s));
+      Grob * g = unsmob_grob (scm_car (s));
 
       Slice sr = grob_system_range (g);
       sr.intersect (system_range);
@@ -396,35 +383,23 @@ Spanner::fast_fubstitute_grob_list (SCM sym,
        }
    }
 
-#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
+   sorting vec[sp_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_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--;)
+ for (int i = sp_indices.size (); i--;)
    sp_indices[i]= Slice (sp_index, len-1);
-#endif
 
  
   assert (it_index <= sp_index);
 
   assert (broken_intos_.size () == system_range.length () + 1); 
-  for (int i = 0; i < broken_intos_.size(); i++)
+  for (int 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);
+      set_break_subsititution (l ? l->self_scm (): SCM_UNDEFINED);
 
       SCM newval = SCM_EOL;
       SCM * tail = &newval;
@@ -437,7 +412,7 @@ Spanner::fast_fubstitute_grob_list (SCM sym,
              {
                *tail = scm_cons (subs, SCM_EOL);
                
-               tail = SCM_CDRLOC(*tail);
+               tail = SCM_CDRLOC (*tail);
              }
 
          }
@@ -446,7 +421,7 @@ Spanner::fast_fubstitute_grob_list (SCM sym,
      
       printf ("%d (%d), sp %d (%d)\n",
              it_indices [i].length (), it_index,
-             sp_indices[i].length() , len -sp_index);
+             sp_indices[i].length () , len -sp_index);
              
       {
        SCM l1 =substitute_grob_list (grob_list);
@@ -454,6 +429,14 @@ Spanner::fast_fubstitute_grob_list (SCM sym,
       }
 #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_);
     }
@@ -462,8 +445,6 @@ Spanner::fast_fubstitute_grob_list (SCM sym,
 }
 
 
-SCM grob_list_p; 
-
 /*
   Although the substitution can be written as
 
@@ -471,7 +452,7 @@ 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
-  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
@@ -480,15 +461,14 @@ SCM grob_list_p;
 SCM
 substitute_mutable_property_alist (SCM alist)
 {
-  if (!grob_list_p)
-    grob_list_p = scm_c_eval_string ("grob-list?");
+  SCM grob_list_p = ly_scheme_function ("grob-list?");
 
   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 sym = scm_caar (s);
+      SCM val = scm_cdar (s);
       SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
 
       if (type == grob_list_p)
@@ -496,10 +476,17 @@ substitute_mutable_property_alist (SCM alist)
       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;
 }
 
@@ -512,6 +499,7 @@ Spanner::substitute_one_mutable_property (SCM sym,
   Spanner*s = this;
   
   bool fast_done = false;
+  SCM grob_list_p = ly_scheme_function ("grob-list?");
   if (type == grob_list_p)
     fast_done = s->fast_fubstitute_grob_list (sym, val);
 
@@ -524,8 +512,25 @@ Spanner::substitute_one_mutable_property (SCM sym,
 
        SCM newval = (type == grob_list_p)
          ? substitute_grob_list (val)
-         : do_break_substitution(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_);
       }