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)
54 for (SCM p = alist; !scm_is_eq (p, based_on); p = scm_cdr (p))
56 if (ly_is_equal (scm_caar (p), key))
62 // Like assq, but removes the found element destructively
63 SCM assq_pop_x (SCM key, SCM *alist)
65 for (SCM p = *alist; scm_is_pair (p); p = *(alist = SCM_CDRLOC (p)))
67 if (scm_is_eq (scm_caar (p), key))
77 Drop key from the list alist..alist_end.
80 evict_from_alist (SCM key, SCM alist, SCM alist_end)
82 // shortcircuit to an eq-using assoc_tail variant when key is a symbol
84 SCM p = scm_is_symbol (key) ? assq_tail (key, alist, alist_end)
85 : assoc_tail (key, alist, alist_end);
87 return partial_list_copy (alist, p, scm_cdr (p));
91 // This is the same as
92 // nested_property_alist (SCM_EOL, prop_path, value) but faster
94 nested_create_alist (SCM prop_path, SCM value)
96 if (scm_is_null (prop_path))
98 return scm_acons (scm_car (prop_path),
99 nested_create_alist (scm_cdr (prop_path), value),
104 PROP_PATH should be big-to-small ordering
107 // Take the given alist and replace the given nested property with the
108 // given value. Multiple overrides of the same property path are not
109 // coalesced for efficiency reasons: they are considered rare enough
110 // to not be worth the cost of detecting them. When sublists are
111 // modified, however, we remove the original sublist and copy the
112 // spine before it. The cost for finding the sublist has already been
115 // A typical use case for this routine is applying (possibly nested)
116 // tweaks to a grob property list.
119 nested_property_alist (SCM alist, SCM prop_path, SCM value)
121 // replacement moves to the front.
122 SCM key = scm_car (prop_path);
123 SCM rest = scm_cdr (prop_path);
124 if (scm_is_pair (rest))
126 SCM where = assq_tail (key, alist);
127 if (scm_is_false (where))
128 return scm_acons (key, nested_create_alist (rest, value), alist);
129 return scm_acons (key, nested_property_alist (scm_cdar (where),
132 partial_list_copy (alist, where, scm_cdr (where)));
134 // Outcommented code would coalesce multiple overrides of the same
137 SCM where = assq_tail (alist, key);
138 if (scm_is_true (where))
139 return scm_acons (key, value,
140 partial_list_copy (alist, where, scm_cdr (where)));
142 return scm_acons (key, value, alist);
146 nested_property (SCM alist, SCM prop_path, SCM fallback)
148 for (; scm_is_pair (prop_path); prop_path = scm_cdr (prop_path))
150 SCM tail = assoc_tail (scm_car (prop_path), alist);
151 if (scm_is_false (tail))
153 alist = scm_cdar (tail);
159 set_nested_property (Grob *me, SCM big_to_small, SCM value)
161 SCM alist = me->get_property (scm_car (big_to_small));
163 alist = nested_property_alist (alist, scm_cdr (big_to_small), value);
165 me->set_property (scm_car (big_to_small), alist);
168 // This converts an alist with nested overrides in it to a proper
169 // alist. The number of nested overrides is known in advance,
170 // everything up to the last nested override is copied, the tail is
174 nalist_to_alist (SCM nalist, int nested)
178 SCM copied = SCM_EOL;
179 SCM partials = SCM_EOL;
180 // partials is a alist of partial overrides
183 SCM elt = scm_car (nalist);
184 nalist = scm_cdr (nalist);
185 SCM key = scm_car (elt);
186 if (!scm_is_symbol (key))
188 if (scm_is_bool (key))
190 if (scm_is_false (key))
195 if (scm_is_pair (key))
196 // nested override: record for key in partial
198 SCM pair = scm_sloppy_assq (scm_car (key), partials);
199 if (scm_is_false (pair))
200 partials = scm_acons (scm_car (key), scm_list_1 (elt),
203 scm_set_cdr_x (pair, scm_cons (elt, scm_cdr (pair)));
207 // plain override: apply any known corresponding partials
208 SCM pair = assq_pop_x (key, &partials);
209 if (scm_is_true (pair))
211 SCM value = scm_cdr (elt);
212 for (SCM pp = scm_cdr (pair); scm_is_pair (pp); pp = scm_cdr (pp))
213 value = nested_property_alist (value, scm_cdaar (pp), scm_cdar (pp));
214 copied = scm_acons (key, value, copied);
217 copied = scm_cons (elt, copied);
219 // Now need to work off the remaining partials. All of them are
220 // unique, so we can push them to `copied' after resolving without
221 // losing information.
223 for (;scm_is_pair (partials); partials = scm_cdr (partials))
225 SCM pair = scm_car (partials);
226 SCM key = scm_car (pair);
227 SCM elt = scm_sloppy_assq (key, nalist);
229 if (scm_is_true (elt))
230 value = scm_cdr (elt);
232 for (SCM pp = scm_cdr (pair); scm_is_pair (pp); pp = scm_cdr (pp))
233 value = nested_property_alist (value, scm_cdaar (pp), scm_cdar (pp));
235 copied = scm_acons (key, value, copied);
237 return fast_reverse_x (copied, nalist);
241 // Alternative approach: don't unfold those partial overrides while
242 // they are part of contexts but instead use a special accessor for
243 // subproperties in the grob. Not used or tested for now.
246 nassq_ref (SCM key, SCM nalist, SCM fallback)
248 SCM partials = SCM_EOL;
249 // partials is list of partial overrides for the given property
250 for (SCM p = nalist; scm_is_pair (p); p = scm_cdr (p))
252 SCM elt = scm_car (p);
253 SCM pkey = scm_car (elt);
254 if (scm_is_pair (pkey))
256 if (scm_is_eq (scm_car (pkey), key))
257 partials = scm_cons (elt, partials);
259 else if (scm_is_eq (pkey, key))
261 SCM value = scm_cdr (elt);
262 for (; scm_is_pair (partials); partials = scm_cdr (partials))
264 value = nested_property_alist (value, scm_cdaar (partials),
265 scm_cdar (partials));
270 if (scm_is_pair (partials))
272 // Bit of a quandary here: we have only subproperty overrides
273 // but no main property. Could be a programming error, but we
274 // instead override an empty list.
275 SCM value = nested_create_alist (scm_cdaar (partials), scm_cdar (partials));
276 partials = scm_cdr (partials);
277 for (; scm_is_pair (partials); partials = scm_cdr (partials))
278 value = nested_property_alist (value, scm_cdaar (partials),
279 scm_cdar (partials));
282 return SCM_UNBNDP (fallback) ? SCM_EOL : fallback;
285 // Also needed for this approach to make sense: an accessor for true
288 nassq_nested_ref (SCM key, SCM subpath, SCM nalist, SCM fallback);