]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-property.cc
* input/regression/grace-auto-beam.ly: new file
[lilypond.git] / lily / grob-property.cc
1 /*
2   Implement storage and manipulation of grob properties.
3  */
4
5 #include <string.h>
6 #include <math.h>
7
8 #include "main.hh"
9 #include "input-smob.hh"
10
11 #include "group-interface.hh"
12 #include "misc.hh"
13 #include "paper-score.hh"
14 #include "paper-def.hh"
15 #include "grob.hh"
16
17 #include "spanner.hh"
18 #include "item.hh"
19 #include "misc.hh"
20 #include "item.hh"
21
22 /*
23   HASHING_FOR_MUTABLE_PROPS:
24
25   
26   plain, -O0 compile
27   
28 user    0m12.400s
29
30 sz == 13, -O0 compile
31   
32  xdvi trip
33
34 user    0m13.780s
35   
36 sz == 5
37
38
39 user    0m13.000s
40
41 sz == 3
42
43
44 user    0m13.080s
45
46 Hashing doesn't improve the result of grob property lookup, at least
47 not with naive hashing. It is possible that the overhead of the
48 scm_hash* functions take too much time. One way to solve this is by
49 using vector accesses directly, and precompute the hashvalues, similar
50 to CACHE_SYMBOLS. That option could only cause slowdowns if the hash
51 tables produces weird cache-line trashing.
52
53 Second option: we could index immutable props in a hash tab as
54 well. This only takes space, since they are immutable no updates are
55 needed.  This does take a lot of space, since we must duplicate the
56 alists (but not the entries).
57
58 */
59
60 // #define HASHING_FOR_MUTABLE_PROPS
61
62 /*
63   Remove the value associated with KEY, and return it. The result is
64   that a next call will yield SCM_EOL (and not the underlying
65   `basic' property.
66 */
67 SCM
68 Grob::remove_grob_property (const char* key)
69 {
70   SCM val = get_grob_property (key);
71   if (val != SCM_EOL)
72     set_grob_property (key, SCM_EOL);
73   return val;
74 }
75
76
77 SCM
78 Grob::get_property_alist_chain (SCM def) const
79 {
80 #ifndef HASHING_FOR_MUTABLE_PROPS
81   return  scm_list_n (mutable_property_alist_,
82                       immutable_property_alist_,
83                       def,
84                       SCM_UNDEFINED);
85 #else
86   SCM chain = gh_list (immutable_property_alist_, def, SCM_UNDEFINED);
87   SCM * velts = SCM_VELTS (mutable_property_alist_);
88   int l = SCM_VECTOR_LENGTH(mutable_property_alist_);
89   for (int i = 0; i < l; i++)
90     {
91       if (gh_pair_p (velts[i]))
92         chain = gh_cons ( velts[i], chain);
93     }
94
95   return chain;
96 #endif
97 }
98
99
100
101 /*
102   This special add_thing routine is slightly more efficient than
103
104     set_prop (name,cons (thing, get_prop (name)))
105
106   since it can reuse the handle returned by scm_assq().
107 */
108 void
109 Grob::add_to_list_property (SCM sym, SCM thing) 
110 {
111   SCM handle
112 #ifndef HASHING_FOR_MUTABLE_PROPS
113     = scm_sloppy_assq (sym, mutable_property_alist_)
114 #else
115     = scm_hashq_get_handle (mutable_property_alist_, sym);
116 #endif
117     ;
118   if (handle != SCM_BOOL_F)
119     {
120       gh_set_cdr_x (handle, gh_cons (thing, gh_cdr (handle)));
121     }
122   else
123     {
124       /*
125         There is no mutable prop yet, so create an entry, and put it in front of the
126         mutable prop list.
127       */
128       handle = scm_sloppy_assq (sym, immutable_property_alist_);
129       SCM tail = (handle != SCM_BOOL_F) ? gh_cdr(handle) : SCM_EOL;
130       SCM val = gh_cons (thing, tail);
131 #ifndef HASHING_FOR_MUTABLE_PROPS
132       mutable_property_alist_ = gh_cons (gh_cons (sym, val),
133                                          mutable_property_alist_);
134 #else
135       scm_hashq_set_x (mutable_property_alist_, sym, val);
136 #endif
137     }
138 }
139
140
141 extern void check_interfaces_for_property (Grob const *me, SCM sym);
142
143 void
144 Grob::internal_set_grob_property (SCM s, SCM v)
145 {
146   /*
147     Perhaps we simply do the assq_set, but what the heck.
148    */
149   if (!live())
150     return ; 
151
152 #ifndef NDEBUG
153   if (internal_type_checking_global_b)
154     {
155       assert (type_check_assignment (s, v, ly_symbol2scm ("backend-type?")));
156       check_interfaces_for_property(this, s);
157     }
158 #endif
159
160 #ifndef HASHING_FOR_MUTABLE_PROPS
161   mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
162 #else
163   scm_hashq_set_x (mutable_property_alist_, s, v);
164 #endif
165 }
166
167
168 SCM
169 Grob::internal_get_grob_property (SCM sym) const
170 {
171 #ifndef HASHING_FOR_MUTABLE_PROPS
172   SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
173   if (s != SCM_BOOL_F)
174     return ly_cdr (s);
175 #else
176   if (mutable_property_alist_ == SCM_EOL)
177     return SCM_EOL;
178   
179   SCM s = scm_hashq_ref (mutable_property_alist_, sym, SCM_EOL);
180   if (s!=SCM_EOL)
181     return s;
182 #endif
183
184   s = scm_sloppy_assq (sym, immutable_property_alist_);
185   
186 #ifndef NDEBUG
187   if (internal_type_checking_global_b && gh_pair_p (s))
188     {
189       assert (type_check_assignment (sym, gh_cdr (s), ly_symbol2scm ("backend-type?")));
190       check_interfaces_for_property(this, sym);
191     }
192 #endif
193
194   return (s == SCM_BOOL_F) ? SCM_EOL : ly_cdr (s); 
195 }
196
197 void
198 Grob::substitute_mutable_properties (SCM crit, SCM orig)
199 {
200   set_break_subsititution(crit);
201 #ifndef HASHING_FOR_MUTABLE_PROPS
202   mutable_property_alist_ = substitute_mutable_property_alist (orig);
203 #else
204   if (orig == SCM_EOL)
205     {
206       mutable_property_alist_ = SCM_EOL;
207       return ;
208     }
209   
210   SCM * src_elts = SCM_VELTS (orig);
211   SCM * dest_elts = SCM_VELTS (mutable_property_alist_);  
212   unsigned int l = SCM_VECTOR_LENGTH(mutable_property_alist_);
213   assert (l == SCM_VECTOR_LENGTH(orig));
214   for (unsigned int i = 0; i < l; i++)
215     {
216       dest_elts[i] = substitute_mutable_property_alist (src_elts[i]);
217     }
218 #endif
219 }
220
221
222 bool
223 Grob::live () const
224 {
225   return immutable_property_alist_ != SCM_EOL;
226 }