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