Update copyright notices with scripts/update-copyrights.
[glibc.git] / locale / programs / 3level.h
blobc8ed119ffdb89d643ad738f79bc5255c32196784
1 /* Copyright (C) 2000-2013 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 as published
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
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);
205 #endif
207 #ifndef NO_FINALIZE
208 /* Finalize and shrink. */
209 static void
210 CONCAT(TABLE,_finalize) (struct TABLE *t)
212 size_t i, j, k;
213 uint32_t reorder3[t->level3_size];
214 uint32_t reorder2[t->level2_size];
215 uint32_t level1_offset, level2_offset, level3_offset, last_offset;
217 /* Uniquify level3 blocks. */
218 k = 0;
219 for (j = 0; j < t->level3_size; j++)
221 for (i = 0; i < k; i++)
222 if (memcmp (&t->level3[i << t->p], &t->level3[j << t->p],
223 (1 << t->p) * sizeof (ELEMENT)) == 0)
224 break;
225 /* Relocate block j to block i. */
226 reorder3[j] = i;
227 if (i == k)
229 if (i != j)
230 memcpy (&t->level3[i << t->p], &t->level3[j << t->p],
231 (1 << t->p) * sizeof (ELEMENT));
232 k++;
235 t->level3_size = k;
237 for (i = 0; i < (t->level2_size << t->q); i++)
238 if (t->level2[i] != EMPTY)
239 t->level2[i] = reorder3[t->level2[i]];
241 /* Uniquify level2 blocks. */
242 k = 0;
243 for (j = 0; j < t->level2_size; j++)
245 for (i = 0; i < k; i++)
246 if (memcmp (&t->level2[i << t->q], &t->level2[j << t->q],
247 (1 << t->q) * sizeof (uint32_t)) == 0)
248 break;
249 /* Relocate block j to block i. */
250 reorder2[j] = i;
251 if (i == k)
253 if (i != j)
254 memcpy (&t->level2[i << t->q], &t->level2[j << t->q],
255 (1 << t->q) * sizeof (uint32_t));
256 k++;
259 t->level2_size = k;
261 for (i = 0; i < t->level1_size; i++)
262 if (t->level1[i] != EMPTY)
263 t->level1[i] = reorder2[t->level1[i]];
265 /* Create and fill the resulting compressed representation. */
266 last_offset =
267 5 * sizeof (uint32_t)
268 + t->level1_size * sizeof (uint32_t)
269 + (t->level2_size << t->q) * sizeof (uint32_t)
270 + (t->level3_size << t->p) * sizeof (ELEMENT);
271 t->result_size = (last_offset + 3) & ~3ul;
272 t->result = (char *) xmalloc (t->result_size);
274 level1_offset =
275 5 * sizeof (uint32_t);
276 level2_offset =
277 5 * sizeof (uint32_t)
278 + t->level1_size * sizeof (uint32_t);
279 level3_offset =
280 5 * sizeof (uint32_t)
281 + t->level1_size * sizeof (uint32_t)
282 + (t->level2_size << t->q) * sizeof (uint32_t);
284 ((uint32_t *) t->result)[0] = t->q + t->p;
285 ((uint32_t *) t->result)[1] = t->level1_size;
286 ((uint32_t *) t->result)[2] = t->p;
287 ((uint32_t *) t->result)[3] = (1 << t->q) - 1;
288 ((uint32_t *) t->result)[4] = (1 << t->p) - 1;
290 for (i = 0; i < t->level1_size; i++)
291 ((uint32_t *) (t->result + level1_offset))[i] =
292 (t->level1[i] == EMPTY
294 : (t->level1[i] << t->q) * sizeof (uint32_t) + level2_offset);
296 for (i = 0; i < (t->level2_size << t->q); i++)
297 ((uint32_t *) (t->result + level2_offset))[i] =
298 (t->level2[i] == EMPTY
300 : (t->level2[i] << t->p) * sizeof (ELEMENT) + level3_offset);
302 for (i = 0; i < (t->level3_size << t->p); i++)
303 ((ELEMENT *) (t->result + level3_offset))[i] = t->level3[i];
305 if (last_offset < t->result_size)
306 memset (t->result + last_offset, 0, t->result_size - last_offset);
308 if (t->level1_alloc > 0)
309 free (t->level1);
310 if (t->level2_alloc > 0)
311 free (t->level2);
312 if (t->level3_alloc > 0)
313 free (t->level3);
315 #endif
317 #undef EMPTY
318 #undef TABLE
319 #undef ELEMENT
320 #undef DEFAULT
321 #undef ITERATE
322 #undef NO_FINALIZE