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 assv_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 (scm_is_true (scm_eqv_p (scm_caar (p), key)))
52 assoc_tail (SCM key, SCM alist, SCM based_on = SCM_EOL)
55 return assq_tail (key, alist, based_on);
56 if (scm_is_number (key) || scm_is_true (scm_char_p (key)))
57 return assv_tail (key, alist, based_on);
58 for (SCM p = alist; !scm_is_eq (p, based_on); p = scm_cdr (p))
60 if (ly_is_equal (scm_caar (p), key))
66 // Like assq, but removes the found element destructively
67 SCM assq_pop_x (SCM key, SCM *alist)
69 for (SCM p = *alist; scm_is_pair (p); p = *(alist = SCM_CDRLOC (p)))
71 if (scm_is_eq (scm_caar (p), key))
81 Drop key from the list alist..alist_end.
84 evict_from_alist (SCM key, SCM alist, SCM alist_end)
86 SCM p = assoc_tail (key, alist, alist_end);
89 return partial_list_copy (alist, p, scm_cdr (p));
93 // This is the same as
94 // nested_property_alist (SCM_EOL, prop_path, value) but faster
96 nested_create_alist (SCM prop_path, SCM value)
98 if (scm_is_null (prop_path))
100 return scm_acons (scm_car (prop_path),
101 nested_create_alist (scm_cdr (prop_path), value),
106 PROP_PATH should be big-to-small ordering
109 // Take the given alist and replace the given nested property with the
110 // given value. Multiple overrides of the same property path are not
111 // coalesced for efficiency reasons: they are considered rare enough
112 // to not be worth the cost of detecting them. When sublists are
113 // modified, however, we remove the original sublist and copy the
114 // spine before it. The cost for finding the sublist has already been
117 // A typical use case for this routine is applying (possibly nested)
118 // tweaks to a grob property list.
121 nested_property_alist (SCM alist, SCM prop_path, SCM value)
123 // replacement moves to the front.
124 SCM key = scm_car (prop_path);
125 SCM rest = scm_cdr (prop_path);
126 if (scm_is_pair (rest))
128 SCM where = assq_tail (key, alist);
129 if (scm_is_false (where))
130 return scm_acons (key, nested_create_alist (rest, value), alist);
131 return scm_acons (key, nested_property_alist (scm_cdar (where),
134 partial_list_copy (alist, where, scm_cdr (where)));
136 // Outcommented code would coalesce multiple overrides of the same
139 SCM where = assq_tail (alist, key);
140 if (scm_is_true (where))
141 return scm_acons (key, value,
142 partial_list_copy (alist, where, scm_cdr (where)));
144 return scm_acons (key, value, alist);
148 nested_property (SCM alist, SCM prop_path, SCM fallback)
150 for (; scm_is_pair (prop_path); prop_path = scm_cdr (prop_path))
152 SCM tail = assoc_tail (scm_car (prop_path), alist);
153 if (scm_is_false (tail))
155 alist = scm_cdar (tail);
161 set_nested_property (Grob *me, SCM big_to_small, SCM value)
163 SCM alist = me->get_property (scm_car (big_to_small));
165 alist = nested_property_alist (alist, scm_cdr (big_to_small), value);
167 me->set_property (scm_car (big_to_small), alist);
170 // This converts an alist with nested overrides in it to a proper
171 // alist. The number of nested overrides is known in advance,
172 // everything up to the last nested override is copied, the tail is
176 nalist_to_alist (SCM nalist, int nested)
180 SCM copied = SCM_EOL;
181 SCM partials = SCM_EOL;
182 // partials is a alist of partial overrides
185 SCM elt = scm_car (nalist);
186 nalist = scm_cdr (nalist);
187 SCM key = scm_car (elt);
188 if (!scm_is_symbol (key))
190 if (scm_is_bool (key))
192 if (scm_is_false (key))
197 if (scm_is_pair (key))
198 // nested override: record for key in partial
200 SCM pair = scm_sloppy_assq (scm_car (key), partials);
201 if (scm_is_false (pair))
202 partials = scm_acons (scm_car (key), scm_list_1 (elt),
205 scm_set_cdr_x (pair, scm_cons (elt, scm_cdr (pair)));
209 // plain override: apply any known corresponding partials
210 SCM pair = assq_pop_x (key, &partials);
211 if (scm_is_true (pair))
213 SCM value = scm_cdr (elt);
214 for (SCM pp = scm_cdr (pair); scm_is_pair (pp); pp = scm_cdr (pp))
215 value = nested_property_alist (value, scm_cdaar (pp), scm_cdar (pp));
216 copied = scm_acons (key, value, copied);
219 copied = scm_cons (elt, copied);
221 // Now need to work off the remaining partials. All of them are
222 // unique, so we can push them to `copied' after resolving without
223 // losing information.
225 for (;scm_is_pair (partials); partials = scm_cdr (partials))
227 SCM pair = scm_car (partials);
228 SCM key = scm_car (pair);
229 SCM elt = scm_sloppy_assq (key, nalist);
231 if (scm_is_true (elt))
232 value = scm_cdr (elt);
234 for (SCM pp = scm_cdr (pair); scm_is_pair (pp); pp = scm_cdr (pp))
235 value = nested_property_alist (value, scm_cdaar (pp), scm_cdar (pp));
237 copied = scm_acons (key, value, copied);
239 return fast_reverse_x (copied, nalist);
243 // Alternative approach: don't unfold those partial overrides while
244 // they are part of contexts but instead use a special accessor for
245 // subproperties in the grob. Not used or tested for now.
248 nassq_ref (SCM key, SCM nalist, SCM fallback)
250 SCM partials = SCM_EOL;
251 // partials is list of partial overrides for the given property
252 for (SCM p = nalist; scm_is_pair (p); p = scm_cdr (p))
254 SCM elt = scm_car (p);
255 SCM pkey = scm_car (elt);
256 if (scm_is_pair (pkey))
258 if (scm_is_eq (scm_car (pkey), key))
259 partials = scm_cons (elt, partials);
261 else if (scm_is_eq (pkey, key))
263 SCM value = scm_cdr (elt);
264 for (; scm_is_pair (partials); partials = scm_cdr (partials))
266 value = nested_property_alist (value, scm_cdaar (partials),
267 scm_cdar (partials));
272 if (scm_is_pair (partials))
274 // Bit of a quandary here: we have only subproperty overrides
275 // but no main property. Could be a programming error, but we
276 // instead override an empty list.
277 SCM value = nested_create_alist (scm_cdaar (partials), scm_cdar (partials));
278 partials = scm_cdr (partials);
279 for (; scm_is_pair (partials); partials = scm_cdr (partials))
280 value = nested_property_alist (value, scm_cdaar (partials),
281 scm_cdar (partials));
284 return SCM_UNBNDP (fallback) ? SCM_EOL : fallback;
287 // Also needed for this approach to make sense: an accessor for true
290 nassq_nested_ref (SCM key, SCM subpath, SCM nalist, SCM fallback);