summaryrefslogtreecommitdiff
path: root/libjava/gnu/gcj/convert/make-trie.c
blob: fe2d7892641b352c3deeedcad207ee20daac3ba5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Copyright (C) 1999  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

#include <stdio.h>
#include <stdlib.h>
#include "make-trie.h"

typedef struct trie_node
{
  int key;
  int level;
  int position;
  union
  {
    int value;
    struct trie_node *node;
  } u[16];
} trie_node;

trie_node *
make_node ()
{
  trie_node *node = (trie_node *) malloc (sizeof(trie_node));
  if (node == NULL)
    abort();
  return node;
}

trie_node *
make_leaf_node ()
{
  trie_node *node = make_node ();
  int i = 16;
  while (--i >= 0)
    node->u[i].value = -1;
  return node;
}

trie_node *
make_branch_node ()
{
  trie_node *node = make_node ();
  int i = 16;
  while (--i >= 0)
    node->u[i].node = NULL;
  return node;
}


trie_node *table = NULL;

void
enter (int key, int value)
{
  trie_node **ptr = &table;
  int shift = 12;
  for (; shift > 0;  shift -= 4)
    {
      if (*ptr == NULL)
	{
	  *ptr = make_branch_node ();
	  (*ptr)->key = key & (0xFFFF << (shift + 4));
	  (*ptr)->level = shift / 4;
	}
      ptr = &(*ptr)->u[(key >> shift) & 0xF].node;
    }
  if (*ptr == NULL)
    {
      *ptr = make_leaf_node ();
      (*ptr)->key = key & 0xFFF0;
      (*ptr)->level = 0;
    }
  if ((*ptr)->u[key & 0xF].value != -1
      && (*ptr)->u[key & 0xF].value != value)
    fprintf(stderr, "duplicate value for key: %d, %d!\n", key, value);
  (*ptr)->u[key & 0xF].value = value;
}

int assigned = 0;

void
assign (trie_node *node, int level)
{
  int i;
  if (node == NULL)
    return;
  if (node->level != level)
    abort();
  node->position = assigned;
  assigned++;
  if (level == 0)
    return;
  for (i = 0;  i < 16;  i++)
    {
      assign (node->u[i].node, level-1);
    }
}

int next_node_index_toprint = 0;

void
print (trie_node *node, int index, int level, FILE *out)
{
  int i;
  if (node->key != index || node->level != level)
    abort();
  if (level == 0) /* leaf node */
    {
      for (i = 0;  i < 16;  i++)
	{
	  int node_index = index | (i << (level * 4));
	  if (node_index < next_node_index_toprint)
	    abort();
	  if (node->u[i].value == -1)
	    fprintf (out, " /* key: 0x%x */ 0xffff,\n", node_index);
	  else
	    fprintf (out, " /* key: 0x%x */ 0x%x,\n",
		     node_index, node->u[i].value);
	  next_node_index_toprint = node_index + 1;
	}
    }
  else
    {
      for (i = 0;  i < 16;  i++)
	{
	  int node_index = index | (i << (level * 4));
	  fprintf (out, " /* branch: 0x%0*x%.*s */ ",
		  4 - level, node_index  >> ( 4 * level),
		  level, "XXXX");
	  if (node->u[i].node == NULL)
	    fprintf (out, "0,\n");
	  else
	    fprintf (out, "%d,\n", 16 * node->u[i].node->position);
	}

      for (i = 0;  i < 16;  i++)
	{
	  int node_index = index | (i << (level * 4));
	  if (node->u[i].node != NULL)
	    print (node->u[i].node, node_index, level-1, out);
	}
    }
}

void
print_table (char *name, FILE *out)
{
  assign (table, 3);

  fprintf(out, "/* This file is automatically generated. */\n");
  fprintf(out, "unsigned short %s[] = {\n", name);
  print (table, 0x0000, 3, out);
  fprintf(out, "};\n");
}

#if 0
int
main (int argc, char **argv)
{
  int count = 0;
  for (;;)
    {
      int key, value;
      int i = scanf (" 0x%x 0x%x", &key, &value);
      if (i < 2)
	break;
      count++;
      enter (key, value);
    }
  return 0;
}
#endif