]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-property.cc
* VERSION: 1.5.72 released
[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 SCM
63 Grob::get_property_alist_chain (SCM def) const
64 {
65 #ifndef HASHING_FOR_MUTABLE_PROPS
66   return  scm_list_n (mutable_property_alist_,
67                       immutable_property_alist_,
68                       def,
69                       SCM_UNDEFINED);
70 #else
71   SCM chain = gh_list (immutable_property_alist_, def, SCM_UNDEFINED);
72   SCM * velts = SCM_VELTS (mutable_property_alist_);
73   int l = SCM_VECTOR_LENGTH(mutable_property_alist_);
74   for (int i = 0; i < l; i++)
75     {
76       if (gh_pair_p (velts[i]))
77         chain = gh_cons ( velts[i], chain);
78     }
79
80   return chain;
81 #endif
82 }
83
84
85
86 /*
87   This special add_thing routine is slightly more efficient than
88
89     set_prop (name,cons (thing, get_prop (name)))
90
91   since it can reuse the handle returned by scm_assq().
92 */
93 void
94 Grob::add_to_list_property (SCM sym, SCM thing) 
95 {
96   SCM handle
97 #ifndef HASHING_FOR_MUTABLE_PROPS
98     = scm_sloppy_assq (sym, mutable_property_alist_)
99 #else
100     = scm_hashq_get_handle (mutable_property_alist_, sym);
101 #endif
102     ;
103   if (handle != SCM_BOOL_F)
104     {
105       gh_set_cdr_x (handle, gh_cons (thing, gh_cdr (handle)));
106     }
107   else
108     {
109       /*
110         There is no mutable prop yet, so create an entry, and put it in front of the
111         mutable prop list.
112       */
113       handle = scm_sloppy_assq (sym, immutable_property_alist_);
114       SCM tail = (handle != SCM_BOOL_F) ? gh_cdr(handle) : SCM_EOL;
115       SCM val = gh_cons (thing, tail);
116 #ifndef HASHING_FOR_MUTABLE_PROPS
117       mutable_property_alist_ = gh_cons (gh_cons (sym, val),
118                                          mutable_property_alist_);
119 #else
120       scm_hashq_set_x (mutable_property_alist_, sym, val);
121 #endif
122     }
123 }
124
125
126 extern void check_interfaces_for_property (Grob const *me, SCM sym);
127
128 void
129 Grob::internal_set_grob_property (SCM s, SCM v)
130 {
131   /*
132     Perhaps we simply do the assq_set, but what the heck.
133    */
134   if (!live())
135     return ; 
136
137 #ifndef NDEBUG
138   if (internal_type_checking_global_b)
139     {
140       assert (type_check_assignment (s, v, ly_symbol2scm ("backend-type?")));
141       check_interfaces_for_property(this, s);
142     }
143 #endif
144
145 #ifndef HASHING_FOR_MUTABLE_PROPS
146   mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
147 #else
148   scm_hashq_set_x (mutable_property_alist_, s, v);
149 #endif
150 }
151
152
153 SCM
154 Grob::internal_get_grob_property (SCM sym) const
155 {
156 #ifndef HASHING_FOR_MUTABLE_PROPS
157   SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
158   if (s != SCM_BOOL_F)
159     return ly_cdr (s);
160 #else
161   if (mutable_property_alist_ == SCM_EOL)
162     return SCM_EOL;
163   
164   SCM s = scm_hashq_ref (mutable_property_alist_, sym, SCM_EOL);
165   if (s!=SCM_EOL)
166     return s;
167 #endif
168
169   s = scm_sloppy_assq (sym, immutable_property_alist_);
170   
171 #ifndef NDEBUG
172   if (internal_type_checking_global_b && gh_pair_p (s))
173     {
174       assert (type_check_assignment (sym, gh_cdr (s), ly_symbol2scm ("backend-type?")));
175       check_interfaces_for_property(this, sym);
176     }
177 #endif
178
179   return (s == SCM_BOOL_F) ? SCM_EOL : ly_cdr (s); 
180 }
181
182 void
183 Grob::substitute_mutable_properties (SCM crit, SCM orig)
184 {
185   set_break_subsititution(crit);
186 #ifndef HASHING_FOR_MUTABLE_PROPS
187   mutable_property_alist_ = substitute_mutable_property_alist (orig);
188 #else
189   if (orig == SCM_EOL)
190     {
191       mutable_property_alist_ = SCM_EOL;
192       return ;
193     }
194   
195   SCM * src_elts = SCM_VELTS (orig);
196   SCM * dest_elts = SCM_VELTS (mutable_property_alist_);  
197   unsigned int l = SCM_VECTOR_LENGTH(mutable_property_alist_);
198   assert (l == SCM_VECTOR_LENGTH(orig));
199   for (unsigned int i = 0; i < l; i++)
200     {
201       dest_elts[i] = substitute_mutable_property_alist (src_elts[i]);
202     }
203 #endif
204 }
205
206
207 bool
208 Grob::live () const
209 {
210   return immutable_property_alist_ != SCM_EOL;
211 }