]> pd.if.org Git - zpackage/blob - lib/jsw/jsw_hlib.h
remove c++isms from jsw code
[zpackage] / lib / 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 #include <stddef.h>
22
23 typedef struct jsw_hash jsw_hash_t;
24
25 /* Application specific hash function */
26 typedef unsigned (*hash_f) ( const void *key );
27
28 /* Application specific key comparison function */
29 typedef int      (*cmp_f) ( const void *a, const void *b );
30
31 /* Application specific key copying function */
32 typedef void    *(*keydup_f) ( const void *key );
33
34 /* Application specific data copying function */
35 typedef void    *(*itemdup_f) ( const void *item );
36
37 /* Application specific key deletion function */
38 typedef void     (*keyrel_f) ( void *key );
39
40 /* Application specific data deletion function */
41 typedef void     (*itemrel_f) ( void *item );
42
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 */
48 } jsw_hstat_t;
49
50 /*
51   Create a new hash table with a capacity of size, and
52   user defined functions for handling keys and items.
53
54   Returns: An empty hash table, or NULL on failure.
55 */
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 );
59
60 /* Release all memory used by the hash table */
61 void         jsw_hdelete ( jsw_hash_t *htab );
62
63 /*
64   Find an item with the selected key
65
66   Returns: The item, or NULL if not found
67 */
68 void        *jsw_hfind ( jsw_hash_t *htab, void *key );
69
70 /*
71   Insert an item with the selected key
72
73   Returns: non-zero for success, zero for failure
74 */
75 int          jsw_hinsert ( jsw_hash_t *htab, void *key, void *item );
76
77 /*
78   Remove an item with the selected key
79
80   Returns: non-zero for success, zero for failure
81 */
82 int          jsw_herase ( jsw_hash_t *htab, void *key );
83
84 /*
85   Grow or shrink the table, this is a slow operation
86   
87   Returns: non-zero for success, zero for failure
88 */
89 int          jsw_hresize ( jsw_hash_t *htab, size_t new_size );
90
91 /* Reset the traversal markers to the beginning */
92 void         jsw_hreset ( jsw_hash_t *htab );
93
94 /* Traverse forward by one key */
95 int          jsw_hnext ( jsw_hash_t *htab );
96
97 /* Get the current key */
98 const void  *jsw_hkey ( jsw_hash_t *htab );
99
100 /* Get the current item */
101 void        *jsw_hitem ( jsw_hash_t *htab );
102
103 /* Current number of items in the table */
104 size_t       jsw_hsize ( jsw_hash_t *htab );
105
106 /* Total allowable number of items without resizing */
107 size_t       jsw_hcapacity ( jsw_hash_t *htab );
108
109 /* Get statistics for the hash table */
110 jsw_hstat_t *jsw_hstat ( jsw_hash_t *htab );
111
112 #endif