]> pd.if.org Git - jsw/blob - jsw_hlib.h
removed dos line ending carriage return
[jsw] / jsw_hlib.h
1 #ifndef JSW_HLIB
2 #define JSW_HLIB
3
4 /*
5   Hash table library using separate chaining
6
7     > Created (Julienne Walker): August 7, 2005
8     > Modified (Julienne Walker): August 11, 2005
9
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.
15
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.
20 */
21 #ifdef __cplusplus
22 #include <cstddef>
23
24 using std::size_t;
25
26 extern "C" {
27 #else
28 #include <stddef.h>
29 #endif
30
31 typedef struct jsw_hash jsw_hash_t;
32
33 /* Application specific hash function */
34 typedef unsigned (*hash_f) ( const void *key );
35
36 /* Application specific key comparison function */
37 typedef int      (*cmp_f) ( const void *a, const void *b );
38
39 /* Application specific key copying function */
40 typedef void    *(*keydup_f) ( const void *key );
41
42 /* Application specific data copying function */
43 typedef void    *(*itemdup_f) ( const void *item );
44
45 /* Application specific key deletion function */
46 typedef void     (*keyrel_f) ( void *key );
47
48 /* Application specific data deletion function */
49 typedef void     (*itemrel_f) ( void *item );
50
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 */
56 } jsw_hstat_t;
57
58 /*
59   Create a new hash table with a capacity of size, and
60   user defined functions for handling keys and items.
61
62   Returns: An empty hash table, or NULL on failure.
63 */
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 );
67
68 /* Release all memory used by the hash table */
69 void         jsw_hdelete ( jsw_hash_t *htab );
70
71 /*
72   Find an item with the selected key
73
74   Returns: The item, or NULL if not found
75 */
76 void        *jsw_hfind ( jsw_hash_t *htab, void *key );
77
78 /*
79   Insert an item with the selected key
80
81   Returns: non-zero for success, zero for failure
82 */
83 int          jsw_hinsert ( jsw_hash_t *htab, void *key, void *item );
84
85 /*
86   Remove an item with the selected key
87
88   Returns: non-zero for success, zero for failure
89 */
90 int          jsw_herase ( jsw_hash_t *htab, void *key );
91
92 /*
93   Grow or shrink the table, this is a slow operation
94   
95   Returns: non-zero for success, zero for failure
96 */
97 int          jsw_hresize ( jsw_hash_t *htab, size_t new_size );
98
99 /* Reset the traversal markers to the beginning */
100 void         jsw_hreset ( jsw_hash_t *htab );
101
102 /* Traverse forward by one key */
103 int          jsw_hnext ( jsw_hash_t *htab );
104
105 /* Get the current key */
106 const void  *jsw_hkey ( jsw_hash_t *htab );
107
108 /* Get the current item */
109 void        *jsw_hitem ( jsw_hash_t *htab );
110
111 /* Current number of items in the table */
112 size_t       jsw_hsize ( jsw_hash_t *htab );
113
114 /* Total allowable number of items without resizing */
115 size_t       jsw_hcapacity ( jsw_hash_t *htab );
116
117 /* Get statistics for the hash table */
118 jsw_hstat_t *jsw_hstat ( jsw_hash_t *htab );
119
120 #ifdef __cplusplus
121 }
122 #endif
123
124 #endif