1 /* ------------------------------------------------------
2 * Open-addressed hash using double hash probing
5 * h(k, i) = ( h1(k) + i*h2(k) ) mod m
7 * requires: 1) m must be a power of two
8 * 2) h2(k) must return an odd number
10 * Besed on code published in _Mastering Algorithms in C_
11 * by Kyle Loudon (O'Reilly 1999).
12 * Modified by Philip Kovacs (kovacsp3@comcast.net)
15 * ------------------------------------------------------ */
20 typedef struct _hash_table_t {
26 int (*p_hash_fun1)(const void *p_data);
27 int (*p_hash_fun2)(const void *p_data);
28 int (*p_match_fun)(const void *p_data1, const void *p_data2);
29 void (*p_destroy_fun)(void *p_data);
33 /* Create and initialize a hash table on the heap.
34 Returns 0 on success, -1 otherwise. */
35 int hash_create( hash_table_t *p_hash_table,
37 int (*p_hash_fun1)(const void *p_data),
38 int (*p_hash_fun2)(const void *p_data),
39 int (*p_match_fun)(const void *p_data1, const void *p_data2),
40 void (*p_destroy_fun)(void *p_data)
43 /* Destroy a hash table */
44 void hash_destroy( hash_table_t *p_hash_table );
46 /* Insert an element into a hash table.
47 Returns 0 on successful insert, 1 if data already in hash and -1 if unable to insert. */
48 int hash_insert( hash_table_t *p_hash_table, const void *p_data);
50 /* Delete an element from a hash table.
51 Returns 0 on successful delete, -1 if not found. */
52 int hash_remove( hash_table_t *p_hash_table, void **pp_data);
54 /* Lookup an element in a hash table.
55 Returns 0 if found (data passed back byref), -1 if not found. */
56 int hash_lookup(const hash_table_t *p_hash_table, void **pp_data);
58 /* Return size of a hash table */
59 #define hash_size(p_hash_table) ((p_hash_table)->size)