1- /* "$Id: sc_hash.cc,v 3.0.1.2 1997/11/05 22:33:50 sauderd DP3.1 $"; */
2-
3- /*
1+ /* * \file sc_hash.cc
42 * Dynamic hashing, after CACM April 1988 pp 446-457, by Per-Ake Larson.
53 * Coded into C, with minor code improvements, and with hsearch(3) interface,
64 * by ejp@ausmelb.oz, Jul 26, 1988: 13:16;
1311#include < string.h>
1412#include < sc_memmgr.h>
1513
16- /* ************/
1714/* constants */
18- /* ************/
19-
2015#define HASH_NULL (Hash_TableP)NULL
21-
2216#define SEGMENT_SIZE 256
2317#define SEGMENT_SIZE_SHIFT 8 /* log2(SEGMENT_SIZE) */
2418#define PRIME1 37
2519#define PRIME2 1048583
2620#define MAX_LOAD_FACTOR 5
2721
28- /* ***********/
29- /* typedefs */
30- /* ***********/
31-
32- typedef unsigned long Address;
33-
34- /* *****************************/
3522/* macro function definitions */
36- /* *****************************/
37-
38- /*
39- ** Fast arithmetic, relying on powers of 2
40- */
41-
42- #define MUL (x,y ) ((x) << (y##_SHIFT))
43- #define DIV (x,y ) ((x) >> (y##_SHIFT))
44- #define MOD (x,y ) ((x) & ((y)-1 ))
45-
4623#define SC_HASH_Table_new () new Hash_Table
4724#define SC_HASH_Table_destroy (x ) delete x
4825#define SC_HASH_Element_new () new Element
4926#define SC_HASH_Element_destroy (x ) delete x
5027
28+ /* Macros for fast arithmetic, relying on powers of 2 */
29+ #define MUL (x,y ) ((x) << (y##_SHIFT))
30+ #define DIV (x,y ) ((x) >> (y##_SHIFT))
31+ #define MOD (x,y ) ((x) & ((y)-1 ))
32+
33+ /* typedefs */
34+ typedef unsigned long Address;
5135typedef struct Element * ElementP;
5236typedef struct Hash_Table * Hash_TableP;
5337
54- /*
55- ** Internal routines
56- */
57-
38+ /* Internal routines */
5839Address SC_HASHhash ( char *, Hash_TableP );
5940static void SC_HASHexpand_table ( Hash_TableP );
6041
6142# ifdef HASH_STATISTICS
6243static long HashAccesses, HashCollisions;
6344# endif
6445
65- void *
66- SC_HASHfind ( Hash_TableP t, char * s ) {
46+ // / find entry in given hash table
47+ void * SC_HASHfind ( Hash_TableP t, char * s ) {
6748 struct Element e;
6849 struct Element * ep;
6950
@@ -73,8 +54,8 @@ SC_HASHfind( Hash_TableP t, char * s ) {
7354 return ( ep ? ep->data : 0 );
7455}
7556
76- void
77- SC_HASHinsert ( Hash_TableP t, char * s, void * data ) {
57+ // / insert entry into given hash table
58+ void SC_HASHinsert ( Hash_TableP t, char * s, void * data ) {
7859 struct Element e, *e2 ;
7960
8061 e.key = s;
@@ -86,8 +67,8 @@ SC_HASHinsert( Hash_TableP t, char * s, void * data ) {
8667 }
8768}
8869
89- Hash_TableP
90- SC_HASHcreate ( unsigned count ) {
70+ // / create a hash table
71+ Hash_TableP SC_HASHcreate ( unsigned count ) {
9172 unsigned int i;
9273 Hash_TableP table;
9374
@@ -138,29 +119,26 @@ SC_HASHcreate( unsigned count ) {
138119 return ( table );
139120}
140121
141- /* initialize pointer to beginning of hash table so we can step through it */
142- /* on repeated calls to HASHlist - DEL */
143- void
144- SC_HASHlistinit ( Hash_TableP table, HashEntry * he ) {
122+ /* * initialize pointer to beginning of hash table so we can
123+ * step through it on repeated calls to HASHlist - DEL */
124+ void SC_HASHlistinit ( Hash_TableP table, HashEntry * he ) {
145125 he->i = he->j = 0 ;
146126 he->p = 0 ;
147127 he->table = table;
148128 he->type = ' *' ;
149129 he->e = 0 ;
150130}
151131
152- void
153- SC_HASHlistinit_by_type ( Hash_TableP table, HashEntry * he, char type ) {
132+ void SC_HASHlistinit_by_type ( Hash_TableP table, HashEntry * he, char type ) {
154133 he->i = he->j = 0 ;
155134 he->p = 0 ;
156135 he->table = table;
157136 he->type = type;
158137 he->e = 0 ;
159138}
160139
161- /* provide a way to step through the hash */
162- struct Element *
163- SC_HASHlist ( HashEntry * he ) {
140+ /* * provide a way to step through the hash */
141+ struct Element * SC_HASHlist ( HashEntry * he ) {
164142 int i2 = he->i ;
165143 int j2 = he->j ;
166144 struct Element ** s;
@@ -202,8 +180,8 @@ SC_HASHlist( HashEntry * he ) {
202180 return ( he->e );
203181}
204182
205- void
206- SC_HASHdestroy ( Hash_TableP table ) {
183+ // / destroy all elements in given table, then the table itself
184+ void SC_HASHdestroy ( Hash_TableP table ) {
207185 struct Element ** s;
208186 struct Element * p, *q;
209187
@@ -226,16 +204,13 @@ SC_HASHdestroy( Hash_TableP table ) {
226204 }
227205 SC_HASH_Table_destroy ( table );
228206# if defined(HASH_STATISTICS) && defined(DEBUG)
229- fprintf ( stderr,
230- " [hdestroy] Accesses %ld Collisions %ld\n " ,
231- HashAccesses,
232- HashCollisions );
207+ fprintf ( stderr, " [hdestroy] Accesses %ld Collisions %ld\n " , HashAccesses, HashCollisions );
233208# endif
234209 }
235210}
236211
237- struct Element *
238- SC_HASHsearch ( Hash_TableP table, const struct Element * item, Action action ) {
212+ // / search table for 'item', perform 'action' (find/insert/delete)
213+ struct Element * SC_HASHsearch ( Hash_TableP table, const struct Element * item, Action action ) {
239214 Address h;
240215 struct Element ** CurrentSegment;
241216 int SegmentIndex;
@@ -317,8 +292,7 @@ SC_HASHsearch( Hash_TableP table, const struct Element * item, Action action ) {
317292** Internal routines
318293*/
319294
320- Address
321- SC_HASHhash ( char * Key, Hash_TableP table ) {
295+ Address SC_HASHhash ( char * Key, Hash_TableP table ) {
322296 Address h, address;
323297 register unsigned char * k = ( unsigned char * )Key;
324298
@@ -337,9 +311,7 @@ SC_HASHhash( char * Key, Hash_TableP table ) {
337311 return ( address );
338312}
339313
340- static
341- void
342- SC_HASHexpand_table ( Hash_TableP table ) {
314+ static void SC_HASHexpand_table ( Hash_TableP table ) {
343315 struct Element ** OldSegment, **NewSegment;
344316 struct Element * Current, **Previous, **LastOfNew;
345317
@@ -407,7 +379,7 @@ SC_HASHexpand_table( Hash_TableP table ) {
407379 }
408380}
409381
410- /* following code is for testing hash package */
382+ /* for testing sc_hash */
411383#ifdef HASHTEST
412384struct Element e1 , e2 , e3 , *e;
413385struct Hash_Table * t;
0 commit comments