ucx/map.c

Mon, 08 Oct 2012 12:29:27 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 08 Oct 2012 12:29:27 +0200
changeset 53
e533c170bfb8
parent 52
34f50d0bada4
child 67
27e67e725d35
permissions
-rw-r--r--

added ucx_map_remove

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

mercurial