| 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 * |
| 183 | dict_clone(Dict *old, void * (*key_clone)(void*), void * (*value_clone)(void*)) { |
| 184 | Dict *d; |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 185 | int i; |
| 186 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 187 | debug(DEBUG_FUNCTION, "dict_clone()"); |
| 188 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 189 | d = malloc(sizeof(Dict)); |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 190 | if (!d) { |
| 191 | perror("malloc()"); |
| 192 | exit(1); |
| 193 | } |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 194 | memcpy(d, old, sizeof(Dict)); |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 195 | for (i = 0; i < DICTTABLESIZE; i++) { /* better use memset()? */ |
| 196 | struct dict_entry *de_old; |
| 197 | struct dict_entry **de_new; |
| 198 | |
| 199 | de_old = old->buckets[i]; |
| 200 | de_new = &d->buckets[i]; |
| 201 | while (de_old) { |
| 202 | *de_new = malloc(sizeof(struct dict_entry)); |
| 203 | if (!*de_new) { |
| 204 | perror("malloc()"); |
| 205 | exit(1); |
| 206 | } |
| 207 | memcpy(*de_new, de_old, sizeof(struct dict_entry)); |
| 208 | (*de_new)->key = key_clone(de_old->key); |
| 209 | (*de_new)->value = value_clone(de_old->value); |
| 210 | de_new = &(*de_new)->next; |
| 211 | de_old = de_old->next; |
| 212 | } |
| 213 | } |
| 214 | return d; |
| 215 | } |