]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-property.cc
''
[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 #include "debug.hh"
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   assert (live());
147
148 #ifndef NDEBUG
149   if (internal_type_checking_global_b)
150     {
151       assert (type_check_assignment (s, v, ly_symbol2scm ("backend-type?")));
152       check_interfaces_for_property(this, s);
153     }
154 #endif
155
156 #ifndef HASHING_FOR_MUTABLE_PROPS
157   mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
158 #else
159   scm_hashq_set_x (mutable_property_alist_, s, v);
160 #endif
161 }
162
163
164 SCM
165 Grob::internal_get_grob_property (SCM sym) const
166 {
167 #ifndef HASHING_FOR_MUTABLE_PROPS
168   SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
169   if (s != SCM_BOOL_F)
170     return ly_cdr (s);
171 #else
172   if (mutable_property_alist_ == SCM_EOL)
173     return SCM_EOL;
174   
175   SCM s = scm_hashq_ref (mutable_property_alist_, sym, SCM_EOL);
176   if (s!=SCM_EOL)
177     return s;
178 #endif
179
180   s = scm_sloppy_assq (sym, immutable_property_alist_);
181   
182 #ifndef NDEBUG
183   if (internal_type_checking_global_b && gh_pair_p (s))
184     {
185       assert (type_check_assignment (sym, gh_cdr (s), ly_symbol2scm ("backend-type?")));
186       check_interfaces_for_property(this, sym);
187     }
188 #endif
189
190   return (s == SCM_BOOL_F) ? SCM_EOL : ly_cdr (s); 
191 }
192
193 void
194 Grob::substitute_mutable_properties (SCM crit, SCM orig)
195 {
196   set_break_subsititution(crit);
197 #ifndef HASHING_FOR_MUTABLE_PROPS
198   mutable_property_alist_ = substitute_mutable_property_alist (orig);
199 #else
200   if (orig == SCM_EOL)
201     {
202       mutable_property_alist_ = SCM_EOL;
203       return ;
204     }
205   
206   SCM * src_elts = SCM_VELTS (orig);
207   SCM * dest_elts = SCM_VELTS (mutable_property_alist_);  
208   unsigned int l = SCM_VECTOR_LENGTH(mutable_property_alist_);
209   assert (l == SCM_VECTOR_LENGTH(orig));
210   for (unsigned int i = 0; i < l; i++)
211     {
212       dest_elts[i] = substitute_mutable_property_alist (src_elts[i]);
213     }
214 #endif
215 }
216
217
218 bool
219 Grob::live () const
220 {
221   return immutable_property_alist_ != SCM_EOL;
222 }