Debian Codegraph @

texk/web2c/luatexdir/utils/managed-sa.c

raw
1 /*
2
3 Copyright 2006-2010 Taco Hoekwater <[email protected]>
4
5 This file is part of LuaTeX.
6
7 LuaTeX is free software; you can redistribute it and/or modify it under the terms
8 of the GNU General Public License as published by the Free Software Foundation;
9 either version 2 of the License, or (at your option) any later version.
10
11 LuaTeX is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along with
16 LuaTeX; if not, see <http://www.gnu.org/licenses/>.
17
18 */
19
20 /*tex
21
22 Here we implement sparse arrays with an embedded save stack. These functions
23 are called very often but a few days of experimenting proved that there is
24 not much to gain (if at all) from using macros or optimizations like
25 preallocating and fast access to the first 128 entries. In practice the
26 overhead is mostly in accessing memory and not in (probably inlined) calls.
27 So, we should accept fate and wait for faster memory. It's the price we pay
28 for being unicode on the one hand and sparse on the other.
29
30 */
31
32 #include "ptexlib.h"
33
34 static void store_sa_stack(sa_tree a, int n, sa_tree_item v, int gl)
35 {
36 sa_stack_item st;
37 st.code = n;
38 st.value = v;
39 st.level = gl;
40 if (a->stack == NULL) {
41 a->stack = Mxmalloc_array(sa_stack_item, a->stack_size);
42 } else if (((a->stack_ptr) + 1) >= a->stack_size) {
43 a->stack_size += a->stack_step;
44 a->stack = Mxrealloc_array(a->stack, sa_stack_item, a->stack_size);
45 }
46 (a->stack_ptr)++;
47 a->stack[a->stack_ptr] = st;
48 }
49
50 static void skip_in_stack(sa_tree a, int n)
51 {
52 int p = a->stack_ptr;
53 if (a->stack == NULL)
54 return;
55 while (p > 0) {
56 if (a->stack[p].code == n && a->stack[p].level > 0) {
57 a->stack[p].level = -(a->stack[p].level);
58 }
59 p--;
60 }
61 }
62
63 sa_tree_item get_sa_item(const sa_tree head, const int n)
64 {
65 if (head->tree != NULL) {
66 register int h = HIGHPART_PART(n);
67 if (head->tree[h] != NULL) {
68 register int m = MIDPART_PART(n);
69 if (head->tree[h][m] != NULL) {
70 return head->tree[h][m][LOWPART_PART(n)];
71 }
72 }
73 }
74 return head->dflt;
75 }
76
77 void set_sa_item(sa_tree head, int n, sa_tree_item v, int gl)
78 {
79 int h = HIGHPART_PART(n);
80 int m = MIDPART_PART(n);
81 int l = LOWPART_PART(n);
82 if (head->tree == NULL) {
83 head->tree = (sa_tree_item ***) Mxcalloc_array(sa_tree_item **, HIGHPART);
84 }
85 if (head->tree[h] == NULL) {
86 head->tree[h] = (sa_tree_item **) Mxcalloc_array(sa_tree_item *, MIDPART);
87 }
88 if (head->tree[h][m] == NULL) {
89 int i;
90 head->tree[h][m] = (sa_tree_item *) Mxmalloc_array(sa_tree_item, LOWPART);
91 for (i = 0; i < LOWPART; i++) {
92 head->tree[h][m][i] = head->dflt;
93 }
94 }
95 if (gl <= 1) {
96 skip_in_stack(head, n);
97 } else {
98 store_sa_stack(head, n, head->tree[h][m][l], gl);
99 }
100 head->tree[h][m][l] = v;
101 }
102
103 void rawset_sa_item(sa_tree head, int n, sa_tree_item v)
104 {
105 head->tree[HIGHPART_PART(n)][MIDPART_PART(n)][LOWPART_PART(n)] = v;
106 }
107
108 void clear_sa_stack(sa_tree a)
109 {
110 xfree(a->stack);
111 a->stack_ptr = 0;
112 a->stack_size = a->stack_step;
113 }
114
115 void destroy_sa_tree(sa_tree a)
116 {
117 if (a == NULL)
118 return;
119 if (a->tree != NULL) {
120 int h, m;
121 for (h = 0; h < HIGHPART; h++) {
122 if (a->tree[h] != NULL) {
123 for (m = 0; m < MIDPART; m++) {
124 xfree(a->tree[h][m]);
125 }
126 xfree(a->tree[h]);
127 }
128 }
129 xfree(a->tree);
130 }
131 xfree(a->stack);
132 xfree(a);
133 }
134
135 sa_tree copy_sa_tree(sa_tree b)
136 {
137 sa_tree a = (sa_tree) Mxmalloc_array(sa_tree_head, 1);
138 a->stack_step = b->stack_step;
139 a->stack_size = b->stack_size;
140 a->stack_type = b->stack_type;
141 a->dflt = b->dflt;
142 a->stack = NULL;
143 a->stack_ptr = 0;
144 a->tree = NULL;
145 if (b->tree != NULL) {
146 int h, m;
147 a->tree = (sa_tree_item ***) Mxcalloc_array(void *, HIGHPART);
148 for (h = 0; h < HIGHPART; h++) {
149 if (b->tree[h] != NULL) {
150 a->tree[h] = (sa_tree_item **) Mxcalloc_array(void *, MIDPART);
151 for (m = 0; m < MIDPART; m++) {
152 if (b->tree[h][m] != NULL) {
153 a->tree[h][m] = Mxmalloc_array(sa_tree_item, LOWPART);
154 memcpy(a->tree[h][m], b->tree[h][m],
155 sizeof(sa_tree_item) * LOWPART);
156 }
157 }
158 }
159 }
160 }
161 return a;
162 }
163
164 /*tes
165
166 The main reason to fill in the lowest entry branches here immediately is that
167 most of the sparse arrays have a bias toward \ASCII\ values.
168
169 Allocating those here immediately improves the chance of the structure
170 |a->tree[0][0][x]| being close together in actual memory locations
171
172 We could save less for type 0 stacks.
173
174 */
175
176 sa_tree new_sa_tree(int size, int type, sa_tree_item dflt)
177 {
178 sa_tree_head *a;
179 a = (sa_tree_head *) xmalloc(sizeof(sa_tree_head));
180 a->dflt = dflt;
181 a->stack = NULL;
182 a->tree = (sa_tree_item ***) Mxcalloc_array(sa_tree_item **, HIGHPART);
183 a->tree[0] = (sa_tree_item **) Mxcalloc_array(sa_tree_item *, MIDPART);
184 a->stack_size = size;
185 a->stack_step = size;
186 a->stack_type = type;
187 a->stack_ptr = 0;
188 return (sa_tree) a;
189 }
190
191 void restore_sa_stack(sa_tree head, int gl)
192 {
193 sa_stack_item st;
194 if (head->stack == NULL)
195 return;
196 while (head->stack_ptr > 0 && abs(head->stack[head->stack_ptr].level) >= gl) {
197 st = head->stack[head->stack_ptr];
198 if (st.level > 0) {
199 rawset_sa_item(head, st.code, st.value);
200 }
201 (head->stack_ptr)--;
202 }
203 }
204
205 void dump_sa_tree(sa_tree a, const char * name)
206 {
207 boolean f;
208 int x, n;
209 int h, m, l;
210 dump_int(a->stack_step);
211 x = a->dflt.int_value;
212 dump_int(x);
213 if (a->tree != NULL) {
214 /*tex A marker: */
215 dump_int(1);
216 n = a->stack_type;
217 dump_int(n);
218 for (h = 0; h < HIGHPART; h++) {
219 if (a->tree[h] != NULL) {
220 f = 1;
221 dump_qqqq(f);
222 for (m = 0; m < MIDPART; m++) {
223 if (a->tree[h][m] != NULL) {
224 f = 1;
225 dump_qqqq(f);
226 for (l = 0; l < LOWPART; l++) {
227 if (n == 2) {
228 x = a->tree[h][m][l].dump_uint.value_1;
229 dump_int(x);
230 x = a->tree[h][m][l].dump_uint.value_2;
231 dump_int(x);
232 } else {
233 x = a->tree[h][m][l].uint_value;
234 dump_int(x);
235 }
236 }
237 } else {
238 f = 0;
239 dump_qqqq(f);
240 }
241 }
242 } else {
243 f = 0;
244 dump_qqqq(f);
245 }
246 }
247 } else {
248 /*tex A marker: */
249 dump_int(0);
250 }
251 }
252
253 sa_tree undump_sa_tree(const char * name)
254 {
255 int x, n;
256 int h, m, l;
257 boolean f;
258 sa_tree a = (sa_tree) Mxmalloc_array(sa_tree_head, 1);
259 undump_int(x);
260 a->stack_step = x;
261 a->stack_size = x;
262 undump_int(x);
263 a->dflt.int_value = x;
264 a->stack = Mxmalloc_array(sa_stack_item, a->stack_size);
265 a->stack_ptr = 0;
266 a->tree = NULL;
267 /*tex The marker: */
268 undump_int(x);
269 if (x == 0)
270 return a;
271 a->tree = (sa_tree_item ***) Mxcalloc_array(void *, HIGHPART);
272 undump_int(n);
273 a->stack_type = n;
274 for (h = 0; h < HIGHPART; h++) {
275 undump_qqqq(f);
276 if (f > 0) {
277 a->tree[h] = (sa_tree_item **) Mxcalloc_array(void *, MIDPART);
278 for (m = 0; m < MIDPART; m++) {
279 undump_qqqq(f);
280 if (f > 0) {
281 a->tree[h][m] = Mxmalloc_array(sa_tree_item, LOWPART);
282 for (l = 0; l < LOWPART; l++) {
283 if (n == 2) {
284 undump_int(x);
285 a->tree[h][m][l].dump_uint.value_1 = x;
286 undump_int(x);
287 a->tree[h][m][l].dump_uint.value_2 = x;
288 } else {
289 undump_int(x);
290 a->tree[h][m][l].uint_value = x;
291 }
292 }
293 }
294 }
295 }
296 }
297 return a;
298 }