source: trunk/third/gettext/lib/3level.h @ 16931

Revision 16931, 8.8 KB checked in by ghudson, 23 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r16930, which included commits to RCS files with non-trunk default branches.
Line 
1/* Copyright (C) 2000 Free Software Foundation, Inc.
2   This file is part of the GNU C Library.
3   Contributed by Bruno Haible <haible@clisp.cons.org>, 2000.
4
5
6   NOTE: The canonical source of this file is maintained with the GNU C Library.
7   Bugs can be reported to bug-glibc@gnu.org.
8
9   This program is free software; you can redistribute it and/or modify it
10   under the terms of the GNU General Public License as published by the
11   Free Software Foundation; either version 2, or (at your option) any
12   later version.
13
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22   USA.  */
23
24/* Construction of sparse 3-level tables.
25   See wchar-lookup.h or coll-lookup.h for their structure and the
26   meaning of p and q.
27
28   Before including this file, set
29     TABLE        to the name of the structure to be defined
30     ELEMENT      to the type of every entry
31     DEFAULT      to the default value for empty entries
32     ITERATE      if you want the TABLE_iterate function to be defined
33     NO_FINALIZE  if you don't want the TABLE_finalize function to be defined
34
35   This will define
36
37     struct TABLE;
38     void TABLE_init (struct TABLE *t);
39     ELEMENT TABLE_get (struct TABLE *t, uint32_t wc);
40     void TABLE_add (struct TABLE *t, uint32_t wc, ELEMENT value);
41     void TABLE_iterate (struct TABLE *t,
42                         void (*fn) (uint32_t wc, ELEMENT value));
43     void TABLE_finalize (struct TABLE *t);
44*/
45
46#define CONCAT(a,b) CONCAT1(a,b)
47#define CONCAT1(a,b) a##b
48
49struct TABLE
50{
51  /* Parameters.  */
52  unsigned int p;
53  unsigned int q;
54  /* Working representation.  */
55  size_t level1_alloc;
56  size_t level1_size;
57  uint32_t *level1;
58  size_t level2_alloc;
59  size_t level2_size;
60  uint32_t *level2;
61  size_t level3_alloc;
62  size_t level3_size;
63  ELEMENT *level3;
64  /* Compressed representation.  */
65  size_t result_size;
66  char *result;
67};
68
69/* Initialize.  Assumes t->p and t->q have already been set.  */
70static inline void
71CONCAT(TABLE,_init) (struct TABLE *t)
72{
73  t->level1 = NULL;
74  t->level1_alloc = t->level1_size = 0;
75  t->level2 = NULL;
76  t->level2_alloc = t->level2_size = 0;
77  t->level3 = NULL;
78  t->level3_alloc = t->level3_size = 0;
79}
80
81/* Retrieve an entry.  */
82static inline ELEMENT
83CONCAT(TABLE,_get) (struct TABLE *t, uint32_t wc)
84{
85  uint32_t index1 = wc >> (t->q + t->p);
86  if (index1 < t->level1_size)
87    {
88      uint32_t lookup1 = t->level1[index1];
89      if (lookup1 != ~((uint32_t) 0))
90        {
91          uint32_t index2 = ((wc >> t->p) & ((1 << t->q) - 1))
92                            + (lookup1 << t->q);
93          uint32_t lookup2 = t->level2[index2];
94          if (lookup2 != ~((uint32_t) 0))
95            {
96              uint32_t index3 = (wc & ((1 << t->p) - 1))
97                                + (lookup2 << t->p);
98              ELEMENT lookup3 = t->level3[index3];
99
100              return lookup3;
101            }
102        }
103    }
104  return DEFAULT;
105}
106
107/* Add one entry.  */
108static void
109CONCAT(TABLE,_add) (struct TABLE *t, uint32_t wc, ELEMENT value)
110{
111  uint32_t index1 = wc >> (t->q + t->p);
112  uint32_t index2 = (wc >> t->p) & ((1 << t->q) - 1);
113  uint32_t index3 = wc & ((1 << t->p) - 1);
114  size_t i, i1, i2;
115
116  if (value == CONCAT(TABLE,_get) (t, wc))
117    return;
118
119  if (index1 >= t->level1_size)
120    {
121      if (index1 >= t->level1_alloc)
122        {
123          size_t alloc = 2 * t->level1_alloc;
124          if (alloc <= index1)
125            alloc = index1 + 1;
126          t->level1 = (uint32_t *) xrealloc ((char *) t->level1,
127                                             alloc * sizeof (uint32_t));
128          t->level1_alloc = alloc;
129        }
130      while (index1 >= t->level1_size)
131        t->level1[t->level1_size++] = ~((uint32_t) 0);
132    }
133
134  if (t->level1[index1] == ~((uint32_t) 0))
135    {
136      if (t->level2_size == t->level2_alloc)
137        {
138          size_t alloc = 2 * t->level2_alloc + 1;
139          t->level2 = (uint32_t *) xrealloc ((char *) t->level2,
140                                             (alloc << t->q) * sizeof (uint32_t));
141          t->level2_alloc = alloc;
142        }
143      i1 = t->level2_size << t->q;
144      i2 = (t->level2_size + 1) << t->q;
145      for (i = i1; i < i2; i++)
146        t->level2[i] = ~((uint32_t) 0);
147      t->level1[index1] = t->level2_size++;
148    }
149
150  index2 += t->level1[index1] << t->q;
151
152  if (t->level2[index2] == ~((uint32_t) 0))
153    {
154      if (t->level3_size == t->level3_alloc)
155        {
156          size_t alloc = 2 * t->level3_alloc + 1;
157          t->level3 = (ELEMENT *) xrealloc ((char *) t->level3,
158                                            (alloc << t->p) * sizeof (ELEMENT));
159          t->level3_alloc = alloc;
160        }
161      i1 = t->level3_size << t->p;
162      i2 = (t->level3_size + 1) << t->p;
163      for (i = i1; i < i2; i++)
164        t->level3[i] = DEFAULT;
165      t->level2[index2] = t->level3_size++;
166    }
167
168  index3 += t->level2[index2] << t->p;
169
170  t->level3[index3] = value;
171}
172
173#ifdef ITERATE
174/* Apply a function to all entries in the table.  */
175static void
176CONCAT(TABLE,_iterate) (struct TABLE *t,
177                        void (*fn) (uint32_t wc, ELEMENT value))
178{
179  uint32_t index1;
180  for (index1 = 0; index1 < t->level1_size; index1++)
181    {
182      uint32_t lookup1 = t->level1[index1];
183      if (lookup1 != ~((uint32_t) 0))
184        {
185          uint32_t lookup1_shifted = lookup1 << t->q;
186          uint32_t index2;
187          for (index2 = 0; index2 < (1 << t->q); index2++)
188            {
189              uint32_t lookup2 = t->level2[index2 + lookup1_shifted];
190              if (lookup2 != ~((uint32_t) 0))
191                {
192                  uint32_t lookup2_shifted = lookup2 << t->p;
193                  uint32_t index3;
194                  for (index3 = 0; index3 < (1 << t->p); index3++)
195                    {
196                      ELEMENT lookup3 = t->level3[index3 + lookup2_shifted];
197                      if (lookup3 != DEFAULT)
198                        fn ((((index1 << t->q) + index2) << t->p) + index3,
199                            lookup3);
200                    }
201                }
202            }
203        }
204    }
205}
206#endif
207
208#ifndef NO_FINALIZE
209/* Finalize and shrink.  */
210static void
211CONCAT(TABLE,_finalize) (struct TABLE *t)
212{
213  size_t i, j, k;
214  uint32_t reorder3[t->level3_size];
215  uint32_t reorder2[t->level2_size];
216  uint32_t level1_offset, level2_offset, level3_offset, last_offset;
217
218  /* Uniquify level3 blocks.  */
219  k = 0;
220  for (j = 0; j < t->level3_size; j++)
221    {
222      for (i = 0; i < k; i++)
223        if (memcmp (&t->level3[i << t->p], &t->level3[j << t->p],
224                    (1 << t->p) * sizeof (ELEMENT)) == 0)
225          break;
226      /* Relocate block j to block i.  */
227      reorder3[j] = i;
228      if (i == k)
229        {
230          if (i != j)
231            memcpy (&t->level3[i << t->p], &t->level3[j << t->p],
232                    (1 << t->p) * sizeof (ELEMENT));
233          k++;
234        }
235    }
236  t->level3_size = k;
237
238  for (i = 0; i < (t->level2_size << t->q); i++)
239    if (t->level2[i] != ~((uint32_t) 0))
240      t->level2[i] = reorder3[t->level2[i]];
241
242  /* Uniquify level2 blocks.  */
243  k = 0;
244  for (j = 0; j < t->level2_size; j++)
245    {
246      for (i = 0; i < k; i++)
247        if (memcmp (&t->level2[i << t->q], &t->level2[j << t->q],
248                    (1 << t->q) * sizeof (uint32_t)) == 0)
249          break;
250      /* Relocate block j to block i.  */
251      reorder2[j] = i;
252      if (i == k)
253        {
254          if (i != j)
255            memcpy (&t->level2[i << t->q], &t->level2[j << t->q],
256                    (1 << t->q) * sizeof (uint32_t));
257          k++;
258        }
259    }
260  t->level2_size = k;
261
262  for (i = 0; i < t->level1_size; i++)
263    if (t->level1[i] != ~((uint32_t) 0))
264      t->level1[i] = reorder2[t->level1[i]];
265
266  /* Create and fill the resulting compressed representation.  */
267  last_offset =
268    5 * sizeof (uint32_t)
269    + t->level1_size * sizeof (uint32_t)
270    + (t->level2_size << t->q) * sizeof (uint32_t)
271    + (t->level3_size << t->p) * sizeof (ELEMENT);
272  t->result_size = (last_offset + 3) & ~3ul;
273  t->result = (char *) xmalloc (t->result_size);
274
275  level1_offset =
276    5 * sizeof (uint32_t);
277  level2_offset =
278    5 * sizeof (uint32_t)
279    + t->level1_size * sizeof (uint32_t);
280  level3_offset =
281    5 * sizeof (uint32_t)
282    + t->level1_size * sizeof (uint32_t)
283    + (t->level2_size << t->q) * sizeof (uint32_t);
284
285  ((uint32_t *) t->result)[0] = t->q + t->p;
286  ((uint32_t *) t->result)[1] = t->level1_size;
287  ((uint32_t *) t->result)[2] = t->p;
288  ((uint32_t *) t->result)[3] = (1 << t->q) - 1;
289  ((uint32_t *) t->result)[4] = (1 << t->p) - 1;
290
291  for (i = 0; i < t->level1_size; i++)
292    ((uint32_t *) (t->result + level1_offset))[i] =
293      (t->level1[i] == ~((uint32_t) 0)
294       ? 0
295       : (t->level1[i] << t->q) * sizeof (uint32_t) + level2_offset);
296
297  for (i = 0; i < (t->level2_size << t->q); i++)
298    ((uint32_t *) (t->result + level2_offset))[i] =
299      (t->level2[i] == ~((uint32_t) 0)
300       ? 0
301       : (t->level2[i] << t->p) * sizeof (ELEMENT) + level3_offset);
302
303  for (i = 0; i < (t->level3_size << t->p); i++)
304    ((ELEMENT *) (t->result + level3_offset))[i] = t->level3[i];
305
306  if (last_offset < t->result_size)
307    memset (t->result + last_offset, 0, t->result_size - last_offset);
308
309  if (t->level1_alloc > 0)
310    free (t->level1);
311  if (t->level2_alloc > 0)
312    free (t->level2);
313  if (t->level3_alloc > 0)
314    free (t->level3);
315}
316#endif
317
318#undef TABLE
319#undef ELEMENT
320#undef DEFAULT
321#undef ITERATE
322#undef NO_FINALIZE
Note: See TracBrowser for help on using the repository browser.