2 Hash table library using separate chaining
4 > Created (Julienne Walker): August 7, 2005
5 > Modified (Julienne Walker): August 11, 2005
6 Added a cast for malloc to enable clean
20 typedef struct jsw_node {
21 void *key; /* Key used for searching */
22 void *item; /* Actual content of a node */
23 struct jsw_node *next; /* Next link in the chain */
26 typedef struct jsw_head {
27 jsw_node_t *first; /* First link in the chain */
28 size_t size; /* Length of the chain */
32 jsw_head_t **table; /* Dynamic chained hash table */
33 size_t size; /* Current item count */
34 size_t capacity; /* Current table size */
35 size_t curri; /* Current index for traversal */
36 jsw_node_t *currl; /* Current link for traversal */
37 hash_f hash; /* User defined key hash function */
38 cmp_f cmp; /* User defined key comparison function */
39 keydup_f keydup; /* User defined key copy function */
40 itemdup_f itemdup; /* User defined item copy function */
41 keyrel_f keyrel; /* User defined key delete function */
42 itemrel_f itemrel; /* User defined item delete function */
45 static jsw_node_t *new_node ( void *key, void *item, jsw_node_t *next )
47 jsw_node_t *node = (jsw_node_t *)malloc ( sizeof *node );
59 static jsw_head_t *new_chain ( void )
61 jsw_head_t *chain = (jsw_head_t *)malloc ( sizeof *chain );
73 Create a new hash table with a capacity of size, and
74 user defined functions for handling keys and items.
76 Returns: An empty hash table, or NULL on failure.
78 jsw_hash_t *jsw_hnew ( size_t size, hash_f hash, cmp_f cmp,
79 keydup_f keydup, itemdup_f itemdup,
80 keyrel_f keyrel, itemrel_f itemrel )
82 jsw_hash_t *htab = (jsw_hash_t *)malloc ( sizeof *htab );
88 htab->table = (jsw_head_t **)malloc ( size * sizeof *htab->table );
90 if ( htab->table == NULL ) {
95 /* Empty chains have no head */
96 for ( i = 0; i < size; i++ )
97 htab->table[i] = NULL;
100 htab->capacity = size;
105 htab->keydup = keydup;
106 htab->itemdup = itemdup;
107 htab->keyrel = keyrel;
108 htab->itemrel = itemrel;
113 /* Release all memory used by the hash table */
114 void jsw_hdelete ( jsw_hash_t *htab )
118 /* Release each chain individually */
119 for ( i = 0; i < htab->capacity; i++ ) {
120 jsw_node_t *save, *it;
122 if ( htab->table[i] == NULL )
125 it = htab->table[i]->first;
127 for ( ; it != NULL; it = save ) {
129 htab->keyrel ( it->key );
130 htab->itemrel ( it->item );
134 free ( htab->table[i] );
137 /* Release the hash table */
138 free ( htab->table );
143 Find an item with the selected key
145 Returns: The item, or NULL if not found
147 void *jsw_hfind ( jsw_hash_t *htab, void *key )
149 unsigned h = htab->hash ( key ) % htab->capacity;
151 /* Search the chain only if it exists */
152 if ( htab->table[h] != NULL ) {
153 jsw_node_t *it = htab->table[h]->first;
155 for ( ; it != NULL; it = it->next ) {
156 if ( htab->cmp ( key, it->key ) == 0 )
165 Insert an item with the selected key
167 Returns: non-zero for success, zero for failure
169 int jsw_hinsert ( jsw_hash_t *htab, void *key, void *item )
171 unsigned h = htab->hash ( key ) % htab->capacity;
172 void *dupkey, *dupitem;
173 jsw_node_t *new_item;
175 /* Disallow duplicate keys */
176 if ( jsw_hfind ( htab, key ) != NULL )
179 /* Attempt to create a new item */
180 dupkey = htab->keydup ( key );
181 dupitem = htab->itemdup ( item );
183 new_item = new_node ( dupkey, dupitem, NULL );
185 if ( new_item == NULL )
188 /* Create a chain if the bucket is empty */
189 if ( htab->table[h] == NULL ) {
190 htab->table[h] = new_chain();
192 if ( htab->table[h] == NULL ) {
193 htab->keyrel ( new_item->key );
194 htab->itemrel ( new_item->item );
200 /* Insert at the front of the chain */
201 new_item->next = htab->table[h]->first;
202 htab->table[h]->first = new_item;
204 ++htab->table[h]->size;
211 Remove an item with the selected key
213 Returns: non-zero for success, zero for failure
215 int jsw_herase ( jsw_hash_t *htab, void *key )
217 unsigned h = htab->hash ( key ) % htab->capacity;
218 jsw_node_t *save, *it;
220 if ( htab->table[h] == NULL )
223 it = htab->table[h]->first;
225 /* Remove the first node in the chain? */
226 if ( htab->cmp ( key, it->key ) == 0 ) {
227 htab->table[h]->first = it->next;
229 /* Release the node's memory */
230 htab->keyrel ( it->key );
231 htab->itemrel ( it->item );
234 /* Remove the chain if it's empty */
235 if ( htab->table[h]->first == NULL ) {
236 free ( htab->table[h] );
237 htab->table[h] = NULL;
240 --htab->table[h]->size;
243 /* Search for the node */
244 while ( it->next != NULL ) {
245 if ( htab->cmp ( key, it->next->key ) == 0 )
252 if ( it->next == NULL )
256 it->next = it->next->next;
258 /* Release the node's memory */
259 htab->keyrel ( save->key );
260 htab->itemrel ( save->item );
263 --htab->table[h]->size;
266 /* Erasure invalidates traversal markers */
275 Grow or shrink the table, this is a slow operation
277 Returns: non-zero for success, zero for failure
279 int jsw_hresize ( jsw_hash_t *htab, size_t new_size )
281 jsw_hash_t *new_htab;
285 /* Build a new hash table, then assign it to the old one */
286 new_htab = jsw_hnew ( new_size, htab->hash, htab->cmp,
287 htab->keydup, htab->itemdup, htab->keyrel, htab->itemrel );
289 if ( new_htab == NULL )
292 for ( i = 0; i < htab->capacity; i++ ) {
293 if ( htab->table[i] == NULL )
296 for ( it = htab->table[i]->first; it != NULL; it = it->next )
297 jsw_hinsert ( new_htab, it->key, it->item );
300 /* A hash table holds copies, so release the old table */
301 jsw_hdelete ( htab );
307 /* Reset the traversal markers to the beginning */
308 void jsw_hreset ( jsw_hash_t *htab )
315 /* Find the first non-empty bucket */
316 for ( i = 0; i < htab->capacity; i++ ) {
317 if ( htab->table[i] != NULL )
323 /* Set the link marker if the table was not empty */
324 if ( i != htab->capacity )
325 htab->currl = htab->table[i]->first;
328 /* Traverse forward by one key */
329 int jsw_hnext ( jsw_hash_t *htab )
331 if ( htab->currl != NULL ) {
332 htab->currl = htab->currl->next;
334 /* At the end of the chain? */
335 if ( htab->currl == NULL ) {
336 /* Find the next chain */
337 while ( ++htab->curri < htab->capacity ) {
338 if ( htab->table[htab->curri] != NULL )
342 /* No more chains? */
343 if ( htab->curri == htab->capacity )
346 htab->currl = htab->table[htab->curri]->first;
353 /* Get the current key */
354 const void *jsw_hkey ( jsw_hash_t *htab )
356 return htab->currl != NULL ? htab->currl->key : NULL;
359 /* Get the current item */
360 void *jsw_hitem ( jsw_hash_t *htab )
362 return htab->currl != NULL ? htab->currl->item : NULL;
365 /* Current number of items in the table */
366 size_t jsw_hsize ( jsw_hash_t *htab )
371 /* Total allowable number of items without resizing */
372 size_t jsw_hcapacity ( jsw_hash_t *htab )
374 return htab->capacity;
377 /* Get statistics for the hash table */
378 jsw_hstat_t *jsw_hstat ( jsw_hash_t *htab )
381 double sum = 0, used = 0;
384 /* No stats for an empty table */
385 if ( htab->size == 0 )
388 stat = (jsw_hstat_t *)malloc ( sizeof *stat );
394 stat->schain = (size_t)-1;
396 for ( i = 0; i < htab->capacity; i++ ) {
397 if ( htab->table[i] != NULL ) {
398 sum += htab->table[i]->size;
400 ++used; /* Non-empty buckets */
402 if ( htab->table[i]->size > stat->lchain )
403 stat->lchain = htab->table[i]->size;
405 if ( htab->table[i]->size < stat->schain )
406 stat->schain = htab->table[i]->size;
410 stat->load = used / htab->capacity;
411 stat->achain = sum / used;