5 Hash table library using separate chaining
7 > Created (Julienne Walker): August 7, 2005
8 > Modified (Julienne Walker): August 11, 2005
10 This code is in the public domain. Anyone may
11 use it or change it in any way that they see
12 fit. The author assumes no responsibility for
13 damages incurred through use of the original
14 code or any variations thereof.
16 It is requested, but not required, that due
17 credit is given to the original author and
18 anyone who has modified the code through
19 a header comment, such as this one.
23 typedef struct jsw_hash jsw_hash_t;
25 /* Application specific hash function */
26 typedef unsigned (*hash_f) ( const void *key );
28 /* Application specific key comparison function */
29 typedef int (*cmp_f) ( const void *a, const void *b );
31 /* Application specific key copying function */
32 typedef void *(*keydup_f) ( const void *key );
34 /* Application specific data copying function */
35 typedef void *(*itemdup_f) ( const void *item );
37 /* Application specific key deletion function */
38 typedef void (*keyrel_f) ( void *key );
40 /* Application specific data deletion function */
41 typedef void (*itemrel_f) ( void *item );
43 typedef struct jsw_hstat {
44 double load; /* Table load factor: (M chains)/(table size) */
45 double achain; /* Average chain length */
46 size_t lchain; /* Longest chain */
47 size_t schain; /* Shortest non-empty chain */
51 Create a new hash table with a capacity of size, and
52 user defined functions for handling keys and items.
54 Returns: An empty hash table, or NULL on failure.
56 jsw_hash_t *jsw_hnew ( size_t size, hash_f hash, cmp_f cmp,
57 keydup_f keydup, itemdup_f itemdup,
58 keyrel_f keyrel, itemrel_f itemrel );
60 /* Release all memory used by the hash table */
61 void jsw_hdelete ( jsw_hash_t *htab );
64 Find an item with the selected key
66 Returns: The item, or NULL if not found
68 void *jsw_hfind ( jsw_hash_t *htab, void *key );
71 Insert an item with the selected key
73 Returns: non-zero for success, zero for failure
75 int jsw_hinsert ( jsw_hash_t *htab, void *key, void *item );
78 Remove an item with the selected key
80 Returns: non-zero for success, zero for failure
82 int jsw_herase ( jsw_hash_t *htab, void *key );
85 Grow or shrink the table, this is a slow operation
87 Returns: non-zero for success, zero for failure
89 int jsw_hresize ( jsw_hash_t *htab, size_t new_size );
91 /* Reset the traversal markers to the beginning */
92 void jsw_hreset ( jsw_hash_t *htab );
94 /* Traverse forward by one key */
95 int jsw_hnext ( jsw_hash_t *htab );
97 /* Get the current key */
98 const void *jsw_hkey ( jsw_hash_t *htab );
100 /* Get the current item */
101 void *jsw_hitem ( jsw_hash_t *htab );
103 /* Current number of items in the table */
104 size_t jsw_hsize ( jsw_hash_t *htab );
106 /* Total allowable number of items without resizing */
107 size_t jsw_hcapacity ( jsw_hash_t *htab );
109 /* Get statistics for the hash table */
110 jsw_hstat_t *jsw_hstat ( jsw_hash_t *htab );