-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiset.c
302 lines (276 loc) · 6.65 KB
/
iset.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Sets:
*
* Expanding hash tables with a key.
*/
#include "system.h"
#include "assert.h"
#include "iset.h"
#if !defined(USEPRIMESIZES)
static int sizes[] = {
4, 8, 16, 32,
64, 128, 256, 512,
1024, 2048, 4096, 8192,
16*1024, 32*1024, 64*1024, 128*1024,
256*1024, 512*1024, 1024*1024, 2*1024*1024,
4*1024*1024, 8*1024*1024, 16*1024*1024 };
#define Hash(key,sc) (ISetHASH(key) & (sc->size - 1))
#else
static int sizes[] = {
5, 7, 17, 31,
67, 131, 257, 521,
1031, 2053, 4099, 8093,
16193, 32377, 65557, 131071,
262187, 524869, 1048829, 2097223,
4194371, 8388697, 16777291 };
#define Hash(key, sc) (ISetHASH(key) % sc->size)
#endif
#define MAXFILL(x) (((x) * 17) / 20)
/*
* Turning this on will cause the package to self-check on every (modifying)
* operation. The package runs very slowly when this is enabled.
*/
#undef DEBUGSC
#ifdef DEBUGSC
static void CheckOutHashTable();
#define CHECKOUTHASHTABLE(sc) CheckOutHashTable(sc)
#else
#define CHECKOUTHASHTABLE(sc)
#endif
/* Return a new, empty ISet */
ISet ISetCreate()
{
register int i;
register ISet sc;
sc = (ISet) vmMalloc(sizeof(ISetRecord));
sc->size = sizes[0];
sc->maxCount = MAXFILL(sc->size);
sc->count = 0;
sc->table = (ISetTEPtr) vmMalloc((unsigned) sc->size * sizeof(ISetTE));
for (i = 0; i < sc->size; i++) {
sc->table[i].key = (int)NULL;
}
CHECKOUTHASHTABLE(sc);
return sc;
}
/* Return a new, empty ISet */
ISet ISetCreateN(int n)
{
register int i;
register ISet sc;
sc = (ISet) vmMalloc(sizeof(ISetRecord));
for (i = 0; sizes[i] < n; i++) ;
sc->size = sizes[i];
sc->maxCount = MAXFILL(sc->size);
sc->count = 0;
sc->table = (ISetTEPtr) vmMalloc((unsigned) sc->size * sizeof(ISetTE));
for (i = 0; i < sc->size; i++) {
sc->table[i].key = (int)NULL;
}
CHECKOUTHASHTABLE(sc);
return sc;
}
void ISetDestroy(sc)
register ISet sc;
{
vmFree((char *)sc->table);
vmFree((char *)sc);
}
/* Expand the hash table. Each element in the table is re-hashed and entered
* in the new table. */
static void ExpandHashTable(ISet sc)
{
register ISetTE *nh, *oe, *ne;
register int oldHashTableSize = sc->size, i;
register ISetDomainType key;
int index;
for (i = 0; sizes[i] <= oldHashTableSize; i++) ;
sc->size = sizes[i];
sc->maxCount = MAXFILL(sc->size);
nh = (ISetTEPtr) vmMalloc((unsigned)(sc->size * sizeof(ISetTE)));
for (i = 0; i < sc->size; i++) nh[i].key = (int)NULL;
for (i = 0; i < oldHashTableSize; i++) {
oe = &sc->table[i];
key = oe->key;
if (key == (int)NULL) continue;
index = Hash(key, sc);
while (1) {
ne = &nh[index];
if (ne->key == (int)NULL) {
ne->key = oe->key;
break;
} else {
assert(ne->key !=key);
index++;
if (index >= sc->size) index = 0;
}
}
}
vmFree((char *)sc->table);
sc->table = nh;
CHECKOUTHASHTABLE(sc);
}
/* Is key in the set sc */
int ISetMember(sc, key)
register ISet sc;
register ISetDomainType key;
{
register int index = Hash(key, sc);
register ISetTEPtr e;
CHECKOUTHASHTABLE(sc);
while (1) {
e = &sc->table[index];
if (e->key == (int)NULL) { /* we did not find it */
return 0;
} else if (ISetCOMPARE(e->key, key)) {
return 1;
}
if (++index >= sc->size) index = 0;
}
}
/* Select a random (the first) key from the set sc */
ISetDomainType ISetSelect(sc)
register ISet sc;
{
register int index = 0;
register ISetTEPtr e;
CHECKOUTHASHTABLE(sc);
while (1) {
e = &sc->table[index];
if (e->key != (int)NULL) { /* we found it */
return e->key;
}
if (++index >= sc->size) return (int)NULL;
}
}
void ISetDeleteAll(register ISet sc)
{
int i;
for (i = 0; i < sc->size; i++) {
sc->table[i].key = (int)NULL;
}
sc->count = 0;
}
/* Insert the key in sc. If the key already exists, do nothing. */
void ISetInsert(sc, key)
register ISet sc;
register ISetDomainType key;
{
register int index;
register ISetTEPtr e;
if (sc->count >= sc->maxCount) ExpandHashTable(sc);
index = Hash(key, sc);
while (1) {
e = &sc->table[index];
if (e->key == (int)NULL) { /* put it here */
e->key = key;
sc->count++;
CHECKOUTHASHTABLE(sc);
return;
} else if (ISetCOMPARE(e->key, key)) {
CHECKOUTHASHTABLE(sc);
return;
}
if (++index >= sc->size) index = 0;
}
}
/* Remove the entry, if it is there */
void ISetDelete(sc, key)
register ISet sc;
register ISetDomainType key;
{
register int index = Hash(key, sc);
register ISetTEPtr e;
while (1) {
e = &sc->table[index];
if (e->key == (int)NULL) { /* we did not find it */
CHECKOUTHASHTABLE(sc);
return;
}
if (ISetCOMPARE(e->key, key)) {
/* Found it, now remove it */
sc->count--;
e->key = (int)NULL;
while (1) {
/* rehash until we reach nil again */
if (++index >= sc->size) index = 0;
e = & sc->table[index];
key = e->key;
if (key == (int)NULL) {
CHECKOUTHASHTABLE(sc);
return;
}
/* rehashing is done by removing then reinserting */
e->key = (int)NULL;
sc->count--;
ISetInsert(sc, key);
}
}
if (++index >= sc->size) index = 0;
}
}
#ifdef DEBUGSC
/* DEBUGGING: Print the set */
void ISetPrint(sc)
register ISet sc;
{
ISetDomainType key;
int index;
printf(
"\nDump of sc @ 0x%05x, %d entr%s, current max %d\nIndex\tKey\n",
(int)sc, sc->count, sc->count == 1 ? "y" : "ies", sc->maxCount);
for (index = 0; index < sc->size; index++) {
key = sc->table[index].key;
if (key)
printf("%3d\t%8d\n", index, key);
}
}
/* Make sure that the hash table is internally consistent:
* every key is findable,
* count reflects the number of elements
*/
static void CheckOutHashTable(sc)
register ISet sc;
{
register int i;
register ISetTEPtr realElement, e;
register int index, firstIndex, count;
count = 0;
for (i = 0; i < sc->size; i++) {
realElement = &sc->table[i];
if (realElement->key != NULL) {
count++;
index = Hash(realElement->key, sc);
firstIndex = index;
while (1) {
e = &sc->table[index];
if (e->key == NULL) { /* we did not find it */
break;
} else if (ISetCOMPARE(e->key, realElement->key)) {
break;
} else {
index++;
if (index >= sc->size) index = 0;
if (index == firstIndex) {
index = -1;
break;
}
}
}
if (index == -1 || !ISetCOMPARE(e->key, realElement->key)) {
FIX THIS
fprintf(stderr,
"Sc problem: Key 0x%x, rightIndex %d, realIndex %d\n",
realElement->key, firstIndex, index);
ISetPrint(sc);
}
}
}
if (count != sc->count) {
fprintf(stderr,
"Sc problem: Should have %d entries, but found %d.\n", sc->count,
count);
ISetPrint(sc);
}
}
#endif