Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 1 | /* |
Michael Clark | 837240f | 2007-03-13 08:26:25 +0000 | [diff] [blame] | 2 | * $Id: linkhash.h,v 1.6 2006/01/30 23:07:57 mclark Exp $ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 3 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 5 | * Michael Clark <michael@metaparadigm.com> |
| 6 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 7 | * 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 9 | * |
| 10 | */ |
| 11 | |
| 12 | #ifndef _linkhash_h_ |
| 13 | #define _linkhash_h_ |
| 14 | |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 15 | #ifdef __cplusplus |
| 16 | extern "C" { |
| 17 | #endif |
| 18 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 19 | /** |
| 20 | * golden prime used in hash functions |
| 21 | */ |
| 22 | #define LH_PRIME 0x9e370001UL |
| 23 | |
| 24 | /** |
Eric Haszlakiewicz | 64c0ca3 | 2012-03-31 17:33:58 -0500 | [diff] [blame^] | 25 | * 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 32 | * 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 41 | struct lh_entry; |
| 42 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 43 | /** |
| 44 | * callback function prototypes |
| 45 | */ |
| 46 | typedef void (lh_entry_free_fn) (struct lh_entry *e); |
| 47 | /** |
| 48 | * callback function prototypes |
| 49 | */ |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 50 | typedef unsigned long (lh_hash_fn) (const void *k); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 51 | /** |
| 52 | * callback function prototypes |
| 53 | */ |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 54 | typedef int (lh_equal_fn) (const void *k1, const void *k2); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * An entry in the hash table |
| 58 | */ |
| 59 | struct lh_entry { |
| 60 | /** |
| 61 | * The key. |
| 62 | */ |
| 63 | void *k; |
| 64 | /** |
| 65 | * The value. |
| 66 | */ |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 67 | const void *v; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 68 | /** |
| 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 | */ |
| 82 | struct 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 Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 120 | const char *name; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 121 | |
| 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 Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 146 | extern unsigned long lh_ptr_hash(const void *k); |
| 147 | extern int lh_ptr_equal(const void *k1, const void *k2); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 148 | |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 149 | extern unsigned long lh_char_hash(const void *k); |
| 150 | extern int lh_char_equal(const void *k1, const void *k2); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 151 | |
| 152 | |
| 153 | /** |
| 154 | * Convenience list iterator. |
| 155 | */ |
| 156 | #define lh_foreach(table, entry) \ |
| 157 | for(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) \ |
| 163 | for(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 Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 184 | extern struct lh_table* lh_table_new(int size, const char *name, |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 185 | 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 Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 197 | extern struct lh_table* lh_kchar_table_new(int size, const char *name, |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 198 | 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 Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 209 | extern struct lh_table* lh_kptr_table_new(int size, const char *name, |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 210 | 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 | */ |
| 219 | extern 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 Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 228 | extern int lh_table_insert(struct lh_table *t, void *k, const void *v); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 229 | |
| 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 Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 237 | extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 238 | |
| 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 Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 245 | extern const void* lh_table_lookup(struct lh_table *t, const void *k); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 246 | |
| 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 | */ |
| 257 | extern 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 Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 269 | extern int lh_table_delete(struct lh_table *t, const void *k); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 270 | |
| 271 | |
Michael Clark | 14862b1 | 2007-12-07 02:50:42 +0000 | [diff] [blame] | 272 | void lh_abort(const char *msg, ...); |
| 273 | void lh_table_resize(struct lh_table *t, int new_size); |
| 274 | |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 275 | #ifdef __cplusplus |
| 276 | } |
| 277 | #endif |
| 278 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 279 | #endif |