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.
31 typedef struct jsw_hash jsw_hash_t;
33 /* Application specific hash function */
34 typedef unsigned (*hash_f) ( const void *key );
36 /* Application specific key comparison function */
37 typedef int (*cmp_f) ( const void *a, const void *b );
39 /* Application specific key copying function */
40 typedef void *(*keydup_f) ( const void *key );
42 /* Application specific data copying function */
43 typedef void *(*itemdup_f) ( const void *item );
45 /* Application specific key deletion function */
46 typedef void (*keyrel_f) ( void *key );
48 /* Application specific data deletion function */
49 typedef void (*itemrel_f) ( void *item );
51 typedef struct jsw_hstat {
52 double load; /* Table load factor: (M chains)/(table size) */
53 double achain; /* Average chain length */
54 size_t lchain; /* Longest chain */
55 size_t schain; /* Shortest non-empty chain */
59 Create a new hash table with a capacity of size, and
60 user defined functions for handling keys and items.
62 Returns: An empty hash table, or NULL on failure.
64 jsw_hash_t *jsw_hnew ( size_t size, hash_f hash, cmp_f cmp,
65 keydup_f keydup, itemdup_f itemdup,
66 keyrel_f keyrel, itemrel_f itemrel );
68 /* Release all memory used by the hash table */
69 void jsw_hdelete ( jsw_hash_t *htab );
72 Find an item with the selected key
74 Returns: The item, or NULL if not found
76 void *jsw_hfind ( jsw_hash_t *htab, void *key );
79 Insert an item with the selected key
81 Returns: non-zero for success, zero for failure
83 int jsw_hinsert ( jsw_hash_t *htab, void *key, void *item );
86 Remove an item with the selected key
88 Returns: non-zero for success, zero for failure
90 int jsw_herase ( jsw_hash_t *htab, void *key );
93 Grow or shrink the table, this is a slow operation
95 Returns: non-zero for success, zero for failure
97 int jsw_hresize ( jsw_hash_t *htab, size_t new_size );
99 /* Reset the traversal markers to the beginning */
100 void jsw_hreset ( jsw_hash_t *htab );
102 /* Traverse forward by one key */
103 int jsw_hnext ( jsw_hash_t *htab );
105 /* Get the current key */
106 const void *jsw_hkey ( jsw_hash_t *htab );
108 /* Get the current item */
109 void *jsw_hitem ( jsw_hash_t *htab );
111 /* Current number of items in the table */
112 size_t jsw_hsize ( jsw_hash_t *htab );
114 /* Total allowable number of items without resizing */
115 size_t jsw_hcapacity ( jsw_hash_t *htab );
117 /* Get statistics for the hash table */
118 jsw_hstat_t *jsw_hstat ( jsw_hash_t *htab );