ucx/map.c

Thu, 11 Oct 2012 11:42:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 11 Oct 2012 11:42:31 +0200
changeset 67
27e67e725d35
parent 53
e533c170bfb8
child 69
fb59270b1de3
permissions
-rw-r--r--

added some qualifiers + removed pointer alias in mergesort

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

mercurial