7 void hash_new( hash **hash_ppt, size_t size )
9 /* Martin A. Hansen, June 2008 */
11 /* Initialize a new generic hash structure. */
13 hash *new_hash = *hash_ppt;
14 size_t table_size = 0;
16 new_hash = mem_get( sizeof( new_hash ) );
18 table_size = 1 << size; /* table_size = ( 2 ** size ) */
20 new_hash->table_size = table_size;
21 new_hash->mask = table_size - 1;
22 new_hash->table = mem_get( sizeof( hash_elem * ) * table_size );
23 new_hash->elem_count = 0;
29 //void hash_add( hash *hash_pt, char *key, void *val )
31 // /* Martin A. Hansen, June 2008 */
33 // /* Add a new hash element consisting of a key/value pair to an existing hash. */
35 // hash_elem *old_elem = NULL;
36 // hash_elem *new_elem = NULL;
37 // size_t hash_index = 0;
39 // if ( ( old_elem = hash_get_elem( hash_pt, key ) ) != NULL )
41 // old_elem->val = val;
45 // new_elem = mem_get( sizeof( hash_elem ) );
47 // hash_index = ( hash_key( key ) & hash_pt->mask );
49 // new_elem->key = mem_clone( key, strlen( key ) );
50 // new_elem->val = val;
51 // new_elem->next = hash_pt->table[ hash_index ];
53 // hash_pt->table[ hash_index ] = new_elem;
54 // hash_pt->elem_count++;
59 //void *hash_get( struct hash *myhash, char *key )
61 // /* Martin A. Hansen, June 2008 */
63 // /* Lookup a key in a given hash and return the value - or NULL if not found. */
65 // struct hash_elem *bucket;
67 // bucket = myhash->table[ ( hash_key( key ) & myhash->mask ) ];
69 // while ( bucket != NULL )
71 // if ( strcmp( bucket->key, key ) == 0 ) {
72 // return bucket->val;
75 // bucket = bucket->next;
82 //struct hash_elem *hash_get_elem( struct hash *myhash, char *key )
84 // /* Martin A. Hansen, June 2008 */
86 // /* Lookup a key in a given hash and return the hash element - or NULL if not found. */
88 // struct hash_elem *bucket;
90 // bucket = myhash->table[ ( hash_key( key ) & myhash->mask ) ];
92 // while ( bucket != NULL )
94 // if ( strcmp( bucket->key, key ) == 0 ) {
98 // bucket = bucket->next;
105 //bool hash_del( struct hash *myhash, char *key )
107 // /* Martin A. Hansen, June 2008 */
109 // /* Remove key/value pair from a given hash. */
110 // /* Returns true if a remove was successful. */
112 // struct hash_elem *bucket;
114 // bucket = myhash->table[ ( hash_key( key ) & myhash->mask ) ];
116 // while ( bucket != NULL )
118 // if ( strcmp( bucket->key, key ) == 0 )
120 // myhash->elem_count--;
124 // bucket = bucket->next;
131 //void hash_destroy( struct hash *myhash )
133 // /* Martin A. Hansen, June 2008 */
135 // /* Deallocate memory for hash and all hash elements. */
138 // struct hash_elem *bucket;
140 // for ( i = 0; i < myhash->table_size; i++ )
142 // for ( bucket = myhash->table[ i ]; bucket != NULL; bucket = bucket->next )
144 // mem_free( &bucket->key );
145 //// mem_free( bucket->val );
146 // mem_free( &bucket );
150 // mem_free( ( void * ) &myhash->table );
151 // mem_free( ( void * ) &myhash );
155 //uint hash_key( char *string )
157 // /* Martin A. Hansen, June 2008 */
159 // /* Hash function that generates a hash key, */
160 // /* based on the Jim Kent's stuff. */
162 // char *key = string;
166 // while ( ( c = *key++ ) != '\0' ) {
167 // result += ( result << 3 ) + c;
174 //void hash_collision_stats( struct hash *myhash )
176 // /* Martin A. Hansen, June 2008 */
178 // /* Output some collision stats for a given hash. */
180 // /* Use with biotools: ... | plot_histogram -k Col -x */
184 // struct hash_elem *bucket;
186 // for ( i = 0; i < myhash->table_size; i++ )
190 // for ( bucket = myhash->table[ i ]; bucket != NULL; bucket = bucket->next ) {
194 // printf( "Col: %d\n---\n", col );