1 | /* Copyright (C) 1999 Free Software Foundation
|
---|
2 |
|
---|
3 | This file is part of libgcj.
|
---|
4 |
|
---|
5 | This software is copyrighted work licensed under the terms of the
|
---|
6 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
---|
7 | details. */
|
---|
8 |
|
---|
9 | #include <stdio.h>
|
---|
10 | #include <stdlib.h>
|
---|
11 |
|
---|
12 | typedef struct trie_node
|
---|
13 | {
|
---|
14 | int key;
|
---|
15 | int level;
|
---|
16 | int position;
|
---|
17 | union
|
---|
18 | {
|
---|
19 | int value;
|
---|
20 | struct trie_node *node;
|
---|
21 | } u[16];
|
---|
22 | } trie_node;
|
---|
23 |
|
---|
24 | trie_node *
|
---|
25 | make_node ()
|
---|
26 | {
|
---|
27 | trie_node *node = (trie_node *) malloc (sizeof(trie_node));
|
---|
28 | if (node == NULL)
|
---|
29 | abort();
|
---|
30 | return node;
|
---|
31 | }
|
---|
32 |
|
---|
33 | trie_node *
|
---|
34 | make_leaf_node ()
|
---|
35 | {
|
---|
36 | trie_node *node = make_node ();
|
---|
37 | int i = 16;
|
---|
38 | while (--i >= 0)
|
---|
39 | node->u[i].value = -1;
|
---|
40 | return node;
|
---|
41 | }
|
---|
42 |
|
---|
43 | trie_node *
|
---|
44 | make_branch_node ()
|
---|
45 | {
|
---|
46 | trie_node *node = make_node ();
|
---|
47 | int i = 16;
|
---|
48 | while (--i >= 0)
|
---|
49 | node->u[i].node = NULL;
|
---|
50 | return node;
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | trie_node *table = NULL;
|
---|
55 |
|
---|
56 | void
|
---|
57 | enter (int key, int value)
|
---|
58 | {
|
---|
59 | trie_node **ptr = &table;
|
---|
60 | int shift = 12;
|
---|
61 | for (; shift > 0; shift -= 4)
|
---|
62 | {
|
---|
63 | if (*ptr == NULL)
|
---|
64 | {
|
---|
65 | *ptr = make_branch_node ();
|
---|
66 | (*ptr)->key = key & (0xFFFF << (shift + 4));
|
---|
67 | (*ptr)->level = shift / 4;
|
---|
68 | }
|
---|
69 | ptr = &(*ptr)->u[(key >> shift) & 0xF].node;
|
---|
70 | }
|
---|
71 | if (*ptr == NULL)
|
---|
72 | {
|
---|
73 | *ptr = make_leaf_node ();
|
---|
74 | (*ptr)->key = key & 0xFFF0;
|
---|
75 | (*ptr)->level = 0;
|
---|
76 | }
|
---|
77 | if ((*ptr)->u[key & 0xF].value != -1
|
---|
78 | && (*ptr)->u[key & 0xF].value != value)
|
---|
79 | fprintf(stderr, "duplicate value for key: %d, %d!\n", key, value);
|
---|
80 | (*ptr)->u[key & 0xF].value = value;
|
---|
81 | }
|
---|
82 |
|
---|
83 | int assigned = 0;
|
---|
84 |
|
---|
85 | void
|
---|
86 | assign (trie_node *node, int level)
|
---|
87 | {
|
---|
88 | int i;
|
---|
89 | if (node == NULL)
|
---|
90 | return;
|
---|
91 | if (node->level != level)
|
---|
92 | abort();
|
---|
93 | node->position = assigned;
|
---|
94 | assigned++;
|
---|
95 | if (level == 0)
|
---|
96 | return;
|
---|
97 | for (i = 0; i < 16; i++)
|
---|
98 | {
|
---|
99 | assign (node->u[i].node, level-1);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | int next_node_index_toprint = 0;
|
---|
104 |
|
---|
105 | void
|
---|
106 | print (trie_node *node, int index, int level, FILE *out)
|
---|
107 | {
|
---|
108 | int i;
|
---|
109 | if (node->key != index || node->level != level)
|
---|
110 | abort();
|
---|
111 | if (level == 0) /* leaf node */
|
---|
112 | {
|
---|
113 | for (i = 0; i < 16; i++)
|
---|
114 | {
|
---|
115 | int node_index = index | (i << (level * 4));
|
---|
116 | if (node_index < next_node_index_toprint)
|
---|
117 | abort();
|
---|
118 | if (node->u[i].value == -1)
|
---|
119 | fprintf (out, " /* key: 0x%x */ 0xffff,\n", node_index);
|
---|
120 | else
|
---|
121 | fprintf (out, " /* key: 0x%x */ 0x%x,\n",
|
---|
122 | node_index, node->u[i].value);
|
---|
123 | next_node_index_toprint = node_index + 1;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | else
|
---|
127 | {
|
---|
128 | for (i = 0; i < 16; i++)
|
---|
129 | {
|
---|
130 | int node_index = index | (i << (level * 4));
|
---|
131 | fprintf (out, " /* branch: 0x%0*x%.*s */ ",
|
---|
132 | 4 - level, node_index >> ( 4 * level),
|
---|
133 | level, "XXXX");
|
---|
134 | if (node->u[i].node == NULL)
|
---|
135 | fprintf (out, "0,\n");
|
---|
136 | else
|
---|
137 | fprintf (out, "%d,\n", 16 * node->u[i].node->position);
|
---|
138 | }
|
---|
139 |
|
---|
140 | for (i = 0; i < 16; i++)
|
---|
141 | {
|
---|
142 | int node_index = index | (i << (level * 4));
|
---|
143 | if (node->u[i].node != NULL)
|
---|
144 | print (node->u[i].node, node_index, level-1, out);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | void
|
---|
150 | print_table (char *name, FILE *out)
|
---|
151 | {
|
---|
152 | assign (table, 3);
|
---|
153 |
|
---|
154 | fprintf(out, "/* This file is automatically generated. */\n");
|
---|
155 | fprintf(out, "unsigned short %s[] = {\n", name);
|
---|
156 | print (table, 0x0000, 3, out);
|
---|
157 | fprintf(out, "};\n");
|
---|
158 | }
|
---|
159 |
|
---|
160 | #if 0
|
---|
161 | int
|
---|
162 | main (int argc, char **argv)
|
---|
163 | {
|
---|
164 | int count = 0;
|
---|
165 | for (;;)
|
---|
166 | {
|
---|
167 | int key, value;
|
---|
168 | int i = scanf (" 0x%x 0x%x", &key, &value);
|
---|
169 | if (i < 2)
|
---|
170 | break;
|
---|
171 | count++;
|
---|
172 | enter (key, value);
|
---|
173 | }
|
---|
174 | return 0;
|
---|
175 | }
|
---|
176 | #endif
|
---|