1 /* Martin Asser Hansen (mail@maasha.dk) Copyright (C) 2008 - All right reserved */
9 void hash_new( hash **hash_ppt, size_t size )
11 /* Martin A. Hansen, June 2008 */
13 /* Initialize a new generic hash structure. */
15 hash *new_hash = *hash_ppt;
16 size_t table_size = 0;
18 new_hash = mem_get( sizeof( new_hash ) );
20 table_size = 1 << size; /* table_size = ( 2 ** size ) */
22 new_hash->table_size = table_size;
23 new_hash->mask = table_size - 1;
24 new_hash->table = mem_get( sizeof( hash_elem * ) * table_size );
25 new_hash->elem_count = 0;
31 //void hash_add( hash *hash_pt, char *key, void *val )
33 // /* Martin A. Hansen, June 2008 */
35 // /* Add a new hash element consisting of a key/value pair to an existing hash. */
37 // hash_elem *old_elem = NULL;
38 // hash_elem *new_elem = NULL;
39 // size_t hash_index = 0;
41 // if ( ( old_elem = hash_get_elem( hash_pt, key ) ) != NULL )
43 // old_elem->val = val;
47 // new_elem = mem_get( sizeof( hash_elem ) );
49 // hash_index = ( hash_key( key ) & hash_pt->mask );
51 // new_elem->key = mem_clone( key, strlen( key ) );
52 // new_elem->val = val;
53 // new_elem->next = hash_pt->table[ hash_index ];
55 // hash_pt->table[ hash_index ] = new_elem;
56 // hash_pt->elem_count++;
61 //void *hash_get( struct hash *myhash, char *key )
63 // /* Martin A. Hansen, June 2008 */
65 // /* Lookup a key in a given hash and return the value - or NULL if not found. */
67 // struct hash_elem *bucket;
69 // bucket = myhash->table[ ( hash_key( key ) & myhash->mask ) ];
71 // while ( bucket != NULL )
73 // if ( strcmp( bucket->key, key ) == 0 ) {
74 // return bucket->val;
77 // bucket = bucket->next;
84 //struct hash_elem *hash_get_elem( struct hash *myhash, char *key )
86 // /* Martin A. Hansen, June 2008 */
88 // /* Lookup a key in a given hash and return the hash element - or NULL if not found. */
90 // struct hash_elem *bucket;
92 // bucket = myhash->table[ ( hash_key( key ) & myhash->mask ) ];
94 // while ( bucket != NULL )
96 // if ( strcmp( bucket->key, key ) == 0 ) {
100 // bucket = bucket->next;
107 //bool hash_del( struct hash *myhash, char *key )
109 // /* Martin A. Hansen, June 2008 */
111 // /* Remove key/value pair from a given hash. */
112 // /* Returns true if a remove was successful. */
114 // struct hash_elem *bucket;
116 // bucket = myhash->table[ ( hash_key( key ) & myhash->mask ) ];
118 // while ( bucket != NULL )
120 // if ( strcmp( bucket->key, key ) == 0 )
122 // myhash->elem_count--;
126 // bucket = bucket->next;
133 //void hash_destroy( struct hash *myhash )
135 // /* Martin A. Hansen, June 2008 */
137 // /* Deallocate memory for hash and all hash elements. */
140 // struct hash_elem *bucket;
142 // for ( i = 0; i < myhash->table_size; i++ )
144 // for ( bucket = myhash->table[ i ]; bucket != NULL; bucket = bucket->next )
146 // mem_free( &bucket->key );
147 //// mem_free( bucket->val );
148 // mem_free( &bucket );
152 // mem_free( ( void * ) &myhash->table );
153 // mem_free( ( void * ) &myhash );
157 //uint hash_key( char *string )
159 // /* Martin A. Hansen, June 2008 */
161 // /* Hash function that generates a hash key, */
162 // /* based on the Jim Kent's stuff. */
164 // char *key = string;
168 // while ( ( c = *key++ ) != '\0' ) {
169 // result += ( result << 3 ) + c;
176 //void hash_collision_stats( struct hash *myhash )
178 // /* Martin A. Hansen, June 2008 */
180 // /* Output some collision stats for a given hash. */
182 // /* Use with biotools: ... | plot_histogram -k Col -x */
186 // struct hash_elem *bucket;
188 // for ( i = 0; i < myhash->table_size; i++ )
192 // for ( bucket = myhash->table[ i ]; bucket != NULL; bucket = bucket->next ) {
196 // printf( "Col: %d\n---\n", col );