2.5-18.1
[glibc.git] / locale / programs / 3level.h
blob88f1b49724e8ddd916ebac326c0919904b3cf3e2
1 /* Copyright (C) 2000-2001, 2003, 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Bruno Haible <haible@clisp.cons.org>, 2000.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Construction of sparse 3-level tables.
19 See wchar-lookup.h or coll-lookup.h for their structure and the
20 meaning of p and q.
22 Before including this file, set
23 TABLE to the name of the structure to be defined
24 ELEMENT to the type of every entry
25 DEFAULT to the default value for empty entries
26 ITERATE if you want the TABLE_iterate function to be defined
27 NO_FINALIZE if you don't want the TABLE_finalize function to be defined
29 This will define
31 struct TABLE;
32 void TABLE_init (struct TABLE *t);
33 ELEMENT TABLE_get (struct TABLE *t, uint32_t wc);
34 void TABLE_add (struct TABLE *t, uint32_t wc, ELEMENT value);
35 void TABLE_iterate (struct TABLE *t,
36 void (*fn) (uint32_t wc, ELEMENT value));
37 void TABLE_finalize (struct TABLE *t);
40 #define CONCAT(a,b) CONCAT1(a,b)
41 #define CONCAT1(a,b) a##b
43 struct TABLE
45 /* Parameters. */
46 unsigned int p;
47 unsigned int q;
48 /* Working representation. */
49 size_t level1_alloc;
50 size_t level1_size;
51 uint32_t *level1;
52 size_t level2_alloc;
53 size_t level2_size;
54 uint32_t *level2;
55 size_t level3_alloc;
56 size_t level3_size;
57 ELEMENT *level3;
58 /* Compressed representation. */
59 size_t result_size;
60 char *result;
63 /* Initialize. Assumes t->p and t->q have already been set. */
64 static inline void
65 CONCAT(TABLE,_init) (struct TABLE *t)
67 t->level1 = NULL;
68 t->level1_alloc = t->level1_size = 0;
69 t->level2 = NULL;
70 t->level2_alloc = t->level2_size = 0;
71 t->level3 = NULL;
72 t->level3_alloc = t->level3_size = 0;
75 /* Marker for an empty slot. This has the value 0xFFFFFFFF, regardless
76 whether 'int' is 16 bit, 32 bit, or 64 bit. */
77 #define EMPTY ((uint32_t) ~0)
79 /* Retrieve an entry. */
80 static inline ELEMENT
81 __attribute ((always_inline))
82 CONCAT(TABLE,_get) (struct TABLE *t, uint32_t wc)
84 uint32_t index1 = wc >> (t->q + t->p);
85 if (index1 < t->level1_size)
87 uint32_t lookup1 = t->level1[index1];
88 if (lookup1 != EMPTY)
90 uint32_t index2 = ((wc >> t->p) & ((1 << t->q) - 1))
91 + (lookup1 << t->q);
92 uint32_t lookup2 = t->level2[index2];
93 if (lookup2 != EMPTY)
95 uint32_t index3 = (wc & ((1 << t->p) - 1))
96 + (lookup2 << t->p);
97 ELEMENT lookup3 = t->level3[index3];
99 return lookup3;
103 return DEFAULT;
106 /* Add one entry. */
107 static void
108 CONCAT(TABLE,_add) (struct TABLE *t, uint32_t wc, ELEMENT value)
110 uint32_t index1 = wc >> (t->q + t->p);
111 uint32_t index2 = (wc >> t->p) & ((1 << t->q) - 1);
112 uint32_t index3 = wc & ((1 << t->p) - 1);
113 size_t i, i1, i2;
115 if (value == CONCAT(TABLE,_get) (t, wc))
116 return;
118 if (index1 >= t->level1_size)
120 if (index1 >= t->level1_alloc)
122 size_t alloc = 2 * t->level1_alloc;
123 if (alloc <= index1)
124 alloc = index1 + 1;
125 t->level1 = (uint32_t *) xrealloc ((char *) t->level1,
126 alloc * sizeof (uint32_t));
127 t->level1_alloc = alloc;
129 while (index1 >= t->level1_size)
130 t->level1[t->level1_size++] = EMPTY;
133 if (t->level1[index1] == EMPTY)
135 if (t->level2_size == t->level2_alloc)
137 size_t alloc = 2 * t->level2_alloc + 1;
138 t->level2 = (uint32_t *) xrealloc ((char *) t->level2,
139 (alloc << t->q) * sizeof (uint32_t));
140 t->level2_alloc = alloc;
142 i1 = t->level2_size << t->q;
143 i2 = (t->level2_size + 1) << t->q;
144 for (i = i1; i < i2; i++)
145 t->level2[i] = EMPTY;
146 t->level1[index1] = t->level2_size++;
149 index2 += t->level1[index1] << t->q;
151 if (t->level2[index2] == EMPTY)
153 if (t->level3_size == t->level3_alloc)
155 size_t alloc = 2 * t->level3_alloc + 1;
156 t->level3 = (ELEMENT *) xrealloc ((char *) t->level3,
157 (alloc << t->p) * sizeof (ELEMENT));
158 t->level3_alloc = alloc;
160 i1 = t->level3_size << t->p;
161 i2 = (t->level3_size + 1) << t->p;
162 for (i = i1; i < i2; i++)
163 t->level3[i] = DEFAULT;
164 t->level2[index2] = t->level3_size++;
167 index3 += t->level2[index2] << t->p;
169 t->level3[index3] = value;
172 #ifdef ITERATE
173 /* Apply a function to all entries in the table. */
174 static void
175 CONCAT(TABLE,_iterate) (struct TABLE *t,
176 void (*fn) (uint32_t wc, ELEMENT value))
178 uint32_t index1;
179 for (index1 = 0; index1 < t->level1_size; index1++)
181 uint32_t lookup1 = t->level1[index1];
182 if (lookup1 != EMPTY)
184 uint32_t lookup1_shifted = lookup1 << t->q;
185 uint32_t index2;
186 for (index2 = 0; index2 < (1 << t->q); index2++)
188 uint32_t lookup2 = t->level2[index2 + lookup1_shifted];
189 if (lookup2 != EMPTY)
191 uint32_t lookup2_shifted = lookup2 << t->p;
192 uint32_t index3;
193 for (index3 = 0; index3 < (1 << t->p); index3++)
195 ELEMENT lookup3 = t->level3[index3 + lookup2_shifted];
196 if (lookup3 != DEFAULT)
197 fn ((((index1 << t->q) + index2) << t->p) + index3,
198 lookup3);
206 /* GCC ATM seems to do a poor job with pointers to nested functions passed
207 to inlined functions. Help it a little bit with this hack. */
208 #define wchead_table_iterate(tp, fn) \
209 do \
211 struct wchead_table *t = (tp); \
212 uint32_t index1; \
213 for (index1 = 0; index1 < t->level1_size; index1++) \
215 uint32_t lookup1 = t->level1[index1]; \
216 if (lookup1 != ((uint32_t) ~0)) \
218 uint32_t lookup1_shifted = lookup1 << t->q; \
219 uint32_t index2; \
220 for (index2 = 0; index2 < (1 << t->q); index2++) \
222 uint32_t lookup2 = t->level2[index2 + lookup1_shifted]; \
223 if (lookup2 != ((uint32_t) ~0)) \
225 uint32_t lookup2_shifted = lookup2 << t->p; \
226 uint32_t index3; \
227 for (index3 = 0; index3 < (1 << t->p); index3++) \
229 struct element_t *lookup3 \
230 = t->level3[index3 + lookup2_shifted]; \
231 if (lookup3 != NULL) \
232 fn ((((index1 << t->q) + index2) << t->p) + index3, \
233 lookup3); \
239 } while (0)
241 #endif
243 #ifndef NO_FINALIZE
244 /* Finalize and shrink. */
245 static void
246 CONCAT(TABLE,_finalize) (struct TABLE *t)
248 size_t i, j, k;
249 uint32_t reorder3[t->level3_size];
250 uint32_t reorder2[t->level2_size];
251 uint32_t level1_offset, level2_offset, level3_offset, last_offset;
253 /* Uniquify level3 blocks. */
254 k = 0;
255 for (j = 0; j < t->level3_size; j++)
257 for (i = 0; i < k; i++)
258 if (memcmp (&t->level3[i << t->p], &t->level3[j << t->p],
259 (1 << t->p) * sizeof (ELEMENT)) == 0)
260 break;
261 /* Relocate block j to block i. */
262 reorder3[j] = i;
263 if (i == k)
265 if (i != j)
266 memcpy (&t->level3[i << t->p], &t->level3[j << t->p],
267 (1 << t->p) * sizeof (ELEMENT));
268 k++;
271 t->level3_size = k;
273 for (i = 0; i < (t->level2_size << t->q); i++)
274 if (t->level2[i] != EMPTY)
275 t->level2[i] = reorder3[t->level2[i]];
277 /* Uniquify level2 blocks. */
278 k = 0;
279 for (j = 0; j < t->level2_size; j++)
281 for (i = 0; i < k; i++)
282 if (memcmp (&t->level2[i << t->q], &t->level2[j << t->q],
283 (1 << t->q) * sizeof (uint32_t)) == 0)
284 break;
285 /* Relocate block j to block i. */
286 reorder2[j] = i;
287 if (i == k)
289 if (i != j)
290 memcpy (&t->level2[i << t->q], &t->level2[j << t->q],
291 (1 << t->q) * sizeof (uint32_t));
292 k++;
295 t->level2_size = k;
297 for (i = 0; i < t->level1_size; i++)
298 if (t->level1[i] != EMPTY)
299 t->level1[i] = reorder2[t->level1[i]];
301 /* Create and fill the resulting compressed representation. */
302 last_offset =
303 5 * sizeof (uint32_t)
304 + t->level1_size * sizeof (uint32_t)
305 + (t->level2_size << t->q) * sizeof (uint32_t)
306 + (t->level3_size << t->p) * sizeof (ELEMENT);
307 t->result_size = (last_offset + 3) & ~3ul;
308 t->result = (char *) xmalloc (t->result_size);
310 level1_offset =
311 5 * sizeof (uint32_t);
312 level2_offset =
313 5 * sizeof (uint32_t)
314 + t->level1_size * sizeof (uint32_t);
315 level3_offset =
316 5 * sizeof (uint32_t)
317 + t->level1_size * sizeof (uint32_t)
318 + (t->level2_size << t->q) * sizeof (uint32_t);
320 ((uint32_t *) t->result)[0] = t->q + t->p;
321 ((uint32_t *) t->result)[1] = t->level1_size;
322 ((uint32_t *) t->result)[2] = t->p;
323 ((uint32_t *) t->result)[3] = (1 << t->q) - 1;
324 ((uint32_t *) t->result)[4] = (1 << t->p) - 1;
326 for (i = 0; i < t->level1_size; i++)
327 ((uint32_t *) (t->result + level1_offset))[i] =
328 (t->level1[i] == EMPTY
330 : (t->level1[i] << t->q) * sizeof (uint32_t) + level2_offset);
332 for (i = 0; i < (t->level2_size << t->q); i++)
333 ((uint32_t *) (t->result + level2_offset))[i] =
334 (t->level2[i] == EMPTY
336 : (t->level2[i] << t->p) * sizeof (ELEMENT) + level3_offset);
338 for (i = 0; i < (t->level3_size << t->p); i++)
339 ((ELEMENT *) (t->result + level3_offset))[i] = t->level3[i];
341 if (last_offset < t->result_size)
342 memset (t->result + last_offset, 0, t->result_size - last_offset);
344 if (t->level1_alloc > 0)
345 free (t->level1);
346 if (t->level2_alloc > 0)
347 free (t->level2);
348 if (t->level3_alloc > 0)
349 free (t->level3);
351 #endif
353 #undef EMPTY
354 #undef TABLE
355 #undef ELEMENT
356 #undef DEFAULT
357 #undef ITERATE
358 #undef NO_FINALIZE