-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate_hash_table.c
227 lines (188 loc) · 5.5 KB
/
state_hash_table.c
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
This file is part of Hyacc, a LR(0)/LALR(1)/LR(1)/LR(k) parser generator.
Copyright (C) 2007 Xin Chen. [email protected]
Hyacc is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Hyacc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Hyacc; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* state_hash_table.c
*
* This hash table stores state numbers. It hashes a state by
* a function of the rule index and marker position of the
* state's core configurations.
*
* This hash table is used to expidite the the process of fi-
* nding a same or compatible state in the function addTrans-
* itionStates2New().
*
* This is an alternative of the function isExistingState(),
* which uses linear search in all the states, and is slower.
*
* For example, for C.y, in -O0 (no optimization), there are
* 1605 states in the parsing machine, so from the first to
* the last state as they are added, in average there will be
* sum_{k=1_to_n}(k/2) = n(n+1)/4 searches, where n = 1605.
* This is 644,407.
*
* But using this state hash table, in average there are 5.02
* states per list (from StateHashTbl_dump() output), so the
* cost is (1 + 5.02) * 1605 = 9,662, where 1 is the cost of
* getting the hash value for a state.
*
* This is 644,407/9,662 = 67 times faster.
*
* @Author: Xin Chen
* @Created on: 3/3/2006
* @Last modified: 3/21/2007
*/
#include "y.h"
struct StateTableNode {
State * state;
struct StateTableNode * next;
};
typedef struct StateTableNode StateNode;
typedef struct {
int count;
StateNode * next;
} StateHashTblNode;
#define SHT_SIZE 997 /* State hash table size */
StateHashTblNode StateHashTbl[SHT_SIZE];
StateNode * createStateNode(State * s)
{
StateNode * n;
HYY_NEW(n, StateNode, 1);
n->state = s;
n->next = NULL;
return n;
}
void destroyStateNode(StateNode * n)
{
free(n);
}
void initStateHashTbl()
{
int i;
for (i = 0; i < SHT_SIZE; i ++) {
StateHashTbl[i].count = 0;
StateHashTbl[i].next = NULL;
}
}
static int getStateHashVal(State * s)
{
int i, sum = 0;
for (i = 0; i < s->core_config_count; i ++) {
sum = (sum + s->config[i]->ruleID * 97 +
s->config[i]->marker * 7 + i) % SHT_SIZE;
}
return sum;
}
extern BOOL in_lanetracing;
/*
* Search the state hash table for state s.
* If not found, insert it and return NULL.
* else, return the found state.
*/
State * searchStateHashTbl(State * s, int * is_compatible)
{
int v = getStateHashVal(s);
StateNode * n = StateHashTbl[v].next;
StateNode * n_prev;
(* is_compatible) = 0; // default to 0 - false.
if (n == NULL) {
StateHashTbl[v].next = createStateNode(s);
StateHashTbl[v].count = 1;
return NULL;
}
while (n != NULL) {
n_prev = n;
if (isSameState(n->state, s) == TRUE) {
return n->state;
}
if (USE_COMBINE_COMPATIBLE_STATES) {
if (isCompatibleStates(n->state, s) == TRUE) {
combineCompatibleStates(n->state, s);
(* is_compatible) = 1;
return n->state;
}
}
n = n->next;
}
// n == NULL, s does not exist. insert at end.
n_prev->next = createStateNode(s);
StateHashTbl[v].count ++;
return NULL;
}
/*
* Search the state hash table for state s.
* If not found, insert it and return NULL.
* else, return the found state.
*
* Is similar to searchStateHashTbl, but does not use
* weak compatibility.
*/
State * searchSameStateHashTbl(State * s)
{
int v = getStateHashVal(s);
StateNode * n = StateHashTbl[v].next;
StateNode * n_prev;
if (n == NULL) {
StateHashTbl[v].next = createStateNode(s);
StateHashTbl[v].count = 1;
return NULL;
}
while (n != NULL) {
n_prev = n;
if (isSameState(n->state, s) == TRUE) {
return n->state;
}
n = n->next;
}
// n == NULL, s does not exist. insert at end.
n_prev->next = createStateNode(s);
StateHashTbl[v].count ++;
return NULL;
}
/*
* load factor: number of entry / hash table size.
* hash table cell usage:
* number of used hash table cells / hash table size.
*/
void StateHashTbl_dump()
{
int i, states_count = 0, list_count = 0;
StateNode * n;
yyprintf("\n--state hash table--\n");
yyprintf("-----------------------\n");
yyprintf("cell | count | state\n");
yyprintf("-----------------------\n");
for (i = 0; i < SHT_SIZE; i ++) {
if (StateHashTbl[i].count == 0) continue;
list_count ++;
states_count += StateHashTbl[i].count;
yyprintf3("[%3d] (count=%d) : ", i, StateHashTbl[i].count);
if ((n = StateHashTbl[i].next) != NULL) {
yyprintf2("%d", n->state->state_no);
n = n->next;
while (n != NULL) {
yyprintf2(", %d", n->state->state_no);
n = n->next;
}
}
yyprintf("\n");
}
yyprintf4("%d states, %d lists, in average %.2f states/list.\n",
states_count, list_count,
((double) states_count)/list_count);
yyprintf3("load factor: %.2f, hash table cell usage: %.2f\n",
((double) states_count)/SHT_SIZE,
((double) list_count)/SHT_SIZE);
}