4 // scm_reverse_x without the checks
6 fast_reverse_x (SCM lst, SCM tail)
8 while (!scm_is_null (lst))
10 SCM n = scm_cdr (lst);
11 scm_set_cdr_x (lst, tail);
18 // copy the spine of lst not including tail, appending newtail
21 partial_list_copy (SCM lst, SCM tail, SCM newtail)
24 for (; !scm_is_eq (lst, tail); lst = scm_cdr (lst))
25 p = scm_cons (scm_car (lst), p);
26 return fast_reverse_x (p, newtail);
30 assq_tail (SCM key, SCM alist, SCM based_on = SCM_EOL)
32 for (SCM p = alist; !scm_is_eq (p, based_on); p = scm_cdr (p))
34 if (scm_is_eq (scm_caar (p), key))
41 assoc_tail (SCM key, SCM alist, SCM based_on = SCM_EOL)
43 for (SCM p = alist; !scm_is_eq (p, based_on); p = scm_cdr (p))
45 if (ly_is_equal (scm_caar (p), key))
51 // Like assq, but removes the found element destructively
52 SCM assq_pop_x (SCM key, SCM *alist)
54 for (SCM p = *alist; scm_is_pair (p); p = *(alist = SCM_CDRLOC (p)))
56 if (scm_is_eq (scm_caar (p), key))
66 Drop key from the list alist..alist_end.
69 evict_from_alist (SCM key, SCM alist, SCM alist_end)
71 // shortcircuit to an eq-using assoc_tail variant when key is a symbol
73 SCM p = scm_is_symbol (key) ? assq_tail (key, alist, alist_end)
74 : assoc_tail (key, alist, alist_end);
76 return partial_list_copy (alist, p, scm_cdr (p));
80 // This is the same as
81 // nested_property_alist (SCM_EOL, prop_path, value) but faster
83 nested_create_alist (SCM prop_path, SCM value)
85 if (scm_is_null (prop_path))
87 return scm_acons (scm_car (prop_path),
88 nested_create_alist (scm_cdr (prop_path), value),
93 PROP_PATH should be big-to-small ordering
96 // Take the given alist and replace the given nested property with the
97 // given value. Multiple overrides of the same property path are not
98 // coalesced for efficiency reasons: they are considered rare enough
99 // to not be worth the cost of detecting them. When sublists are
100 // modified, however, we remove the original sublist and copy the
101 // spine before it. The cost for finding the sublist has already been
104 // A typical use case for this routine is applying (possibly nested)
105 // tweaks to a grob property list.
108 nested_property_alist (SCM alist, SCM prop_path, SCM value)
110 // replacement moves to the front.
111 SCM key = scm_car (prop_path);
112 SCM rest = scm_cdr (prop_path);
113 if (scm_is_pair (rest))
115 SCM where = assq_tail (key, alist);
116 if (scm_is_false (where))
117 return scm_acons (key, nested_create_alist (rest, value), alist);
118 return scm_acons (key, nested_property_alist (scm_cdar (where),
121 partial_list_copy (alist, where, scm_cdr (where)));
123 // Outcommented code would coalesce multiple overrides of the same
126 SCM where = assq_tail (alist, key);
127 if (scm_is_true (where))
128 return scm_acons (key, value,
129 partial_list_copy (alist, where, scm_cdr (where)));
131 return scm_acons (key, value, alist);
135 set_nested_property (Grob *me, SCM big_to_small, SCM value)
137 SCM alist = me->get_property (scm_car (big_to_small));
139 alist = nested_property_alist (alist, scm_cdr (big_to_small), value);
141 me->set_property (scm_car (big_to_small), alist);
144 // This converts an alist with nested overrides in it to a proper
145 // alist. The number of nested overrides is known in advance,
146 // everything up to the last nested override is copied, the tail is
150 nalist_to_alist (SCM nalist, int nested)
154 SCM copied = SCM_EOL;
155 SCM partials = SCM_EOL;
156 // partials is a alist of partial overrides
159 SCM elt = scm_car (nalist);
160 nalist = scm_cdr (nalist);
161 SCM key = scm_car (elt);
162 if (scm_is_pair (key))
163 // nested override: record for key in partial
165 SCM pair = scm_sloppy_assq (scm_car (key), partials);
166 if (scm_is_false (pair))
167 partials = scm_acons (scm_car (key), scm_list_1 (elt),
170 scm_set_cdr_x (pair, scm_cons (elt, scm_cdr (pair)));
175 // plain override: apply any known corresponding partials
177 SCM pair = assq_pop_x (key, &partials);
178 if (scm_is_true (pair))
180 SCM value = scm_cdr (elt);
181 for (SCM pp = scm_cdr (pair); scm_is_pair (pp); pp = scm_cdr (pp))
182 value = nested_property_alist (value, scm_cdaar (pp), scm_cdar (pp));
183 copied = scm_acons (key, value, copied);
186 copied = scm_cons (elt, copied);
189 // Now need to work off the remaining partials. All of them are
190 // unique, so we can push them to `copied' after resolving without
191 // losing information.
193 for (;scm_is_pair (partials); partials = scm_cdr (partials))
195 SCM pair = scm_car (partials);
196 SCM key = scm_car (pair);
197 SCM elt = scm_sloppy_assq (key, nalist);
199 if (scm_is_true (elt))
200 value = scm_cdr (elt);
202 for (SCM pp = scm_cdr (pair); scm_is_pair (pp); pp = scm_cdr (pp))
203 value = nested_property_alist (value, scm_cdaar (pp), scm_cdar (pp));
205 copied = scm_acons (key, value, copied);
207 return fast_reverse_x (copied, nalist);
211 // Alternative approach: don't unfold those partial overrides while
212 // they are part of contexts but instead use a special accessor for
213 // subproperties in the grob. Not used or tested for now.
216 nassq_ref (SCM key, SCM nalist, SCM fallback)
218 SCM partials = SCM_EOL;
219 // partials is list of partial overrides for the given property
220 for (SCM p = nalist; scm_is_pair (p); p = scm_cdr (p))
222 SCM elt = scm_car (p);
223 SCM pkey = scm_car (elt);
224 if (scm_is_pair (pkey))
226 if (scm_is_eq (scm_car (pkey), key))
227 partials = scm_cons (elt, partials);
229 else if (scm_is_eq (pkey, key))
231 SCM value = scm_cdr (elt);
232 for (; scm_is_pair (partials); partials = scm_cdr (partials))
234 value = nested_property_alist (value, scm_cdaar (partials),
235 scm_cdar (partials));
240 if (scm_is_pair (partials))
242 // Bit of a quandary here: we have only subproperty overrides
243 // but no main property. Could be a programming error, but we
244 // instead override an empty list.
245 SCM value = nested_create_alist (scm_cdaar (partials), scm_cdar (partials));
246 partials = scm_cdr (partials);
247 for (; scm_is_pair (partials); partials = scm_cdr (partials))
248 value = nested_property_alist (value, scm_cdaar (partials),
249 scm_cdar (partials));
252 return SCM_UNBNDP (fallback) ? SCM_EOL : fallback;
255 // Also needed for this approach to make sense: an accessor for true
258 nassq_nested_ref (SCM key, SCM subpath, SCM nalist, SCM fallback);