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 nested_property (SCM alist, SCM prop_path, SCM fallback)
137 for (; scm_is_pair (prop_path); prop_path = scm_cdr (prop_path))
139 SCM tail = assoc_tail (scm_car (prop_path), alist);
140 if (scm_is_false (tail))
142 alist = scm_cdar (tail);
148 set_nested_property (Grob *me, SCM big_to_small, SCM value)
150 SCM alist = me->get_property (scm_car (big_to_small));
152 alist = nested_property_alist (alist, scm_cdr (big_to_small), value);
154 me->set_property (scm_car (big_to_small), alist);
157 // This converts an alist with nested overrides in it to a proper
158 // alist. The number of nested overrides is known in advance,
159 // everything up to the last nested override is copied, the tail is
163 nalist_to_alist (SCM nalist, int nested)
167 SCM copied = SCM_EOL;
168 SCM partials = SCM_EOL;
169 // partials is a alist of partial overrides
172 SCM elt = scm_car (nalist);
173 nalist = scm_cdr (nalist);
174 SCM key = scm_car (elt);
175 if (!scm_is_symbol (key))
177 if (scm_is_bool (key))
179 if (scm_is_false (key))
184 if (scm_is_pair (key))
185 // nested override: record for key in partial
187 SCM pair = scm_sloppy_assq (scm_car (key), partials);
188 if (scm_is_false (pair))
189 partials = scm_acons (scm_car (key), scm_list_1 (elt),
192 scm_set_cdr_x (pair, scm_cons (elt, scm_cdr (pair)));
196 // plain override: apply any known corresponding partials
197 SCM pair = assq_pop_x (key, &partials);
198 if (scm_is_true (pair))
200 SCM value = scm_cdr (elt);
201 for (SCM pp = scm_cdr (pair); scm_is_pair (pp); pp = scm_cdr (pp))
202 value = nested_property_alist (value, scm_cdaar (pp), scm_cdar (pp));
203 copied = scm_acons (key, value, copied);
206 copied = scm_cons (elt, copied);
208 // Now need to work off the remaining partials. All of them are
209 // unique, so we can push them to `copied' after resolving without
210 // losing information.
212 for (;scm_is_pair (partials); partials = scm_cdr (partials))
214 SCM pair = scm_car (partials);
215 SCM key = scm_car (pair);
216 SCM elt = scm_sloppy_assq (key, nalist);
218 if (scm_is_true (elt))
219 value = scm_cdr (elt);
221 for (SCM pp = scm_cdr (pair); scm_is_pair (pp); pp = scm_cdr (pp))
222 value = nested_property_alist (value, scm_cdaar (pp), scm_cdar (pp));
224 copied = scm_acons (key, value, copied);
226 return fast_reverse_x (copied, nalist);
230 // Alternative approach: don't unfold those partial overrides while
231 // they are part of contexts but instead use a special accessor for
232 // subproperties in the grob. Not used or tested for now.
235 nassq_ref (SCM key, SCM nalist, SCM fallback)
237 SCM partials = SCM_EOL;
238 // partials is list of partial overrides for the given property
239 for (SCM p = nalist; scm_is_pair (p); p = scm_cdr (p))
241 SCM elt = scm_car (p);
242 SCM pkey = scm_car (elt);
243 if (scm_is_pair (pkey))
245 if (scm_is_eq (scm_car (pkey), key))
246 partials = scm_cons (elt, partials);
248 else if (scm_is_eq (pkey, key))
250 SCM value = scm_cdr (elt);
251 for (; scm_is_pair (partials); partials = scm_cdr (partials))
253 value = nested_property_alist (value, scm_cdaar (partials),
254 scm_cdar (partials));
259 if (scm_is_pair (partials))
261 // Bit of a quandary here: we have only subproperty overrides
262 // but no main property. Could be a programming error, but we
263 // instead override an empty list.
264 SCM value = nested_create_alist (scm_cdaar (partials), scm_cdar (partials));
265 partials = scm_cdr (partials);
266 for (; scm_is_pair (partials); partials = scm_cdr (partials))
267 value = nested_property_alist (value, scm_cdaar (partials),
268 scm_cdar (partials));
271 return SCM_UNBNDP (fallback) ? SCM_EOL : fallback;
274 // Also needed for this approach to make sense: an accessor for true
277 nassq_nested_ref (SCM key, SCM subpath, SCM nalist, SCM fallback);