| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 3 | #include <string.h> |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 4 | #include <assert.h> |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 5 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 6 | #include "common.h" |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 7 | |
| 8 | /* |
| 9 | Dictionary based on code by Morten Eriksen <mortene@sim.no>. |
| 10 | */ |
| 11 | |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 12 | struct dict_entry { |
| 13 | unsigned int hash; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 14 | void *key; |
| 15 | void *value; |
| 16 | struct dict_entry *next; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 17 | }; |
| 18 | |
| 19 | /* #define DICTTABLESIZE 97 */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 20 | #define DICTTABLESIZE 997 /* Semi-randomly selected prime number. */ |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 21 | /* #define DICTTABLESIZE 9973 */ |
| 22 | /* #define DICTTABLESIZE 99991 */ |
| 23 | /* #define DICTTABLESIZE 999983 */ |
| 24 | |
| 25 | struct dict { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 26 | struct dict_entry *buckets[DICTTABLESIZE]; |
| Petr Machata | 345c9b5 | 2012-02-10 13:10:03 +0100 | [diff] [blame] | 27 | unsigned int (*key2hash) (const void *); |
| 28 | int (*key_cmp) (const void *, const void *); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 29 | }; |
| 30 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 31 | Dict * |
| Petr Machata | 345c9b5 | 2012-02-10 13:10:03 +0100 | [diff] [blame] | 32 | dict_init(unsigned int (*key2hash) (const void *), |
| 33 | int (*key_cmp) (const void *, const void *)) |
| 34 | { |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 35 | Dict *d; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 36 | int i; |
| 37 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 38 | debug(DEBUG_FUNCTION, "dict_init()"); |
| 39 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 40 | d = malloc(sizeof(Dict)); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 41 | if (!d) { |
| 42 | perror("malloc()"); |
| 43 | exit(1); |
| 44 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 45 | for (i = 0; i < DICTTABLESIZE; i++) { /* better use memset()? */ |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 46 | d->buckets[i] = NULL; |
| 47 | } |
| 48 | d->key2hash = key2hash; |
| 49 | d->key_cmp = key_cmp; |
| 50 | return d; |
| 51 | } |
| 52 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 53 | void |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 54 | dict_clear(Dict *d) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 55 | int i; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 56 | struct dict_entry *entry, *nextentry; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 57 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 58 | debug(DEBUG_FUNCTION, "dict_clear()"); |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 59 | assert(d); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 60 | for (i = 0; i < DICTTABLESIZE; i++) { |
| 61 | for (entry = d->buckets[i]; entry != NULL; entry = nextentry) { |
| 62 | nextentry = entry->next; |
| 63 | free(entry); |
| 64 | } |
| 65 | d->buckets[i] = NULL; |
| 66 | } |
| 67 | free(d); |
| 68 | } |
| 69 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 70 | int |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 71 | dict_enter(Dict *d, void *key, void *value) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 72 | struct dict_entry *entry, *newentry; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 73 | unsigned int hash; |
| 74 | unsigned int bucketpos; |
| 75 | |
| 76 | debug(DEBUG_FUNCTION, "dict_enter()"); |
| 77 | |
| 78 | hash = d->key2hash(key); |
| 79 | bucketpos = hash % DICTTABLESIZE; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 80 | |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 81 | assert(d); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 82 | newentry = malloc(sizeof(struct dict_entry)); |
| 83 | if (!newentry) { |
| 84 | perror("malloc"); |
| 85 | exit(1); |
| 86 | } |
| 87 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 88 | newentry->hash = hash; |
| 89 | newentry->key = key; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 90 | newentry->value = value; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 91 | newentry->next = NULL; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 92 | |
| 93 | entry = d->buckets[bucketpos]; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 94 | while (entry && entry->next) |
| 95 | entry = entry->next; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 96 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 97 | if (entry) |
| 98 | entry->next = newentry; |
| 99 | else |
| 100 | d->buckets[bucketpos] = newentry; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 101 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 102 | debug(3, "new dict entry at %p[%d]: (%p,%p)", d, bucketpos, key, value); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 106 | void * |
| Petr Machata | 77fbb8f | 2012-04-19 16:57:57 +0200 | [diff] [blame^] | 107 | dict_remove(Dict *d, void *key) |
| 108 | { |
| 109 | assert(d != NULL); |
| 110 | debug(DEBUG_FUNCTION, "dict_remove(%p)", key); |
| 111 | |
| 112 | unsigned int hash = d->key2hash(key); |
| 113 | unsigned int bucketpos = hash % DICTTABLESIZE; |
| 114 | |
| 115 | struct dict_entry **entryp; |
| 116 | for (entryp = &d->buckets[bucketpos]; (*entryp) != NULL; |
| 117 | entryp = &(*entryp)->next) { |
| 118 | struct dict_entry *entry = *entryp; |
| 119 | if (hash != entry->hash) |
| 120 | continue; |
| 121 | if (d->key_cmp(key, entry->key) == 0) { |
| 122 | *entryp = entry->next; |
| 123 | return entry->value; |
| 124 | } |
| 125 | } |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | void * |
| Petr Machata | 345c9b5 | 2012-02-10 13:10:03 +0100 | [diff] [blame] | 130 | dict_find_entry(Dict *d, const void *key) |
| 131 | { |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 132 | unsigned int hash; |
| 133 | unsigned int bucketpos; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 134 | struct dict_entry *entry; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 135 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 136 | debug(DEBUG_FUNCTION, "dict_find_entry()"); |
| 137 | |
| 138 | hash = d->key2hash(key); |
| 139 | bucketpos = hash % DICTTABLESIZE; |
| 140 | |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 141 | assert(d); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 142 | for (entry = d->buckets[bucketpos]; entry; entry = entry->next) { |
| 143 | if (hash != entry->hash) { |
| 144 | continue; |
| 145 | } |
| 146 | if (!d->key_cmp(key, entry->key)) { |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | return entry ? entry->value : NULL; |
| 151 | } |
| 152 | |
| 153 | void |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 154 | dict_apply_to_all(Dict *d, |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 155 | void (*func) (void *key, void *value, void *data), void *data) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 156 | int i; |
| 157 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 158 | debug(DEBUG_FUNCTION, "dict_apply_to_all()"); |
| 159 | |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 160 | if (!d) { |
| 161 | return; |
| 162 | } |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 163 | for (i = 0; i < DICTTABLESIZE; i++) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 164 | struct dict_entry *entry = d->buckets[i]; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 165 | while (entry) { |
| 166 | func(entry->key, entry->value, data); |
| 167 | entry = entry->next; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /*****************************************************************************/ |
| 173 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 174 | unsigned int |
| Petr Machata | 345c9b5 | 2012-02-10 13:10:03 +0100 | [diff] [blame] | 175 | dict_key2hash_string(const void *key) |
| 176 | { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 177 | const char *s = (const char *)key; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 178 | unsigned int total = 0, shift = 0; |
| 179 | |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 180 | assert(key); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 181 | while (*s) { |
| 182 | total = total ^ ((*s) << shift); |
| 183 | shift += 5; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 184 | if (shift > 24) |
| 185 | shift -= 24; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 186 | s++; |
| 187 | } |
| 188 | return total; |
| 189 | } |
| 190 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 191 | int |
| Petr Machata | 345c9b5 | 2012-02-10 13:10:03 +0100 | [diff] [blame] | 192 | dict_key_cmp_string(const void *key1, const void *key2) |
| 193 | { |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 194 | assert(key1); |
| 195 | assert(key2); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 196 | return strcmp((const char *)key1, (const char *)key2); |
| 197 | } |
| 198 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 199 | unsigned int |
| Petr Machata | 345c9b5 | 2012-02-10 13:10:03 +0100 | [diff] [blame] | 200 | dict_key2hash_int(const void *key) |
| 201 | { |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 202 | return (unsigned long)key; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 205 | int |
| Petr Machata | 345c9b5 | 2012-02-10 13:10:03 +0100 | [diff] [blame] | 206 | dict_key_cmp_int(const void *key1, const void *key2) |
| 207 | { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 208 | return key1 - key2; |
| 209 | } |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 210 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 211 | Dict * |
| Petr Machata | 534e00f | 2011-09-27 17:58:38 +0200 | [diff] [blame] | 212 | dict_clone2(Dict * old, void * (*key_clone)(void *, void *), |
| 213 | void * (*value_clone)(void *, void *), void * data) |
| 214 | { |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 215 | Dict *d; |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 216 | int i; |
| 217 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 218 | debug(DEBUG_FUNCTION, "dict_clone()"); |
| 219 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 220 | d = malloc(sizeof(Dict)); |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 221 | if (!d) { |
| 222 | perror("malloc()"); |
| 223 | exit(1); |
| 224 | } |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 225 | memcpy(d, old, sizeof(Dict)); |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 226 | for (i = 0; i < DICTTABLESIZE; i++) { /* better use memset()? */ |
| 227 | struct dict_entry *de_old; |
| 228 | struct dict_entry **de_new; |
| 229 | |
| 230 | de_old = old->buckets[i]; |
| 231 | de_new = &d->buckets[i]; |
| 232 | while (de_old) { |
| Petr Machata | 534e00f | 2011-09-27 17:58:38 +0200 | [diff] [blame] | 233 | void * nkey, * nval; |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 234 | *de_new = malloc(sizeof(struct dict_entry)); |
| 235 | if (!*de_new) { |
| 236 | perror("malloc()"); |
| 237 | exit(1); |
| 238 | } |
| 239 | memcpy(*de_new, de_old, sizeof(struct dict_entry)); |
| Petr Machata | 534e00f | 2011-09-27 17:58:38 +0200 | [diff] [blame] | 240 | |
| 241 | /* The error detection is rather weak :-/ */ |
| 242 | nkey = key_clone(de_old->key, data); |
| 243 | if (nkey == NULL && de_old->key != NULL) { |
| 244 | perror("key_clone"); |
| 245 | err: |
| 246 | /* XXX Will this actually work? We |
| 247 | * simply memcpy the old dictionary |
| 248 | * over up there. */ |
| 249 | dict_clear(d); |
| 250 | free(de_new); |
| 251 | return NULL; |
| 252 | } |
| 253 | |
| 254 | nval = value_clone(de_old->value, data); |
| 255 | if (nval == NULL && de_old->value != NULL) { |
| 256 | perror("value_clone"); |
| 257 | goto err; |
| 258 | } |
| 259 | |
| 260 | (*de_new)->key = nkey; |
| 261 | (*de_new)->value = nval; |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 262 | de_new = &(*de_new)->next; |
| 263 | de_old = de_old->next; |
| 264 | } |
| 265 | } |
| 266 | return d; |
| 267 | } |
| Petr Machata | 534e00f | 2011-09-27 17:58:38 +0200 | [diff] [blame] | 268 | |
| 269 | struct wrap_clone_cb |
| 270 | { |
| 271 | void * (*key_clone)(void *); |
| 272 | void * (*value_clone)(void *); |
| 273 | }; |
| 274 | |
| 275 | static void * |
| 276 | value_clone_1(void * arg, void * data) |
| 277 | { |
| 278 | return ((struct wrap_clone_cb *)data)->value_clone(arg); |
| 279 | } |
| 280 | |
| 281 | static void * |
| 282 | key_clone_1(void * arg, void * data) |
| 283 | { |
| 284 | return ((struct wrap_clone_cb *)data)->key_clone(arg); |
| 285 | } |
| 286 | |
| 287 | Dict * |
| 288 | dict_clone(Dict * old, void * (*key_clone)(void *), |
| 289 | void * (*value_clone)(void *)) |
| 290 | { |
| 291 | struct wrap_clone_cb cb = { key_clone, value_clone }; |
| 292 | return dict_clone2(old, &key_clone_1, &value_clone_1, &cb); |
| 293 | } |