ucx/map.c

Fri, 05 Oct 2012 14:06:40 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 05 Oct 2012 14:06:40 +0200
changeset 51
1c78cd19fb6b
parent 48
621a4430c404
child 52
34f50d0bada4
permissions
-rw-r--r--

added rehashing to maps by using clone function

olaf@20 1 /*
olaf@20 2 *
olaf@20 3 */
olaf@2 4
olaf@20 5 #include <stdlib.h>
olaf@20 6 #include <string.h>
olaf@20 7
olaf@20 8 #include "map.h"
olaf@20 9
olaf@20 10 UcxMap *ucx_map_new(size_t size) {
olaf@45 11 if(size == 0) {
olaf@45 12 size = 16;
olaf@45 13 }
olaf@45 14
olaf@20 15 UcxMap *map = (UcxMap*)malloc(sizeof(UcxMap));
olaf@20 16 if(map == NULL) {
olaf@20 17 return NULL;
olaf@20 18 }
olaf@20 19
universe@29 20 map->map = (UcxMapElement**)calloc(size, sizeof(UcxMapElement*));
olaf@20 21 if(map->map == NULL) {
olaf@20 22 free(map);
olaf@20 23 return NULL;
olaf@20 24 }
olaf@20 25 map->size = size;
olaf@45 26 map->count = 0;
olaf@20 27
olaf@20 28 return map;
olaf@20 29 }
olaf@20 30
universe@29 31 void ucx_map_free(UcxMap *map) {
universe@29 32 for (size_t n = 0 ; n < map->size ; n++) {
universe@29 33 UcxMapElement *elem = map->map[n];
universe@29 34 if (elem != NULL) {
universe@29 35 do {
universe@29 36 UcxMapElement *next = elem->next;
olaf@30 37 free(elem->key.data);
universe@29 38 free(elem);
universe@29 39 elem = next;
universe@29 40 } while (elem != NULL);
universe@29 41 }
universe@29 42 }
olaf@30 43 free(map->map);
universe@29 44 free(map);
universe@29 45 }
universe@29 46
olaf@44 47 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) {
universe@51 48 size_t bs = (map->count * 5) >> 1;
olaf@45 49 UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size);
olaf@44 50 UcxMapIterator i = ucx_map_iterator(map);
olaf@44 51 void *value;
olaf@44 52 UCX_MAP_FOREACH(value, i) {
olaf@44 53 ucx_map_put(newmap, i.cur->key, fnc ? fnc(value, data) : value);
olaf@44 54 }
olaf@44 55 return newmap;
olaf@44 56 }
olaf@44 57
universe@51 58 UcxMap *ucx_map_rehash(UcxMap *map) {
universe@51 59 size_t load = (map->size * 3) >> 2;
universe@51 60 if (map->count > load) {
universe@51 61 UcxMap *newmap = ucx_map_clone(map, NULL, NULL);
universe@51 62 ucx_map_free(map);
universe@51 63 return newmap;
universe@51 64 } else {
universe@51 65 return map;
universe@51 66 }
universe@51 67 }
universe@51 68
olaf@20 69 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
olaf@20 70 if(key.hash == 0) {
olaf@20 71 key.hash = ucx_hash((char*)key.data, key.len);
olaf@20 72 }
olaf@20 73
universe@29 74 size_t slot = key.hash%map->size;
universe@29 75 UcxMapElement *elm = map->map[slot];
universe@29 76 UcxMapElement *prev = NULL;
universe@29 77
universe@29 78 while (elm != NULL && elm->key.hash < key.hash) {
universe@29 79 prev = elm;
universe@29 80 elm = elm->next;
universe@29 81 }
universe@29 82
universe@29 83 if (elm == NULL || elm->key.hash != key.hash) {
olaf@20 84 UcxMapElement *e = (UcxMapElement*)malloc(sizeof(UcxMapElement));
olaf@20 85 if(e == NULL) {
olaf@20 86 return -1;
olaf@20 87 }
olaf@30 88 e->key.data = NULL;
universe@29 89 if (prev == NULL) {
universe@29 90 map->map[slot] = e;
universe@29 91 } else {
universe@29 92 prev->next = e;
universe@29 93 }
universe@29 94 e->next = elm;
olaf@20 95 elm = e;
olaf@20 96 }
universe@29 97
olaf@30 98 if(elm->key.data == NULL) {
olaf@30 99 void *kd = malloc(key.len);
olaf@30 100 if (kd == NULL) {
olaf@30 101 return -1;
olaf@30 102 }
olaf@30 103 memcpy(kd, key.data, key.len);
olaf@30 104 key.data = kd;
olaf@30 105 elm->key = key;
olaf@45 106 map->count++;
olaf@30 107 }
olaf@20 108 elm->data = data;
olaf@20 109
olaf@20 110 return 0;
olaf@20 111 }
olaf@20 112
olaf@20 113 void* ucx_map_get(UcxMap *map, UcxKey key) {
olaf@20 114 if(key.hash == 0) {
olaf@20 115 key.hash = ucx_hash((char*)key.data, key.len);
olaf@20 116 }
olaf@20 117
universe@29 118 UcxMapElement *elm = map->map[key.hash%map->size];
universe@29 119 while (elm != NULL && elm->key.hash <= key.hash) {
olaf@20 120 if(elm->key.hash == key.hash) {
olaf@20 121 int n = (key.len > elm->key.len) ? elm->key.len : key.len;
universe@29 122 if (memcmp(elm->key.data, key.data, n) == 0) {
olaf@20 123 return elm->data;
olaf@20 124 }
olaf@20 125 }
olaf@20 126 elm = elm->next;
olaf@20 127 }
olaf@20 128
olaf@20 129 return NULL;
olaf@20 130 }
olaf@20 131
olaf@20 132 UcxKey ucx_key(void *data, size_t len) {
olaf@20 133 UcxKey key;
olaf@20 134 key.data = data;
olaf@20 135 key.len = len;
olaf@20 136 key.hash = ucx_hash(data, len);
olaf@20 137 return key;
olaf@20 138 }
olaf@20 139
olaf@20 140
olaf@20 141 int ucx_hash(char *data, size_t len) {
olaf@20 142 /* murmur hash 2 */
olaf@20 143
olaf@20 144 int m = 0x5bd1e995;
olaf@20 145 int r = 24;
olaf@20 146
olaf@20 147 int h = 25 ^ len;
olaf@20 148
olaf@20 149 int i = 0;
olaf@20 150 while (len >= 4) {
olaf@20 151 int k = data[i + 0] & 0xFF;
olaf@20 152 k |= (data[i + 1] & 0xFF) << 8;
olaf@20 153 k |= (data[i + 2] & 0xFF) << 16;
olaf@20 154 k |= (data[i + 3] & 0xFF) << 24;
olaf@20 155
olaf@20 156 k *= m;
olaf@20 157 k ^= k >> r;
olaf@20 158 k *= m;
olaf@20 159
olaf@20 160 h *= m;
olaf@20 161 h ^= k;
olaf@20 162
olaf@20 163 i += 4;
olaf@20 164 len -= 4;
olaf@20 165 }
olaf@20 166
olaf@20 167 switch (len) {
olaf@20 168 case 3: h ^= (data[i + 2] & 0xFF) << 16;
universe@38 169 /* no break */
olaf@20 170 case 2: h ^= (data[i + 1] & 0xFF) << 8;
universe@38 171 /* no break */
olaf@20 172 case 1: h ^= (data[i + 0] & 0xFF); h *= m;
universe@38 173 /* no break */
olaf@20 174 }
olaf@20 175
olaf@20 176 h ^= h >> 13;
olaf@20 177 h *= m;
olaf@20 178 h ^= h >> 15;
olaf@20 179
olaf@20 180 return h;
olaf@20 181 }
olaf@31 182
olaf@31 183 UcxMapIterator ucx_map_iterator(UcxMap *map) {
olaf@31 184 UcxMapIterator i;
olaf@31 185 i.map = map;
olaf@31 186 i.cur = NULL;
olaf@31 187 i.index = 0;
olaf@31 188 return i;
olaf@31 189 }
olaf@31 190
olaf@31 191 int ucx_map_iter_next(UcxMapIterator *i, void **elm) {
olaf@31 192 UcxMapElement *e = i->cur;
olaf@31 193
olaf@31 194 if(e == NULL) {
olaf@31 195 e = i->map->map[0];
olaf@31 196 } else {
olaf@31 197 e = e->next;
olaf@31 198 }
olaf@31 199
olaf@31 200 while(i->index < i->map->size) {
olaf@31 201 if(e != NULL) {
olaf@31 202 if(e->data != NULL) {
olaf@31 203 i->cur = e;
olaf@31 204 *elm = e->data;
olaf@31 205 return 0;
olaf@31 206 }
olaf@31 207
olaf@31 208 e = e->next;
olaf@31 209 } else {
olaf@31 210 i->index++;
olaf@31 211
olaf@31 212 if(i->index < i->map->size) {
olaf@31 213 e = i->map->map[i->index];
olaf@31 214 }
olaf@31 215 }
olaf@31 216 }
olaf@31 217
olaf@31 218 return 1;
olaf@31 219 }
universe@42 220
universe@48 221 int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator,
universe@48 222 ucx_map_coder decoder, void* decdata) {
universe@42 223
universe@43 224 int c; int r, n;
universe@42 225
universe@42 226 char *key, *value;
universe@42 227
universe@43 228 while ((c = fgetc(f)) > 0) {
universe@42 229 /* Discard leading spaces and comments */
universe@43 230 if (c < 33) continue;
universe@42 231 if (c == '#' || c == '!') {
universe@42 232 while ((c = (char) fgetc(f)) > 0) {
universe@42 233 if (c == '\n') break;
universe@42 234 }
universe@42 235 continue;
universe@42 236 }
universe@42 237
universe@42 238 /* read into key buffer */
universe@42 239 n = 16;
universe@42 240 key = malloc(n);
universe@42 241 r = 0;
universe@42 242 do {
universe@42 243 if (c == '=') break;
universe@42 244 if (r > n - 2) {
universe@42 245 n *= 2;
universe@42 246 key = realloc(key, n);
universe@42 247 }
universe@42 248 key[r] = c;
universe@42 249 r++;
universe@43 250 } while ((c = fgetc(f)) > 0);
universe@43 251 if (c <= 0) {
universe@42 252 free(key);
universe@42 253 return 1;
universe@42 254 }
universe@42 255 key[r] = 0;
universe@43 256 while (key[--r] == ' ') key[r] = 0;
universe@43 257
universe@43 258 /* skip whitespaces */
universe@43 259 while ((c = fgetc(f)) > 0) {
universe@43 260 if (c > 32) break;
universe@43 261 }
universe@43 262 if (c <= 0) {
universe@43 263 free(key);
universe@43 264 return 1;
universe@43 265 }
universe@42 266
universe@42 267 /* read into value buffer */
universe@42 268 n = 64;
universe@42 269 value = malloc(n);
universe@42 270 r = 0;
universe@43 271 do {
universe@42 272 if (c == '\n') break;
universe@43 273 if (r > n - 2) {
universe@42 274 n *= 2;
universe@42 275 value = realloc(value, n);
universe@42 276 }
universe@42 277 value[r] = c;
universe@42 278 r++;
universe@43 279 } while ((c = fgetc(f)) > 0);
universe@42 280 value[r] = 0;
universe@43 281 while (value[--r] < 33) value[r] = 0;
universe@46 282
universe@48 283 if (decoder) {
universe@48 284 size_t decodedSize;
universe@48 285 void *decoded = decoder(value, decdata, &decodedSize);
universe@46 286 free(value);
universe@46 287 value = decoded;
universe@48 288 r = decodedSize;
universe@48 289 } else {
universe@48 290 r += 2;
universe@48 291 value = realloc(value, r);
universe@48 292 }
universe@48 293
universe@48 294 if (allocator.pool) {
universe@48 295 void *pooledValue = allocator.malloc(allocator.pool, r);
universe@48 296 memcpy(pooledValue, value, r);
universe@48 297 free(value);
universe@48 298 value = pooledValue;
universe@46 299 }
universe@42 300
universe@42 301 ucx_map_cstr_put(map, key, value);
universe@42 302 free(key);
universe@42 303 }
universe@42 304
universe@42 305 return 0;
universe@42 306 }
universe@42 307
universe@48 308 int ucx_map_store_enc(UcxMap *map, FILE *f,
universe@48 309 ucx_map_coder encoder, void *encdata) {
universe@42 310 UcxMapIterator iter = ucx_map_iterator(map);
universe@42 311 char *k, *v;
universe@42 312 sstr_t key, value;
universe@42 313 int written;
universe@42 314
universe@42 315 UCX_MAP_FOREACH(v, iter) {
universe@42 316 k = (char*) iter.cur->key.data;
universe@46 317 key = sstr(k);
universe@48 318 if (encoder) {
universe@48 319 size_t encodedSize;
universe@48 320 void *encoded = encoder(v, encdata, &encodedSize);
universe@48 321 value = sstrn(encoded,encodedSize - 1);
universe@48 322 } else {
universe@46 323 value = sstr(v);
universe@46 324 }
universe@42 325
universe@42 326 written = 0;
universe@42 327 written += fwrite(key.ptr, 1, key.length, f);
universe@42 328 written += fwrite(" = ", 1, 3, f);
universe@42 329 written += fwrite(value.ptr, 1, value.length, f);
universe@42 330 written += fwrite("\n", 1, 1, f);
universe@42 331
universe@48 332 if (encoder) {
universe@46 333 free(value.ptr);
universe@46 334 }
universe@46 335
universe@42 336 if (written != key.length + value.length + 4) return 1;
universe@42 337 }
universe@42 338
universe@42 339 return 0;
universe@42 340 }

mercurial