blob: 9d894604c1121be8cecbaf15381ca73699787685 [file] [log] [blame]
Michael Clarkf0d08882007-03-13 08:26:18 +00001/*
Michael Clark837240f2007-03-13 08:26:25 +00002 * $Id: linkhash.h,v 1.6 2006/01/30 23:07:57 mclark Exp $
Michael Clarkf0d08882007-03-13 08:26:18 +00003 *
Michael Clarkf6a6e482007-03-13 08:26:23 +00004 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
Michael Clarkf0d08882007-03-13 08:26:18 +00005 * Michael Clark <michael@metaparadigm.com>
6 *
Michael Clarkf6a6e482007-03-13 08:26:23 +00007 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the MIT license. See COPYING for details.
Michael Clarkf0d08882007-03-13 08:26:18 +00009 *
10 */
11
12#ifndef _linkhash_h_
13#define _linkhash_h_
14
Michael Clarkaaec1ef2009-02-25 02:31:32 +000015#ifdef __cplusplus
16extern "C" {
17#endif
18
Michael Clarkf0d08882007-03-13 08:26:18 +000019/**
20 * golden prime used in hash functions
21 */
22#define LH_PRIME 0x9e370001UL
23
24/**
Eric Haszlakiewicz64c0ca32012-03-31 17:33:58 -050025 * The fraction of filled hash buckets until an insert will cause the table
26 * to be resized.
27 * This can range from just above 0 up to 1.0.
28 */
29#define LH_LOAD_FACTOR 0.66
30
31/**
Michael Clarkf0d08882007-03-13 08:26:18 +000032 * sentinel pointer value for empty slots
33 */
34#define LH_EMPTY (void*)-1
35
36/**
37 * sentinel pointer value for freed slots
38 */
39#define LH_FREED (void*)-2
40
Michael Clarkf0d08882007-03-13 08:26:18 +000041struct lh_entry;
42
Michael Clarkf0d08882007-03-13 08:26:18 +000043/**
44 * callback function prototypes
45 */
46typedef void (lh_entry_free_fn) (struct lh_entry *e);
47/**
48 * callback function prototypes
49 */
Michael Clark68cafad2009-01-06 22:56:57 +000050typedef unsigned long (lh_hash_fn) (const void *k);
Michael Clarkf0d08882007-03-13 08:26:18 +000051/**
52 * callback function prototypes
53 */
Michael Clark68cafad2009-01-06 22:56:57 +000054typedef int (lh_equal_fn) (const void *k1, const void *k2);
Michael Clarkf0d08882007-03-13 08:26:18 +000055
56/**
57 * An entry in the hash table
58 */
59struct lh_entry {
60 /**
61 * The key.
62 */
63 void *k;
64 /**
65 * The value.
66 */
Michael Clark68cafad2009-01-06 22:56:57 +000067 const void *v;
Michael Clarkf0d08882007-03-13 08:26:18 +000068 /**
69 * The next entry
70 */
71 struct lh_entry *next;
72 /**
73 * The previous entry.
74 */
75 struct lh_entry *prev;
76};
77
78
79/**
80 * The hash table structure.
81 */
82struct lh_table {
83 /**
84 * Size of our hash.
85 */
86 int size;
87 /**
88 * Numbers of entries.
89 */
90 int count;
91
92 /**
93 * Number of collisions.
94 */
95 int collisions;
96
97 /**
98 * Number of resizes.
99 */
100 int resizes;
101
102 /**
103 * Number of lookups.
104 */
105 int lookups;
106
107 /**
108 * Number of inserts.
109 */
110 int inserts;
111
112 /**
113 * Number of deletes.
114 */
115 int deletes;
116
117 /**
118 * Name of the hash table.
119 */
Michael Clark68cafad2009-01-06 22:56:57 +0000120 const char *name;
Michael Clarkf0d08882007-03-13 08:26:18 +0000121
122 /**
123 * The first entry.
124 */
125 struct lh_entry *head;
126
127 /**
128 * The last entry.
129 */
130 struct lh_entry *tail;
131
132 struct lh_entry *table;
133
134 /**
135 * A pointer onto the function responsible for freeing an entry.
136 */
137 lh_entry_free_fn *free_fn;
138 lh_hash_fn *hash_fn;
139 lh_equal_fn *equal_fn;
140};
141
142
143/**
144 * Pre-defined hash and equality functions
145 */
Michael Clark68cafad2009-01-06 22:56:57 +0000146extern unsigned long lh_ptr_hash(const void *k);
147extern int lh_ptr_equal(const void *k1, const void *k2);
Michael Clarkf0d08882007-03-13 08:26:18 +0000148
Michael Clark68cafad2009-01-06 22:56:57 +0000149extern unsigned long lh_char_hash(const void *k);
150extern int lh_char_equal(const void *k1, const void *k2);
Michael Clarkf0d08882007-03-13 08:26:18 +0000151
152
153/**
154 * Convenience list iterator.
155 */
156#define lh_foreach(table, entry) \
157for(entry = table->head; entry; entry = entry->next)
158
159/**
160 * lh_foreach_safe allows calling of deletion routine while iterating.
161 */
162#define lh_foreach_safe(table, entry, tmp) \
163for(entry = table->head; entry && ((tmp = entry->next) || 1); entry = tmp)
164
165
166
167/**
168 * Create a new linkhash table.
169 * @param size initial table size. The table is automatically resized
170 * although this incurs a performance penalty.
171 * @param name the table name.
172 * @param free_fn callback function used to free memory for entries
173 * when lh_table_free or lh_table_delete is called.
174 * If NULL is provided, then memory for keys and values
175 * must be freed by the caller.
176 * @param hash_fn function used to hash keys. 2 standard ones are defined:
177 * lh_ptr_hash and lh_char_hash for hashing pointer values
178 * and C strings respectively.
179 * @param equal_fn comparison function to compare keys. 2 standard ones defined:
180 * lh_ptr_hash and lh_char_hash for comparing pointer values
181 * and C strings respectively.
182 * @return a pointer onto the linkhash table.
183 */
Michael Clark68cafad2009-01-06 22:56:57 +0000184extern struct lh_table* lh_table_new(int size, const char *name,
Michael Clarkf0d08882007-03-13 08:26:18 +0000185 lh_entry_free_fn *free_fn,
186 lh_hash_fn *hash_fn,
187 lh_equal_fn *equal_fn);
188
189/**
190 * Convenience function to create a new linkhash
191 * table with char keys.
192 * @param size initial table size.
193 * @param name table name.
194 * @param free_fn callback function used to free memory for entries.
195 * @return a pointer onto the linkhash table.
196 */
Michael Clark68cafad2009-01-06 22:56:57 +0000197extern struct lh_table* lh_kchar_table_new(int size, const char *name,
Michael Clarkf0d08882007-03-13 08:26:18 +0000198 lh_entry_free_fn *free_fn);
199
200
201/**
202 * Convenience function to create a new linkhash
203 * table with ptr keys.
204 * @param size initial table size.
205 * @param name table name.
206 * @param free_fn callback function used to free memory for entries.
207 * @return a pointer onto the linkhash table.
208 */
Michael Clark68cafad2009-01-06 22:56:57 +0000209extern struct lh_table* lh_kptr_table_new(int size, const char *name,
Michael Clarkf0d08882007-03-13 08:26:18 +0000210 lh_entry_free_fn *free_fn);
211
212
213/**
214 * Free a linkhash table.
215 * If a callback free function is provided then it is called for all
216 * entries in the table.
217 * @param t table to free.
218 */
219extern void lh_table_free(struct lh_table *t);
220
221
222/**
223 * Insert a record into the table.
224 * @param t the table to insert into.
225 * @param k a pointer to the key to insert.
226 * @param v a pointer to the value to insert.
227 */
Michael Clark68cafad2009-01-06 22:56:57 +0000228extern int lh_table_insert(struct lh_table *t, void *k, const void *v);
Michael Clarkf0d08882007-03-13 08:26:18 +0000229
230
231/**
232 * Lookup a record into the table.
233 * @param t the table to lookup
234 * @param k a pointer to the key to lookup
235 * @return a pointer to the record structure of the value or NULL if it does not exist.
236 */
Michael Clark68cafad2009-01-06 22:56:57 +0000237extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k);
Michael Clarkf0d08882007-03-13 08:26:18 +0000238
239/**
240 * Lookup a record into the table
241 * @param t the table to lookup
242 * @param k a pointer to the key to lookup
243 * @return a pointer to the found value or NULL if it does not exist.
244 */
Michael Clark68cafad2009-01-06 22:56:57 +0000245extern const void* lh_table_lookup(struct lh_table *t, const void *k);
Michael Clarkf0d08882007-03-13 08:26:18 +0000246
247
248/**
249 * Delete a record from the table.
250 * If a callback free function is provided then it is called for the
251 * for the item being deleted.
252 * @param t the table to delete from.
253 * @param e a pointer to the entry to delete.
254 * @return 0 if the item was deleted.
255 * @return -1 if it was not found.
256 */
257extern int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e);
258
259
260/**
261 * Delete a record from the table.
262 * If a callback free function is provided then it is called for the
263 * for the item being deleted.
264 * @param t the table to delete from.
265 * @param k a pointer to the key to delete.
266 * @return 0 if the item was deleted.
267 * @return -1 if it was not found.
268 */
Michael Clark68cafad2009-01-06 22:56:57 +0000269extern int lh_table_delete(struct lh_table *t, const void *k);
Michael Clarkf0d08882007-03-13 08:26:18 +0000270
271
Michael Clark14862b12007-12-07 02:50:42 +0000272void lh_abort(const char *msg, ...);
273void lh_table_resize(struct lh_table *t, int new_size);
274
Michael Clarkaaec1ef2009-02-25 02:31:32 +0000275#ifdef __cplusplus
276}
277#endif
278
Michael Clarkf0d08882007-03-13 08:26:18 +0000279#endif