-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathhash.c
More file actions
566 lines (526 loc) · 16.1 KB
/
hash.c
File metadata and controls
566 lines (526 loc) · 16.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*
* 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).
*
* move to local hash table, cleanup abstraction and storage allocation;
* convert to ANSII C.
* by snc (clark@cme.nbs.gov), 10-Mar-1989
*
* these routines simulate hsearch(3) and family, with the important
* difference that the hash table is dynamic - can grow indefinitely
* beyond its original size (as supplied to hcreate()).
*
* Performance appears to be comparable to that of hsearch(3).
* The 'source-code' options referred to in hsearch(3)'s 'man' page
* are not implemented; otherwise functionality is identical.
*
* Compilation controls:
* HASH_DEBUG controls some informative traces, mainly for debugging.
* HASH_STATISTICS causes HashAccesses and HashCollisions to be maintained;
* when combined with HASH_DEBUG, these are displayed by hdestroy().
*
* Problems & fixes to ejp@ausmelb.oz. WARNING: relies on pre-processor
* concatenation property, in probably unnecessary code 'optimisation'.
*
* snc: fixed concatenation for ANSII C, 10-Mar-1989
*
* $Log: hash.c,v $
* Revision 1.8 1997/10/22 16:36:49 sauderd
* Changed the use and definitions of the compiler macros MUL and DIV. They
* seem to be defined and used only within hash.h and hash.c
*
* Revision 1.7 1997/01/21 19:19:51 dar
* made C++ compatible
*
* Revision 1.6 1994/03/23 20:06:51 libes
* botched free in hash destroy
*
* Revision 1.5 1993/10/15 18:49:55 libes
* CADDETC certified
*
* Revision 1.4 1992/08/18 18:27:10 libes
* changed DEBUG to HASH_DEBUG
*
* Revision 1.3 1992/08/18 17:16:22 libes
* rm'd extraneous error messages
*
* Revision 1.2 1992/05/31 08:37:18 libes
* multiple files
*
* Revision 1.1 1992/05/28 03:56:55 libes
* Initial revision
*
* Revision 1.4 1992/05/14 10:15:04 libes
* don't remember
*
* Revision 1.3 1992/02/12 07:06:49 libes
* y
* y
* do sub/supertype
*
* Revision 1.2 1992/02/09 00:51:02 libes
* does ref/use correctly
*
* Revision 1.1 1992/02/05 08:34:24 libes
* Initial revision
*
* Revision 1.0.1.1 1992/01/22 02:40:04 libes
* copied from ~pdes
*
* Revision 1.9 1992/01/22 02:26:58 libes
* added code to test hash package
*
* Revision 1.8 1991/09/17 02:53:23 libes
* fixed bug in HASHcopy (new hash tables were coming out empty)
*
* Revision 1.7 1991/08/30 16:34:36 libes
* fixed HASHlist to use state from parameter instead of static
*
* Revision 1.6 1991/08/13 22:19:39 libes
* forgot to declare s, s2, pp, and q. Oops.
*
* Revision 1.5 1991/08/06 19:03:07 libes
* fixed bugs relating to my previous addition (delete function)
* added HASHcopy to support DICT_copy via OBJcopy
*
* Revision 1.4 1991/07/19 03:57:46 libes
* added action HASH_DELETE
* made action HASH_INSERT return failure upon duplicate entry
*
* Revision 1.3 1991/02/26 19:40:45 libes
* Added functions for visiting every member of the hash table, to duplicate
* similar functionality of linked lists. Note that current implementation
* can only walk through one hash table at a time. This can be improved, but
* the application code doesn't currently need such a feature.
*
* Revision 1.2 1990/09/06 11:06:56 clark
* BPR 2.1 alpha
*
* Revision 1.1 90/06/11 16:44:43 clark
* Initial revision
*
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "sc_memmgr.h"
#include "express/hash.h"
/*
** Internal routines
*/
static inline Address HASHhash( char *, Hash_Table );
static void HASHexpand_table( Hash_Table );
/*
** Local data
*/
# ifdef HASH_STATISTICS
static long HashAccesses, HashCollisions;
# endif
/*
** Code
*/
void
HASHinitialize() {
}
Hash_Table
HASHcreate( unsigned count ) {
unsigned i;
Hash_Table 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_SHIFT );
table = HASH_Table_new();
#if 0
table->in_use = 0;
#endif
table->SegmentCount = table->p = table->KeyCount = 0;
/*
** Allocate initial 'i' segments of buckets
*/
for( i = 0; i < count; i++ )
CALLOC( table->Directory[i], SEGMENT_SIZE, Element )
/*, "segment in HASHcreate");*/
table->SegmentCount = count;
table->maxp = MUL( count, SEGMENT_SIZE_SHIFT );
table->MinLoadFactor = 1;
table->MaxLoadFactor = MAX_LOAD_FACTOR;
# ifdef HASH_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
HASHlistinit( Hash_Table table, HashEntry * he ) {
he->i = he->j = 0;
he->p = 0;
he->table = table;
he->type = '*';
#if 0
table->in_use = 1;
#endif
}
void
HASHlistinit_by_type( Hash_Table table, HashEntry * he, char type ) {
he->i = he->j = 0;
he->p = 0;
he->table = table;
he->type = type;
#if 0
table->in_use = 1;
#endif
}
#if 0
/* if you don't step to the end, you can clear the flag this way */
void
HASHlistend( HashEntry * he ) {
he->table->in_use = 0;
}
#endif
/* provide a way to step through the hash */
Element
HASHlist( HashEntry * he ) {
int i2 = he->i;
int j2 = he->j;
Segment 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
*/
retry:
if( he->p ) {
if( ( he->type != '*' ) &&
( he->type != he->p->type ) ) {
he->p = he->p->next;
goto retry;
}
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 */
#if 0
he->table->in_use = 0;
#endif
return( he->e );
}
#if 0
/* this verifies no one else is walking through the table that we might screw up */
/* it should be called before adding, deleting or destroying a table */
HASH_in_use( Hash_Table table, char * action ) {
fprintf( stderr, "HASH: attempted to %s but hash table in use\n", action );
}
#endif
void
HASHdestroy( Hash_Table table ) {
Segment s;
Element p, q;
if( table != HASH_NULL ) {
unsigned int i, j;
#if 0
if( table->in_use ) {
HASH_in_use( table, "destroy hash table" );
}
#endif
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;
HASH_Element_destroy( p );
p = q;
}
}
sc_free( table->Directory[i] );
}
}
HASH_Table_destroy( table );
# if defined(HASH_STATISTICS) && defined(HASH_DEBUG)
fprintf( stderr,
"[hdestroy] Accesses %ld Collisions %ld\n",
HashAccesses,
HashCollisions );
# endif
}
}
Element
HASHsearch( Hash_Table table, Element item, Action action ) {
Address h;
Segment CurrentSegment;
int SegmentIndex;
int SegmentDir;
Segment p;
Element q;
Element deleteme;
assert( table != HASH_NULL ); /* Kinder really than return(NULL); */
# ifdef HASH_STATISTICS
HashAccesses++;
# endif
h = HASHhash( item->key, table );
SegmentDir = DIV( h, SEGMENT_SIZE_SHIFT );
SegmentIndex = MOD( h, SEGMENT_SIZE );
/*
** valid segment ensured by HASHhash()
*/
CurrentSegment = table->Directory[SegmentDir];
assert( CurrentSegment != NULL ); /* bad failure if tripped */
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( ( Element )q );
case HASH_DELETE:
if( !q ) {
return( 0 );
}
/* at this point, element exists and action == DELETE */
#if 0
if( table->in_use ) {
HASH_in_use( table, "insert element" );
}
#endif
deleteme = q;
*p = q->next;
/*STRINGfree(deleteme->key);*/
HASH_Element_destroy( deleteme );
--table->KeyCount;
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);!!!!!?!?! */
}
#if 0
if( table->in_use ) {
HASH_in_use( table, "delete element" );
}
#endif
/* at this point, element does not exist and action == INSERT */
q = 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_SHIFT ) > table->MaxLoadFactor ) {
HASHexpand_table( table ); /* doesn't affect q */
}
}
return( ( Element )0 ); /* was return (Element)q */
}
/*
** Internal routines
*/
static inline Address HASHhash( char * Key, Hash_Table table ) {
Address h, address;
register unsigned char * k = ( unsigned char * )Key;
h = 0;
/*
** Convert string to integer
*/
/*SUPPRESS 112*/
assert( Key );
while( *k )
/*SUPPRESS 8*/ { /*SUPPRESS 112*/
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 HASHexpand_table( Hash_Table table ) {
Segment OldSegment, NewSegment;
Element Current, *Previous, *LastOfNew;
if( table->maxp + table->p < MUL( DIRECTORY_SIZE, SEGMENT_SIZE_SHIFT ) ) {
Address NewAddress;
int OldSegmentIndex, NewSegmentIndex;
int OldSegmentDir, NewSegmentDir;
/*
** Locate the bucket to be split
*/
OldSegmentDir = DIV( table->p, SEGMENT_SIZE_SHIFT );
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 = DIV( NewAddress, SEGMENT_SIZE_SHIFT );
NewSegmentIndex = MOD( NewAddress, SEGMENT_SIZE );
if( NewSegmentIndex == 0 ) {
CALLOC( table->Directory[NewSegmentDir], SEGMENT_SIZE, Element );
}
/* "segment in HASHexpand_table");*/
NewSegment = table->Directory[NewSegmentDir];
/*
** Adjust state variables
*/
table->p++;
if( table->p == table->maxp ) {
table->maxp <<= 1; /* table->maxp *= 2 */
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( 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;
}
}
}
}
/* make a complete copy of a hash table */
/* Note that individual objects are shallow-copied. OBJcopy is not called! */
/* But then, it isn't called when objects are inserted/deleted so this seems */
/* reasonable - DEL */
Hash_Table
HASHcopy( Hash_Table oldtable ) {
Hash_Table newtable;
Segment s, s2;
Element * pp; /* old element */
Element * qq; /* new element */
unsigned int i, j;
newtable = HASH_Table_new();
for( i = 0; i < oldtable->SegmentCount; i++ ) {
CALLOC( newtable->Directory[i], SEGMENT_SIZE, Element );
/* "segment in HASHcopy");*/
}
newtable->p = oldtable->p;
newtable->SegmentCount = oldtable->SegmentCount;
newtable->maxp = oldtable->maxp;
newtable->MinLoadFactor = oldtable->MinLoadFactor;
newtable->MaxLoadFactor = oldtable->MaxLoadFactor;
newtable->KeyCount = oldtable->KeyCount;
for( i = 0; i < oldtable->SegmentCount; i++ ) {
/* test probably unnecessary */
if( ( s = oldtable->Directory[i] ) != NULL ) {
s2 = newtable->Directory[i];
for( j = 0; j < SEGMENT_SIZE; j++ ) {
qq = &s2[j];
for( pp = &s[j]; *pp; pp = &( *pp )->next ) {
*qq = HASH_Element_new();
/* (*qq)->key = STRINGcopy((*pp)->key);*/
/* I really doubt it is necessary to copy the key!!! */
( *qq )->key = ( *pp )->key;
( *qq )->data = ( *pp )->data;
( *qq )->symbol = ( *pp )->symbol;
( *qq )->type = ( *pp )->type;
( *qq )->next = NULL;
qq = &( ( *qq )->next );
}
}
}
}
return( newtable );
}
/* following code is for testing hash package */
#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 = HASHcreate( 100 );
e = HASHsearch( t, &e1, HASH_INSERT );
e = HASHsearch( t, &e2, HASH_INSERT );
e = HASHsearch( t, &e3, HASH_INSERT );
HASHlistinit( t, &he );
for( ;; ) {
e = HASHlist( &he );
if( !e ) {
exit( 0 );
}
fprintf( stderr, "found key %s, data %d\n", e->key, ( int )e->data );
}
}
#endif