-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathsc_hash.cc
More file actions
413 lines (377 loc) · 12.1 KB
/
sc_hash.cc
File metadata and controls
413 lines (377 loc) · 12.1 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/** \file sc_hash.cc
* Dynamic hashing, after CACM April 1988 pp 446-457, by Per-Ake Larson.
* Coded into C, with minor code improvements, and with hsearch(3) interface,
* by ejp@ausmelb.oz, Jul 26, 1988: 13:16;
* also, hcreate/hdestroy routines added to simulate hsearch(3).
*/
#include <sc_hash.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* constants */
#define HASH_NULL (Hash_TableP)NULL
#define SEGMENT_SIZE 256
#define SEGMENT_SIZE_SHIFT 8 /* log2(SEGMENT_SIZE) */
#define PRIME1 37
#define PRIME2 1048583
#define MAX_LOAD_FACTOR 5
/* macro function definitions */
#define SC_HASH_Table_new() new Hash_Table
#define SC_HASH_Table_destroy(x) delete x
#define SC_HASH_Element_new() new Element
#define SC_HASH_Element_destroy(x) delete x
/* Macros for fast arithmetic, relying on powers of 2 */
#define MUL(x,y) ((x) << (y##_SHIFT))
#define DIV(x,y) ((x) >> (y##_SHIFT))
#define MOD(x,y) ((x) & ((y)-1))
/* typedefs */
typedef unsigned long Address;
typedef struct Element * ElementP;
typedef struct Hash_Table * Hash_TableP;
/* Internal routines */
Address SC_HASHhash( char *, Hash_TableP );
static void SC_HASHexpand_table( Hash_TableP );
# ifdef HASH_STATISTICS
static long HashAccesses, HashCollisions;
# endif
/// find entry in given hash table
void * SC_HASHfind( Hash_TableP t, char * s ) {
struct Element e;
struct Element * ep;
e.key = s;
e.symbol = 0; /* initialize to 0 - 25-Apr-1994 - kcm */
e.type = '*';
ep = SC_HASHsearch( t, &e, HASH_FIND );
return( ep ? ep->data : 0 );
}
/// insert entry into given hash table
void SC_HASHinsert( Hash_TableP t, char * s, void * data ) {
struct Element e, *e2;
e.key = s;
e.data = data;
e.symbol = 0;
e.type = '*';
e2 = SC_HASHsearch( t, &e, HASH_INSERT );
if( e2 ) {
fprintf( stderr, "%s: Redeclaration of %s\n", __func__, s );
}
}
/// create a hash table
Hash_TableP SC_HASHcreate( unsigned count ) {
unsigned int i;
Hash_TableP table;
/*
** Adjust Count to be nearest higher power of 2,
** minimum SEGMENT_SIZE, then convert into segments.
*/
i = SEGMENT_SIZE;
while( i < count ) {
i <<= 1;
}
count = DIV( i, SEGMENT_SIZE );
table = ( Hash_TableP ) SC_HASH_Table_new();
table->SegmentCount = table->p = table->KeyCount = 0;
/*
** First initialize directory to 0\'s
** DIRECTORY_SIZE must be same as in header
*/
for( i = 0; i < DIRECTORY_SIZE; i++ ) {
table->Directory[i] = 0;
}
/*
** Allocate initial 'i' segments of buckets
*/
for( i = 0; i < count; i++ ) {
table->Directory[i] = new struct Element * [SEGMENT_SIZE];
for( int h = 0; h < SEGMENT_SIZE; h++ ) { // initialize to NULL
table->Directory[i][h] = 0;
}
}
table->SegmentCount = count;
table->maxp = MUL( count, SEGMENT_SIZE );
table->MinLoadFactor = 1;
table->MaxLoadFactor = MAX_LOAD_FACTOR;
# ifdef DEBUG
fprintf( stderr,
"[HASHcreate] table %x count %d maxp %d SegmentCount %d\n",
table,
count,
table->maxp,
table->SegmentCount );
# endif
# ifdef HASH_STATISTICS
HashAccesses = HashCollisions = 0;
# endif
return( table );
}
/** initialize pointer to beginning of hash table so we can
* step through it on repeated calls to HASHlist - DEL */
void SC_HASHlistinit( Hash_TableP table, HashEntry * he ) {
he->i = he->j = 0;
he->p = 0;
he->table = table;
he->type = '*';
he->e = 0;
}
void SC_HASHlistinit_by_type( Hash_TableP table, HashEntry * he, char type ) {
he->i = he->j = 0;
he->p = 0;
he->table = table;
he->type = type;
he->e = 0;
}
/** provide a way to step through the hash */
struct Element * SC_HASHlist( HashEntry * he ) {
int i2 = he->i;
int j2 = he->j;
struct Element ** s;
he->e = 0;
for( he->i = i2; he->i < he->table->SegmentCount; he->i++ ) {
/* test probably unnecessary */
if( ( s = he->table->Directory[he->i] ) != NULL ) {
for( he->j = j2; he->j < SEGMENT_SIZE; he->j++ ) {
if( !he->p ) {
he->p = s[he->j];
}
/* if he->p is defined, prepare to return it (by
setting it to he->e) and begin looking for a new value
for he->p
*/
while( he->p && he->type != '*' && he->type != he->p->type ) {
he->p = he->p->next;
}
if( he->p ) {
if( he->e ) {
return( he->e );
}
he->e = he->p;
he->p = he->p->next;
}
/* avoid incrementing he->j by returning here */
if( he->p ) {
return( he->e );
}
}
j2 = 0;
}
}
/* if he->e was set then it is last one */
return( he->e );
}
/// destroy all elements in given table, then the table itself
void SC_HASHdestroy( Hash_TableP table ) {
struct Element ** s;
struct Element * p, *q;
if( table != HASH_NULL ) {
unsigned int i, j;
for( i = 0; i < table->SegmentCount; i++ ) {
/* test probably unnecessary */
if( ( s = table->Directory[i] ) != NULL ) {
for( j = 0; j < SEGMENT_SIZE; j++ ) {
p = s[j];
while( p != NULL ) {
q = p->next;
SC_HASH_Element_destroy( p );
p = q;
}
}
/* free((char *) table->Directory[i]);*/
delete [] table->Directory[i];
}
}
SC_HASH_Table_destroy( table );
# if defined(HASH_STATISTICS) && defined(DEBUG)
fprintf( stderr, "[hdestroy] Accesses %ld Collisions %ld\n", HashAccesses, HashCollisions );
# endif
}
}
/// search table for 'item', perform 'action' (find/insert/delete)
struct Element * SC_HASHsearch( Hash_TableP table, const struct Element * item, Action action ) {
Address h;
struct Element ** CurrentSegment;
int SegmentIndex;
int SegmentDir;
struct Element ** p;
struct Element * q;
struct Element * deleteme;
# ifdef HASH_STATISTICS
HashAccesses++;
# endif
h = SC_HASHhash( item->key, table );
SegmentDir = ( int ) DIV( h, SEGMENT_SIZE );
SegmentIndex = ( int ) MOD( h, SEGMENT_SIZE );
/*
** valid segment ensured by HASHhash()
*/
CurrentSegment = table->Directory[SegmentDir];
p = CurrentSegment + SegmentIndex;
q = *p;
/*
** Follow collision chain
** Now and after we finish this loop
** p = &element, and
** q = element
*/
while( q != NULL && strcmp( q->key, item->key ) ) {
p = &q->next;
q = *p;
# ifdef HASH_STATISTICS
HashCollisions++;
# endif
}
/* at this point, we have either found the element or it doesn't exist */
switch( action ) {
case HASH_FIND:
return( ( struct Element * )q );
case HASH_DELETE:
if( !q ) {
return( 0 );
}
/* at this point, element exists and action == DELETE */
deleteme = q;
*p = q->next;
/*STRINGfree(deleteme->key);*/
SC_HASH_Element_destroy( deleteme );
--table->KeyCount;
// TODO - we shouldn't be returning this pointer - it invites a
// USE_AFTER_FREE error. Could we just replace SC_HASH completely
// with one of the C++ containers?
return( deleteme ); /* of course, user shouldn't deref this! */
case HASH_INSERT:
/* if trying to insert it (twice), let them know */
if( q != NULL ) {
return( q ); /* was return(0);!!!!!?!?! */
}
/* at this point, element does not exist and action == INSERT */
q = ( ElementP ) SC_HASH_Element_new();
*p = q; /* link into chain */
/*
** Initialize new element
*/
/* I don't see the point of copying the key!!!! */
/* q->key = STRINGcopy(item->key);*/
q->key = item->key;
q->data = item->data;
q->symbol = item->symbol;
q->type = item->type;
q->next = NULL;
/*
** table over-full?
*/
if( ++table->KeyCount / MUL( table->SegmentCount, SEGMENT_SIZE ) > table->MaxLoadFactor ) {
SC_HASHexpand_table( table ); /* doesn't affect q */
}
}
return( ( struct Element * )0 ); /* was return (Element)q */
}
/*
** Internal routines
*/
Address SC_HASHhash( char * Key, Hash_TableP table ) {
Address h, address;
unsigned char * k = ( unsigned char * )Key;
h = 0;
/*
** Convert string to integer
*/
while( *k ) {
h = h * PRIME1 ^ ( *k++ - ' ' );
}
h %= PRIME2;
address = MOD( h, table->maxp );
if( address < table->p ) {
address = MOD( h, ( table->maxp << 1 ) ); /* h % (2*table->maxp) */
}
return( address );
}
static void SC_HASHexpand_table( Hash_TableP table ) {
struct Element ** OldSegment, **NewSegment;
struct Element * Current, **Previous, **LastOfNew;
if( table->maxp + table->p < MUL( DIRECTORY_SIZE, SEGMENT_SIZE ) ) {
/*
** Locate the bucket to be split
*/
Address NewAddress;
int OldSegmentIndex, NewSegmentIndex;
int OldSegmentDir, NewSegmentDir;
OldSegmentDir = DIV( table->p, SEGMENT_SIZE );
OldSegment = table->Directory[OldSegmentDir];
OldSegmentIndex = MOD( table->p, SEGMENT_SIZE );
/*
** Expand address space; if necessary create a new segment
*/
NewAddress = table->maxp + table->p;
NewSegmentDir = ( int ) DIV( NewAddress, SEGMENT_SIZE );
NewSegmentIndex = ( int ) MOD( NewAddress, SEGMENT_SIZE );
if( NewSegmentIndex == 0 ) {
table->Directory[NewSegmentDir] = new struct Element * [SEGMENT_SIZE];
for( int h = 0; h < SEGMENT_SIZE; h++ ) { // initialize to NULL
table->Directory[NewSegmentDir][h] = 0;
}
}
NewSegment = table->Directory[NewSegmentDir];
/*
** Adjust state variables
*/
table->p++;
if( table->p == table->maxp ) {
table->maxp <<= 1;
table->p = 0;
}
table->SegmentCount++;
/*
** Relocate records to the new bucket
*/
Previous = &OldSegment[OldSegmentIndex];
Current = *Previous;
LastOfNew = &NewSegment[NewSegmentIndex];
*LastOfNew = NULL;
while( Current != NULL ) {
if( SC_HASHhash( Current->key, table ) == NewAddress ) {
/*
** Attach it to the end of the new chain
*/
*LastOfNew = Current;
/*
** Remove it from old chain
*/
*Previous = Current->next;
LastOfNew = &Current->next;
Current = Current->next;
*LastOfNew = NULL;
} else {
/*
** leave it on the old chain
*/
Previous = &Current->next;
Current = Current->next;
}
}
}
}
/* for testing sc_hash */
#ifdef HASHTEST
struct Element e1, e2, e3, *e;
struct Hash_Table * t;
HashEntry he;
main() {
e1.key = "foo";
e1.data = ( char * )1;
e2.key = "bar";
e2.data = ( char * )2;
e3.key = "herschel";
e3.data = ( char * )3;
t = SC_HASHcreate( 100 );
e = SC_HASHsearch( t, &e1, HASH_INSERT );
e = SC_HASHsearch( t, &e2, HASH_INSERT );
e = SC_HASHsearch( t, &e3, HASH_INSERT );
SC_HASHlistinit( t, &he );
for( ;; ) {
e = SC_HASHlist( &he );
if( !e ) {
exit( 0 );
}
printf( "found key %s, data %d\n", e->key, ( int )e->data );
}
}
#endif