| 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 | |
| 6 | #include "debug.h" |
| 7 | |
| 8 | /* |
| 9 | Dictionary based on code by Morten Eriksen <mortene@sim.no>. |
| 10 | */ |
| 11 | |
| 12 | #include "dict.h" |
| 13 | |
| 14 | struct dict_entry { |
| 15 | unsigned int hash; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 16 | void *key; |
| 17 | void *value; |
| 18 | struct dict_entry *next; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 19 | }; |
| 20 | |
| 21 | /* #define DICTTABLESIZE 97 */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 22 | #define DICTTABLESIZE 997 /* Semi-randomly selected prime number. */ |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 23 | /* #define DICTTABLESIZE 9973 */ |
| 24 | /* #define DICTTABLESIZE 99991 */ |
| 25 | /* #define DICTTABLESIZE 999983 */ |
| 26 | |
| 27 | struct dict { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 28 | struct dict_entry *buckets[DICTTABLESIZE]; |
| 29 | unsigned int (*key2hash) (void *); |
| 30 | int (*key_cmp) (void *, void *); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 31 | }; |
| 32 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame^] | 33 | struct dict * |
| 34 | dict_init(unsigned int (*key2hash) (void *), |
| 35 | int (*key_cmp) (void *, void *)) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 36 | struct dict *d; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 37 | int i; |
| 38 | |
| 39 | d = malloc(sizeof(struct dict)); |
| 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 |
| 53 | dict_clear(struct 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 | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 57 | assert(d); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 58 | for (i = 0; i < DICTTABLESIZE; i++) { |
| 59 | for (entry = d->buckets[i]; entry != NULL; entry = nextentry) { |
| 60 | nextentry = entry->next; |
| 61 | free(entry); |
| 62 | } |
| 63 | d->buckets[i] = NULL; |
| 64 | } |
| 65 | free(d); |
| 66 | } |
| 67 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame^] | 68 | int |
| 69 | dict_enter(struct dict *d, void *key, void *value) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 70 | struct dict_entry *entry, *newentry; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 71 | unsigned int hash = d->key2hash(key); |
| 72 | unsigned int bucketpos = hash % DICTTABLESIZE; |
| 73 | |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 74 | assert(d); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 75 | newentry = malloc(sizeof(struct dict_entry)); |
| 76 | if (!newentry) { |
| 77 | perror("malloc"); |
| 78 | exit(1); |
| 79 | } |
| 80 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 81 | newentry->hash = hash; |
| 82 | newentry->key = key; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 83 | newentry->value = value; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 84 | newentry->next = NULL; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 85 | |
| 86 | entry = d->buckets[bucketpos]; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 87 | while (entry && entry->next) |
| 88 | entry = entry->next; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 89 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 90 | if (entry) |
| 91 | entry->next = newentry; |
| 92 | else |
| 93 | d->buckets[bucketpos] = newentry; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 94 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 95 | 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] | 96 | return 0; |
| 97 | } |
| 98 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame^] | 99 | void * |
| 100 | dict_find_entry(struct dict *d, void *key) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 101 | unsigned int hash = d->key2hash(key); |
| 102 | unsigned int bucketpos = hash % DICTTABLESIZE; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 103 | struct dict_entry *entry; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 104 | |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 105 | assert(d); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 106 | for (entry = d->buckets[bucketpos]; entry; entry = entry->next) { |
| 107 | if (hash != entry->hash) { |
| 108 | continue; |
| 109 | } |
| 110 | if (!d->key_cmp(key, entry->key)) { |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | return entry ? entry->value : NULL; |
| 115 | } |
| 116 | |
| 117 | void |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 118 | dict_apply_to_all(struct dict *d, |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame^] | 119 | void (*func) (void *key, void *value, void *data), void *data) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 120 | int i; |
| 121 | |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 122 | if (!d) { |
| 123 | return; |
| 124 | } |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 125 | for (i = 0; i < DICTTABLESIZE; i++) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 126 | struct dict_entry *entry = d->buckets[i]; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 127 | while (entry) { |
| 128 | func(entry->key, entry->value, data); |
| 129 | entry = entry->next; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /*****************************************************************************/ |
| 135 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame^] | 136 | unsigned int |
| 137 | dict_key2hash_string(void *key) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 138 | const char *s = (const char *)key; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 139 | unsigned int total = 0, shift = 0; |
| 140 | |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 141 | assert(key); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 142 | while (*s) { |
| 143 | total = total ^ ((*s) << shift); |
| 144 | shift += 5; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 145 | if (shift > 24) |
| 146 | shift -= 24; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 147 | s++; |
| 148 | } |
| 149 | return total; |
| 150 | } |
| 151 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame^] | 152 | int |
| 153 | dict_key_cmp_string(void *key1, void *key2) { |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 154 | assert(key1); |
| 155 | assert(key2); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 156 | return strcmp((const char *)key1, (const char *)key2); |
| 157 | } |
| 158 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame^] | 159 | unsigned int |
| 160 | dict_key2hash_int(void *key) { |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 161 | return (unsigned long)key; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame^] | 164 | int |
| 165 | dict_key_cmp_int(void *key1, void *key2) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 166 | return key1 - key2; |
| 167 | } |