]> git.donarmstrong.com Git - lilypond.git/blob - lily/nested-property.cc
Optimize assoc_tail for various key types
[lilypond.git] / lily / nested-property.cc
1 #include "context.hh"
2 #include "grob.hh"
3
4 // scm_reverse_x without the checks
5 SCM
6 fast_reverse_x (SCM lst, SCM tail)
7 {
8   while (!scm_is_null (lst))
9     {
10       SCM n = scm_cdr (lst);
11       scm_set_cdr_x (lst, tail);
12       tail = lst;
13       lst = n;
14     }
15   return tail;
16 }
17
18 // copy the spine of lst not including tail, appending newtail
19 // returns new list.
20 SCM
21 partial_list_copy (SCM lst, SCM tail, SCM newtail)
22 {
23   SCM p = SCM_EOL;
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);
27 }
28
29 SCM
30 assq_tail (SCM key, SCM alist, SCM based_on = SCM_EOL)
31 {
32   for (SCM p = alist; !scm_is_eq (p, based_on); p = scm_cdr (p))
33     {
34       if (scm_is_eq (scm_caar (p), key))
35         return p;
36     }
37   return SCM_BOOL_F;
38 }
39
40 SCM
41 assv_tail (SCM key, SCM alist, SCM based_on = SCM_EOL)
42 {
43   for (SCM p = alist; !scm_is_eq (p, based_on); p = scm_cdr (p))
44     {
45       if (scm_is_true (scm_eqv_p (scm_caar (p), key)))
46         return p;
47     }
48   return SCM_BOOL_F;
49 }
50
51 SCM
52 assoc_tail (SCM key, SCM alist, SCM based_on = SCM_EOL)
53 {
54   if (SCM_IMP (key))
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))
59     {
60       if (ly_is_equal (scm_caar (p), key))
61         return p;
62     }
63   return SCM_BOOL_F;
64 }
65
66 // Like assq, but removes the found element destructively
67 SCM assq_pop_x (SCM key, SCM *alist)
68 {
69   for (SCM p = *alist; scm_is_pair (p); p = *(alist = SCM_CDRLOC (p)))
70     {
71       if (scm_is_eq (scm_caar (p), key))
72         {
73           *alist = scm_cdr (p);
74           return scm_car (p);
75         }
76     }
77   return SCM_BOOL_F;
78 }
79
80 /*
81   Drop key from the list alist..alist_end.
82  */
83 SCM
84 evict_from_alist (SCM key, SCM alist, SCM alist_end)
85 {
86   SCM p = assoc_tail (key, alist, alist_end);
87
88   if (scm_is_true (p))
89     return partial_list_copy (alist, p, scm_cdr (p));
90   return alist;
91 }
92
93 // This is the same as
94 // nested_property_alist (SCM_EOL, prop_path, value) but faster
95 SCM
96 nested_create_alist (SCM prop_path, SCM value)
97 {
98   if (scm_is_null (prop_path))
99     return value;
100   return scm_acons (scm_car (prop_path),
101                     nested_create_alist (scm_cdr (prop_path), value),
102                     SCM_EOL);
103 }
104
105 /*
106   PROP_PATH should be big-to-small ordering
107  */
108
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
115 // paid anyway.
116
117 // A typical use case for this routine is applying (possibly nested)
118 // tweaks to a grob property list.
119
120 SCM
121 nested_property_alist (SCM alist, SCM prop_path, SCM value)
122 {
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))
127     {
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),
132                                                     rest,
133                                                     value),
134                         partial_list_copy (alist, where, scm_cdr (where)));
135     }
136   // Outcommented code would coalesce multiple overrides of the same
137   // property
138 #if 0
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)));
143 #endif
144   return scm_acons (key, value, alist);
145 }
146
147 SCM
148 nested_property (SCM alist, SCM prop_path, SCM fallback)
149 {
150   for (; scm_is_pair (prop_path); prop_path = scm_cdr (prop_path))
151     {
152       SCM tail = assoc_tail (scm_car (prop_path), alist);
153       if (scm_is_false (tail))
154         return fallback;
155       alist = scm_cdar (tail);
156     }
157   return alist;
158 }
159
160 void
161 set_nested_property (Grob *me, SCM big_to_small, SCM value)
162 {
163   SCM alist = me->get_property (scm_car (big_to_small));
164
165   alist = nested_property_alist (alist, scm_cdr (big_to_small), value);
166
167   me->set_property (scm_car (big_to_small), alist);
168 }
169
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
173 // shared
174
175 SCM
176 nalist_to_alist (SCM nalist, int nested)
177 {
178   if (!nested)
179     return nalist;
180   SCM copied = SCM_EOL;
181   SCM partials = SCM_EOL;
182   // partials is a alist of partial overrides
183   while (nested)
184     {
185       SCM elt = scm_car (nalist);
186       nalist = scm_cdr (nalist);
187       SCM key = scm_car (elt);
188       if (!scm_is_symbol (key))
189         --nested;
190       if (scm_is_bool (key))
191         {
192           if (scm_is_false (key))
193             continue;
194           elt = scm_cdr (elt);
195           key = scm_car (elt);
196         }
197       if (scm_is_pair (key))
198         // nested override: record for key in partial
199         {
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),
203                                   partials);
204           else
205             scm_set_cdr_x (pair, scm_cons (elt, scm_cdr (pair)));
206           continue;
207         }
208
209       // plain override: apply any known corresponding partials
210       SCM pair = assq_pop_x (key, &partials);
211       if (scm_is_true (pair))
212         {
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);
217         }
218       else
219         copied = scm_cons (elt, copied);
220     }
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.
224
225   for (;scm_is_pair (partials); partials = scm_cdr (partials))
226     {
227       SCM pair = scm_car (partials);
228       SCM key = scm_car (pair);
229       SCM elt = scm_sloppy_assq (key, nalist);
230       SCM value = SCM_EOL;
231       if (scm_is_true (elt))
232         value = scm_cdr (elt);
233
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));
236
237       copied = scm_acons (key, value, copied);
238     }
239   return fast_reverse_x (copied, nalist);
240 }
241
242 #if 0
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.
246
247 SCM
248 nassq_ref (SCM key, SCM nalist, SCM fallback)
249 {
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))
253     {
254       SCM elt = scm_car (p);
255       SCM pkey = scm_car (elt);
256       if (scm_is_pair (pkey))
257         {
258           if (scm_is_eq (scm_car (pkey), key))
259             partials = scm_cons (elt, partials);
260         }
261       else if (scm_is_eq (pkey, key))
262         {
263           SCM value = scm_cdr (elt);
264           for (; scm_is_pair (partials); partials = scm_cdr (partials))
265             {
266               value = nested_property_alist (value, scm_cdaar (partials),
267                                             scm_cdar (partials));
268             }
269           return value;
270         }
271     }
272   if (scm_is_pair (partials))
273     {
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));
282       return value;
283     }
284   return SCM_UNBNDP (fallback) ? SCM_EOL : fallback;
285 }
286
287 // Also needed for this approach to make sense: an accessor for true
288 // subproperties.
289 SCM
290 nassq_nested_ref (SCM key, SCM subpath, SCM nalist, SCM fallback);
291 // To be implemented
292
293 #endif