|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +struct dnode { |
| 6 | + char *key; |
| 7 | + char *value; |
| 8 | +}; |
| 9 | + |
| 10 | +struct p1dict { |
| 11 | + int alloc; |
| 12 | + int length; |
| 13 | + struct dnode *items; |
| 14 | +}; |
| 15 | + |
| 16 | +int getBucket(char *str, int buckets) |
| 17 | +{ |
| 18 | + unsigned int hash = 123456; |
| 19 | + if ( str == NULL ) return 0; |
| 20 | + for( ; *str ; str++) { |
| 21 | + hash = ( hash << 3 ) ^ *str; |
| 22 | + } |
| 23 | + return hash % buckets; |
| 24 | +} |
| 25 | + |
| 26 | +/* Constructor - dct = dict() */ |
| 27 | +struct p1dict * p1dict_new() { |
| 28 | + int i; |
| 29 | + struct p1dict *p = malloc(sizeof(*p)); |
| 30 | + p->length = 0; |
| 31 | + p->alloc = 2; |
| 32 | + p->items = malloc(p->alloc * sizeof(struct dnode)); |
| 33 | + for(i=0; i < p->alloc; i++) { |
| 34 | + p->items[i].key = NULL; |
| 35 | + p->items[i].value = NULL; |
| 36 | + } |
| 37 | + return p; |
| 38 | +} |
| 39 | + |
| 40 | +/* Destructor - del(dct) */ |
| 41 | +void p1dict_del(struct p1dict* self) { |
| 42 | + int i; |
| 43 | + for(i=0; i < self->alloc; i++) { |
| 44 | + if ( self->items[i].key != NULL ) free(self->items[i].key); |
| 45 | + if ( self->items[i].value != NULL ) free(self->items[i].value); |
| 46 | + } |
| 47 | + free((void *) self->items); |
| 48 | + free((void *) self); |
| 49 | +} |
| 50 | + |
| 51 | +/* print(dct) */ |
| 52 | +/* {'z': 'W', 'y': 'B', 'c': 'C', 'a': 'D'} */ |
| 53 | +void p1dict_print(struct p1dict* self) |
| 54 | +{ |
| 55 | + int first = 1; |
| 56 | + int i; |
| 57 | + printf("{"); |
| 58 | + for(i=0; i < self->alloc; i++) { |
| 59 | + if ( self->items[i].key == NULL ) continue; |
| 60 | + if ( ! first ) printf(", "); |
| 61 | + printf("'%s': ", self->items[i].key); |
| 62 | + printf("'%s'", self->items[i].value); |
| 63 | + first = 0; |
| 64 | + printf(" [%d]", i); |
| 65 | + } |
| 66 | + printf("} [%d, %d]\n", self->length, self->alloc); |
| 67 | +} |
| 68 | + |
| 69 | +int p1dict_len(const struct p1dict* self) |
| 70 | +{ |
| 71 | + return self->length; |
| 72 | +} |
| 73 | + |
| 74 | +/* find a node - used in get and put */ |
| 75 | +struct dnode *p1dict_find(struct p1dict* self, char *key) |
| 76 | +{ |
| 77 | + int i, bucket, offset; |
| 78 | + if ( key == NULL ) return NULL; |
| 79 | + bucket = getBucket(key, self->alloc); |
| 80 | + for(offset=0; offset < self->alloc; offset++) { |
| 81 | + i = (offset + bucket) % self->alloc; |
| 82 | + if ( self->items[i].key == NULL ) { |
| 83 | + return &(self->items[i]); |
| 84 | + } |
| 85 | + if(strcmp(key, self->items[i].key) == 0 ) { |
| 86 | + return &(self->items[i]); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /* This should never happen as long as the resize is implemented */ |
| 91 | + |
| 92 | + printf("Could not find slot for key %s\n", key); |
| 93 | + return NULL; |
| 94 | +} |
| 95 | + |
| 96 | +/* x.get(key) - Returns NULL if not found */ |
| 97 | +char* p1dict_get(struct p1dict* self, char *key) |
| 98 | +{ |
| 99 | + struct dnode *retval = p1dict_find(self, key); |
| 100 | + if ( retval == NULL || retval->key == NULL ) return NULL; |
| 101 | + return retval->value; |
| 102 | +} |
| 103 | + |
| 104 | +/* x[key] = value; Insert or replace the value associated with a key */ |
| 105 | +void p1dict_put(struct p1dict* self, char *key, char *value) { |
| 106 | + |
| 107 | + int i, old_alloc, bucket; |
| 108 | + struct dnode *old, *new_item, *old_items; |
| 109 | + |
| 110 | + if ( key == NULL || value == NULL ) return; |
| 111 | + |
| 112 | + /* First look up */ |
| 113 | + old = p1dict_find(self, key); |
| 114 | + if ( old != NULL && old->key != NULL ) { |
| 115 | + free(old->value); |
| 116 | + old->value = malloc(strlen(value)+1); |
| 117 | + strcpy(old->value, value); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + /* TODO: Check if we need to re-hash the items */ |
| 122 | + if ( self->length >= (self->alloc*0.7) ) { |
| 123 | + printf("We are making space for %s\n", key); |
| 124 | + old_alloc = self->alloc; |
| 125 | + old_items = self->items; |
| 126 | + |
| 127 | + /* make new "empty items" */ |
| 128 | + self->alloc = self->alloc * 2; |
| 129 | + self->items = malloc(self->alloc * sizeof(struct dnode)); |
| 130 | + for(i=0; i < self->alloc; i++) { |
| 131 | + self->items[i].key = NULL; |
| 132 | + self->items[i].value = NULL; |
| 133 | + } |
| 134 | + |
| 135 | + /* We need to loop through old items and add them */ |
| 136 | + for(i=0; i<old_alloc; i++) { |
| 137 | + if ( old_items[i].key == NULL ) continue; |
| 138 | + new_item = p1dict_find(self, old_items[i].key); |
| 139 | + if ( new_item == NULL || new_item->key != NULL ) { |
| 140 | + printf("Very bad news new_item=%p\n", new_item); |
| 141 | + } |
| 142 | + new_item->key = old_items[i].key; |
| 143 | + new_item->value = old_items[i].value; |
| 144 | + } |
| 145 | + free(old_items); |
| 146 | + old = p1dict_find(self, key); |
| 147 | + if ( old == NULL || old->key != NULL ) { |
| 148 | + printf("Very very bad news!!!!\n"); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + /* Not found - time to insert */ |
| 153 | + old->value = malloc(strlen(value)+1); |
| 154 | + strcpy(old->value, value); |
| 155 | + old->key = malloc(strlen(key)+1); |
| 156 | + strcpy(old->key, key); |
| 157 | + |
| 158 | + self->length++; |
| 159 | +} |
| 160 | + |
| 161 | +int main(void) |
| 162 | +{ |
| 163 | + struct dnode * cur; |
| 164 | + struct p1dict * dct = p1dict_new(); |
| 165 | + p1dict_print(dct); |
| 166 | + p1dict_put(dct, "z", "Catch phrase"); |
| 167 | + p1dict_print(dct); |
| 168 | + p1dict_put(dct, "z", "W"); |
| 169 | + p1dict_print(dct); |
| 170 | + p1dict_put(dct, "SAKAI", "B"); |
| 171 | + p1dict_print(dct); |
| 172 | + p1dict_put(dct, "sally", "C"); |
| 173 | + p1dict_print(dct); |
| 174 | + p1dict_put(dct, "a", "D"); |
| 175 | + p1dict_print(dct); |
| 176 | + printf("Length=%d\n",p1dict_len(dct)); |
| 177 | + |
| 178 | + printf("z=%s\n", p1dict_get(dct, "z")); |
| 179 | + printf("x=%s\n", p1dict_get(dct, "x")); |
| 180 | + |
| 181 | + /* |
| 182 | + printf("\nDump\n"); |
| 183 | + for(cur = dct->head; cur != NULL ; cur = cur->next ) { |
| 184 | + printf("%s=%s\n", cur.key, cur.value); |
| 185 | + } |
| 186 | + */ |
| 187 | + |
| 188 | + p1dict_del(dct); |
| 189 | +} |
| 190 | + |
| 191 | +// rm -f a.out ; gcc p1dict.c; a.out ; rm -f a.out |
| 192 | + |
| 193 | + |
0 commit comments