]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/nested-property.cc
Imported Upstream version 2.14.2
[lilypond.git] / lily / nested-property.cc
index c1174b174d5a1bb156e59373b351e539090c7266..db897d3fd847d60cce195e926889eced04c9ec23 100644 (file)
@@ -1,10 +1,12 @@
 #include "context.hh"
 #include "grob.hh"
 
+
+/*
+  Drop symbol from the list alist..alist_end.
+ */
 SCM
-evict_from_alist (SCM symbol,
-                 SCM alist,
-                 SCM alist_end)
+evict_from_alist (SCM symbol, SCM alist, SCM alist_end)
 {
   SCM new_alist = SCM_EOL;
   SCM *tail = &new_alist;
@@ -29,7 +31,7 @@ evict_from_alist (SCM symbol,
 /*
   PROP_PATH should be big-to-small ordering
  */
-SCM 
+SCM
 nested_property_alist (SCM alist, SCM prop_path, SCM value)
 {
   SCM new_value = SCM_BOOL_F;
@@ -40,28 +42,90 @@ nested_property_alist (SCM alist, SCM prop_path, SCM value)
     }
   else
     {
-       new_value = value;
+      new_value = value;
     }
 
   return scm_acons (scm_car (prop_path), new_value, alist);
 }
 
-SCM 
+/*
+  Recursively purge alist of prop_path:
+
+  revert ((sym, val) : L, [sym]) = L
+  revert ((sym, val) : L, sym : props) =
+    (sym, revert (val, rest-props)) ++ L
+  revert ((sym, val) : L, p ++ rest-props) =
+    (sym, val) : revert (L, p ++ rest-props)
+
+ */
+SCM
 nested_property_revert_alist (SCM alist, SCM prop_path)
 {
-  SCM new_sub_alist = SCM_EOL;
-  SCM sym = scm_car (prop_path);
-  if (scm_is_pair (scm_cdr (prop_path)))
-    {
-      SCM sub_alist = ly_assoc_get (sym, alist, SCM_EOL);
-      new_sub_alist = nested_property_revert_alist (sub_alist, scm_cdr (prop_path));
-    }
-  else
+  int copy_count = 0;
+  bool drop = false;
+  assert(scm_is_pair (prop_path));
+
+  SCM wanted_sym = scm_car (prop_path);
+
+  SCM new_list = SCM_EOL;
+  SCM *tail = &new_list;
+  for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
     {
-      new_sub_alist = evict_from_alist (sym, alist, SCM_EOL);
+      SCM sub_sym = scm_caar (s);
+      SCM old_val = scm_cdar (s);
+      drop = false;
+
+      if (sub_sym == wanted_sym)
+       {
+         if (scm_is_pair (scm_cdr (prop_path)))
+           {
+             SCM new_val = nested_property_revert_alist (old_val, scm_cdr (prop_path));
+
+             /* nothing changed: drop newly constructed list. */
+             if (old_val == new_val)
+               return alist;
+
+             *tail = scm_acons (sub_sym, new_val, SCM_EOL);
+             tail = SCM_CDRLOC(*tail);
+              *tail = scm_cdr (s);
+              return new_list;
+           }
+         else
+           {
+              /* old value should be dropped only if we have another copy of it in the alist */
+              copy_count++;
+              /*
+                Only drop the first instance found.
+                the overridden value is always the first
+                if this was the only copy, we will return
+                the original list anyways so it is not relevant
+                if we drop this pair
+              */
+              if (copy_count == 1)
+                drop = true;
+           }
+         /* we now iterate over every item */
+       }
+      /*
+        Make a new list with every item
+        except for the eventual dropped one
+      */
+      if (!drop)
+        {
+          *tail = scm_acons (sub_sym, old_val, SCM_EOL);
+          tail = SCM_CDRLOC (*tail);
+        }
     }
 
-  return scm_acons (sym, new_sub_alist, alist);
+  /*
+    If we find more than one copy of the property
+    push the new list, else it means we are trying to
+    revert the original value
+  */
+  if (copy_count > 1)
+    return new_list;
+  else
+    return alist;
 }
 
 
@@ -71,8 +135,7 @@ set_nested_property (Grob *me, SCM big_to_small, SCM value)
   SCM alist = me->get_property (scm_car (big_to_small));
 
   alist = nested_property_alist (alist, scm_cdr (big_to_small), value);
-  
-  me->set_property (scm_car (big_to_small),
-                   alist);
+
+  me->set_property (scm_car (big_to_small), alist);
 }