]> git.donarmstrong.com Git - lilypond.git/blob - lily/nested-property.cc
Issue 4798/1: Make alist routines use equal? key comparisons
[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 = assoc_tail (key, alist);
129
130       if (scm_is_false (where))
131         return scm_acons (key, nested_create_alist (rest, value), alist);
132       return scm_acons (key, nested_property_alist (scm_cdar (where),
133                                                     rest,
134                                                     value),
135                         partial_list_copy (alist, where, scm_cdr (where)));
136     }
137   // Outcommented code would coalesce multiple overrides of the same
138   // property
139 #if 0
140   SCM where = assq_tail (alist, key);
141   if (scm_is_true (where))
142     return scm_acons (key, value,
143                       partial_list_copy (alist, where, scm_cdr (where)));
144 #endif
145   return scm_acons (key, value, alist);
146 }
147
148 SCM
149 nested_property (SCM alist, SCM prop_path, SCM fallback)
150 {
151   for (; scm_is_pair (prop_path); prop_path = scm_cdr (prop_path))
152     {
153       SCM tail = assoc_tail (scm_car (prop_path), alist);
154       if (scm_is_false (tail))
155         return fallback;
156       alist = scm_cdar (tail);
157     }
158   return alist;
159 }
160
161 void
162 set_nested_property (Grob *me, SCM big_to_small, SCM value)
163 {
164   SCM alist = me->get_property (scm_car (big_to_small));
165
166   alist = nested_property_alist (alist, scm_cdr (big_to_small), value);
167
168   me->set_property (scm_car (big_to_small), alist);
169 }
170
171 // This converts an alist with nested overrides in it to a proper
172 // alist.  The number of nested overrides is known in advance,
173 // everything up to the last nested override is copied, the tail is
174 // shared
175
176 SCM
177 nalist_to_alist (SCM nalist, int nested)
178 {
179   if (!nested)
180     return nalist;
181   SCM copied = SCM_EOL;
182   SCM partials = SCM_EOL;
183   // partials is a alist of partial overrides
184   while (nested)
185     {
186       SCM elt = scm_car (nalist);
187       nalist = scm_cdr (nalist);
188       SCM key = scm_car (elt);
189       if (!scm_is_symbol (key))
190         --nested;
191       if (scm_is_bool (key))
192         {
193           if (scm_is_false (key))
194             continue;
195           elt = scm_cdr (elt);
196           key = scm_car (elt);
197         }
198       if (scm_is_pair (key))
199         // nested override: record for key in partial
200         {
201           SCM pair = scm_sloppy_assq (scm_car (key), partials);
202           if (scm_is_false (pair))
203             partials = scm_acons (scm_car (key), scm_list_1 (elt),
204                                   partials);
205           else
206             scm_set_cdr_x (pair, scm_cons (elt, scm_cdr (pair)));
207           continue;
208         }
209
210       // plain override: apply any known corresponding partials
211       SCM pair = assq_pop_x (key, &partials);
212       if (scm_is_true (pair))
213         {
214           SCM value = scm_cdr (elt);
215           for (SCM pp = scm_cdr (pair); scm_is_pair (pp); pp = scm_cdr (pp))
216             value = nested_property_alist (value, scm_cdaar (pp), scm_cdar (pp));
217           copied = scm_acons (key, value, copied);
218         }
219       else
220         copied = scm_cons (elt, copied);
221     }
222   // Now need to work off the remaining partials.  All of them are
223   // unique, so we can push them to `copied' after resolving without
224   // losing information.
225
226   for (;scm_is_pair (partials); partials = scm_cdr (partials))
227     {
228       SCM pair = scm_car (partials);
229       SCM key = scm_car (pair);
230       SCM elt = scm_sloppy_assq (key, nalist);
231       SCM value = SCM_EOL;
232       if (scm_is_true (elt))
233         value = scm_cdr (elt);
234
235       for (SCM pp = scm_cdr (pair); scm_is_pair (pp); pp = scm_cdr (pp))
236         value = nested_property_alist (value, scm_cdaar (pp), scm_cdar (pp));
237
238       copied = scm_acons (key, value, copied);
239     }
240   return fast_reverse_x (copied, nalist);
241 }
242
243 #if 0
244 // Alternative approach: don't unfold those partial overrides while
245 // they are part of contexts but instead use a special accessor for
246 // subproperties in the grob.  Not used or tested for now.
247
248 SCM
249 nassq_ref (SCM key, SCM nalist, SCM fallback)
250 {
251   SCM partials = SCM_EOL;
252   // partials is list of partial overrides for the given property
253   for (SCM p = nalist; scm_is_pair (p); p = scm_cdr (p))
254     {
255       SCM elt = scm_car (p);
256       SCM pkey = scm_car (elt);
257       if (scm_is_pair (pkey))
258         {
259           if (scm_is_eq (scm_car (pkey), key))
260             partials = scm_cons (elt, partials);
261         }
262       else if (scm_is_eq (pkey, key))
263         {
264           SCM value = scm_cdr (elt);
265           for (; scm_is_pair (partials); partials = scm_cdr (partials))
266             {
267               value = nested_property_alist (value, scm_cdaar (partials),
268                                             scm_cdar (partials));
269             }
270           return value;
271         }
272     }
273   if (scm_is_pair (partials))
274     {
275       // Bit of a quandary here: we have only subproperty overrides
276       // but no main property.  Could be a programming error, but we
277       // instead override an empty list.
278       SCM value = nested_create_alist (scm_cdaar (partials), scm_cdar (partials));
279       partials = scm_cdr (partials);
280       for (; scm_is_pair (partials); partials = scm_cdr (partials))
281         value = nested_property_alist (value, scm_cdaar (partials),
282                                       scm_cdar (partials));
283       return value;
284     }
285   return SCM_UNBNDP (fallback) ? SCM_EOL : fallback;
286 }
287
288 // Also needed for this approach to make sense: an accessor for true
289 // subproperties.
290 SCM
291 nassq_nested_ref (SCM key, SCM subpath, SCM nalist, SCM fallback);
292 // To be implemented
293
294 #endif