ucx/map.c

Fri, 12 Jul 2013 20:50:18 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 12 Jul 2013 20:50:18 +0200
changeset 108
d2b1e67b2b48
parent 107
86b19c98b5fd
child 111
c8c59d7f4536
permissions
-rw-r--r--

new properties parser

olaf@20 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
olaf@20 3 *
universe@103 4 * Copyright 2013 Olaf Wintermann. All rights reserved.
universe@103 5 *
universe@103 6 * Redistribution and use in source and binary forms, with or without
universe@103 7 * modification, are permitted provided that the following conditions are met:
universe@103 8 *
universe@103 9 * 1. Redistributions of source code must retain the above copyright
universe@103 10 * notice, this list of conditions and the following disclaimer.
universe@103 11 *
universe@103 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@103 13 * notice, this list of conditions and the following disclaimer in the
universe@103 14 * documentation and/or other materials provided with the distribution.
universe@103 15 *
universe@103 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@103 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@103 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@103 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@103 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@103 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@103 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@103 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@103 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@103 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@103 26 * POSSIBILITY OF SUCH DAMAGE.
olaf@20 27 */
olaf@2 28
olaf@20 29 #include <stdlib.h>
olaf@20 30 #include <string.h>
olaf@20 31
olaf@20 32 #include "map.h"
olaf@20 33
olaf@20 34 UcxMap *ucx_map_new(size_t size) {
olaf@107 35 return ucx_map_new_allocator(size, NULL);
olaf@107 36 }
olaf@107 37
olaf@107 38 UcxMap *ucx_map_new_allocator(size_t size, UcxAllocator *allocator) {
olaf@45 39 if(size == 0) {
olaf@45 40 size = 16;
olaf@45 41 }
olaf@108 42
olaf@107 43 if(!allocator) {
olaf@107 44 allocator = ucx_default_allocator();
olaf@107 45 }
olaf@107 46
olaf@107 47 UcxMap *map = (UcxMap*)allocator->malloc(allocator->pool, sizeof(UcxMap));
olaf@20 48 if(map == NULL) {
olaf@20 49 return NULL;
olaf@20 50 }
olaf@107 51
olaf@107 52 map->allocator = allocator;
olaf@107 53 map->map = (UcxMapElement**)allocator->calloc(
olaf@107 54 allocator->pool,
olaf@107 55 size,
olaf@107 56 sizeof(UcxMapElement*));
olaf@20 57 if(map->map == NULL) {
olaf@107 58 allocator->free(allocator->pool, map);
olaf@20 59 return NULL;
olaf@20 60 }
olaf@20 61 map->size = size;
olaf@45 62 map->count = 0;
olaf@20 63
olaf@20 64 return map;
olaf@20 65 }
olaf@20 66
olaf@70 67 void ucx_map_free_elmlist(UcxMap *map) {
universe@29 68 for (size_t n = 0 ; n < map->size ; n++) {
universe@29 69 UcxMapElement *elem = map->map[n];
universe@29 70 if (elem != NULL) {
universe@29 71 do {
universe@29 72 UcxMapElement *next = elem->next;
olaf@107 73 map->allocator->free(map->allocator->pool, elem->key.data);
universe@29 74 free(elem);
universe@29 75 elem = next;
universe@29 76 } while (elem != NULL);
universe@29 77 }
universe@29 78 }
olaf@107 79 map->allocator->free(map->allocator->pool, map->map);
olaf@70 80 }
olaf@70 81
olaf@70 82 void ucx_map_free(UcxMap *map) {
olaf@70 83 ucx_map_free_elmlist(map);
olaf@107 84 map->allocator->free(map->allocator->pool, map);
universe@29 85 }
universe@29 86
universe@67 87 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
universe@67 88 copy_func fnc, void *data) {
olaf@52 89 UcxMapIterator i = ucx_map_iterator(from);
olaf@52 90 void *value;
olaf@52 91 UCX_MAP_FOREACH(value, i) {
olaf@52 92 int ret = ucx_map_put(to, i.cur->key, fnc ? fnc(value, data) : value);
olaf@52 93 if(ret != 0) {
olaf@52 94 return 1;
olaf@52 95 }
olaf@52 96 }
olaf@52 97 return 0;
olaf@52 98 }
olaf@52 99
olaf@44 100 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) {
universe@51 101 size_t bs = (map->count * 5) >> 1;
olaf@45 102 UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size);
olaf@52 103 if(newmap == NULL) {
olaf@52 104 return NULL;
olaf@44 105 }
olaf@52 106 ucx_map_copy(map, newmap, fnc, data);
olaf@44 107 return newmap;
olaf@44 108 }
olaf@44 109
olaf@52 110 int ucx_map_rehash(UcxMap *map) {
universe@51 111 size_t load = (map->size * 3) >> 2;
universe@51 112 if (map->count > load) {
olaf@52 113 UcxMap oldmap;
olaf@52 114 oldmap.map = map->map;
olaf@52 115 oldmap.size = map->size;
olaf@52 116 oldmap.count = map->count;
olaf@107 117 oldmap.allocator = map->allocator;
olaf@52 118
olaf@52 119 map->size = (map->count * 5) >> 1;
olaf@107 120 map->map = (UcxMapElement**)map->allocator->calloc(
olaf@107 121 map->allocator->pool,
olaf@107 122 map->size,
olaf@107 123 sizeof(UcxMapElement*));
olaf@52 124 if(map->map == NULL) {
olaf@52 125 *map = oldmap;
olaf@52 126 return 1;
olaf@52 127 }
olaf@52 128 map->count = 0;
olaf@52 129 ucx_map_copy(&oldmap, map, NULL, NULL);
olaf@70 130
olaf@70 131 /* free the UcxMapElement list of oldmap */
olaf@70 132 ucx_map_free_elmlist(&oldmap);
universe@51 133 }
olaf@52 134 return 0;
universe@51 135 }
universe@51 136
olaf@20 137 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
olaf@107 138 UcxAllocator *allocator = map->allocator;
olaf@107 139
olaf@20 140 if(key.hash == 0) {
olaf@20 141 key.hash = ucx_hash((char*)key.data, key.len);
olaf@20 142 }
olaf@20 143
universe@29 144 size_t slot = key.hash%map->size;
universe@67 145 UcxMapElement *restrict elm = map->map[slot];
universe@67 146 UcxMapElement *restrict prev = NULL;
universe@29 147
universe@29 148 while (elm != NULL && elm->key.hash < key.hash) {
universe@29 149 prev = elm;
universe@29 150 elm = elm->next;
universe@29 151 }
universe@29 152
universe@29 153 if (elm == NULL || elm->key.hash != key.hash) {
olaf@107 154 UcxMapElement *e = (UcxMapElement*)allocator->malloc(
olaf@107 155 allocator->pool,
olaf@107 156 sizeof(UcxMapElement));
olaf@20 157 if(e == NULL) {
olaf@20 158 return -1;
olaf@20 159 }
olaf@30 160 e->key.data = NULL;
universe@53 161 if (prev) {
universe@53 162 prev->next = e;
universe@53 163 } else {
universe@29 164 map->map[slot] = e;
universe@29 165 }
universe@29 166 e->next = elm;
olaf@20 167 elm = e;
olaf@20 168 }
universe@29 169
olaf@30 170 if(elm->key.data == NULL) {
olaf@107 171 void *kd = allocator->malloc(allocator->pool, key.len);
olaf@30 172 if (kd == NULL) {
olaf@30 173 return -1;
olaf@30 174 }
olaf@30 175 memcpy(kd, key.data, key.len);
olaf@30 176 key.data = kd;
olaf@30 177 elm->key = key;
olaf@45 178 map->count++;
olaf@30 179 }
olaf@20 180 elm->data = data;
olaf@20 181
olaf@20 182 return 0;
olaf@20 183 }
olaf@20 184
universe@53 185 void* ucx_map_get_and_remove(UcxMap *map, UcxKey key, _Bool remove) {
olaf@20 186 if(key.hash == 0) {
olaf@20 187 key.hash = ucx_hash((char*)key.data, key.len);
olaf@20 188 }
olaf@20 189
universe@53 190 size_t slot = key.hash%map->size;
universe@67 191 UcxMapElement *restrict elm = map->map[slot];
universe@67 192 UcxMapElement *restrict pelm = NULL;
universe@53 193 while (elm && elm->key.hash <= key.hash) {
olaf@20 194 if(elm->key.hash == key.hash) {
olaf@20 195 int n = (key.len > elm->key.len) ? elm->key.len : key.len;
universe@29 196 if (memcmp(elm->key.data, key.data, n) == 0) {
universe@53 197 void *data = elm->data;
universe@53 198 if (remove) {
universe@53 199 if (pelm) {
universe@53 200 pelm->next = elm->next;
universe@53 201 } else {
universe@53 202 map->map[slot] = elm->next;
universe@53 203 }
olaf@107 204 map->allocator->free(map->allocator->pool, elm);
universe@53 205 map->count--;
universe@53 206 }
universe@53 207
universe@53 208 return data;
olaf@20 209 }
olaf@20 210 }
universe@53 211 pelm = elm;
universe@53 212 elm = pelm->next;
olaf@20 213 }
olaf@20 214
olaf@20 215 return NULL;
olaf@20 216 }
olaf@20 217
universe@53 218 void *ucx_map_get(UcxMap *map, UcxKey key) {
universe@53 219 return ucx_map_get_and_remove(map, key, 0);
universe@53 220 }
universe@53 221
universe@53 222 void *ucx_map_remove(UcxMap *map, UcxKey key) {
universe@53 223 return ucx_map_get_and_remove(map, key, 1);
universe@53 224 }
universe@53 225
olaf@20 226 UcxKey ucx_key(void *data, size_t len) {
olaf@20 227 UcxKey key;
olaf@20 228 key.data = data;
olaf@20 229 key.len = len;
universe@69 230 key.hash = ucx_hash((const char*) data, len);
olaf@20 231 return key;
olaf@20 232 }
olaf@20 233
olaf@20 234
universe@67 235 int ucx_hash(const char *data, size_t len) {
olaf@20 236 /* murmur hash 2 */
olaf@20 237
olaf@20 238 int m = 0x5bd1e995;
olaf@20 239 int r = 24;
olaf@20 240
olaf@20 241 int h = 25 ^ len;
olaf@20 242
olaf@20 243 int i = 0;
olaf@20 244 while (len >= 4) {
olaf@20 245 int k = data[i + 0] & 0xFF;
olaf@20 246 k |= (data[i + 1] & 0xFF) << 8;
olaf@20 247 k |= (data[i + 2] & 0xFF) << 16;
olaf@20 248 k |= (data[i + 3] & 0xFF) << 24;
olaf@20 249
olaf@20 250 k *= m;
olaf@20 251 k ^= k >> r;
olaf@20 252 k *= m;
olaf@20 253
olaf@20 254 h *= m;
olaf@20 255 h ^= k;
olaf@20 256
olaf@20 257 i += 4;
olaf@20 258 len -= 4;
olaf@20 259 }
olaf@20 260
olaf@20 261 switch (len) {
olaf@20 262 case 3: h ^= (data[i + 2] & 0xFF) << 16;
universe@38 263 /* no break */
olaf@20 264 case 2: h ^= (data[i + 1] & 0xFF) << 8;
universe@38 265 /* no break */
olaf@20 266 case 1: h ^= (data[i + 0] & 0xFF); h *= m;
universe@38 267 /* no break */
olaf@20 268 }
olaf@20 269
olaf@20 270 h ^= h >> 13;
olaf@20 271 h *= m;
olaf@20 272 h ^= h >> 15;
olaf@20 273
olaf@20 274 return h;
olaf@20 275 }
olaf@31 276
olaf@31 277 UcxMapIterator ucx_map_iterator(UcxMap *map) {
olaf@31 278 UcxMapIterator i;
olaf@31 279 i.map = map;
olaf@31 280 i.cur = NULL;
olaf@31 281 i.index = 0;
olaf@31 282 return i;
olaf@31 283 }
olaf@31 284
olaf@31 285 int ucx_map_iter_next(UcxMapIterator *i, void **elm) {
olaf@31 286 UcxMapElement *e = i->cur;
olaf@31 287
olaf@31 288 if(e == NULL) {
olaf@31 289 e = i->map->map[0];
olaf@31 290 } else {
olaf@31 291 e = e->next;
olaf@31 292 }
olaf@31 293
olaf@31 294 while(i->index < i->map->size) {
olaf@31 295 if(e != NULL) {
olaf@31 296 if(e->data != NULL) {
olaf@31 297 i->cur = e;
olaf@31 298 *elm = e->data;
olaf@31 299 return 0;
olaf@31 300 }
olaf@31 301
olaf@31 302 e = e->next;
olaf@31 303 } else {
olaf@31 304 i->index++;
olaf@31 305
olaf@31 306 if(i->index < i->map->size) {
olaf@31 307 e = i->map->map[i->index];
olaf@31 308 }
olaf@31 309 }
olaf@31 310 }
olaf@31 311
olaf@31 312 return 1;
olaf@31 313 }
universe@42 314
universe@48 315 int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator,
universe@48 316 ucx_map_coder decoder, void* decdata) {
universe@42 317
universe@43 318 int c; int r, n;
universe@42 319
universe@42 320 char *key, *value;
universe@42 321
universe@43 322 while ((c = fgetc(f)) > 0) {
universe@42 323 /* Discard leading spaces and comments */
universe@43 324 if (c < 33) continue;
universe@42 325 if (c == '#' || c == '!') {
universe@42 326 while ((c = (char) fgetc(f)) > 0) {
universe@42 327 if (c == '\n') break;
universe@42 328 }
universe@42 329 continue;
universe@42 330 }
universe@42 331
universe@42 332 /* read into key buffer */
universe@42 333 n = 16;
universe@69 334 key = (char*) malloc(n);
universe@42 335 r = 0;
universe@42 336 do {
universe@42 337 if (c == '=') break;
universe@42 338 if (r > n - 2) {
universe@42 339 n *= 2;
universe@69 340 key = (char*) realloc(key, n);
universe@42 341 }
universe@42 342 key[r] = c;
universe@42 343 r++;
universe@43 344 } while ((c = fgetc(f)) > 0);
universe@43 345 if (c <= 0) {
universe@42 346 free(key);
universe@42 347 return 1;
universe@42 348 }
universe@42 349 key[r] = 0;
universe@43 350 while (key[--r] == ' ') key[r] = 0;
universe@43 351
universe@43 352 /* skip whitespaces */
universe@43 353 while ((c = fgetc(f)) > 0) {
universe@43 354 if (c > 32) break;
universe@43 355 }
universe@43 356 if (c <= 0) {
universe@43 357 free(key);
universe@43 358 return 1;
universe@43 359 }
universe@42 360
universe@42 361 /* read into value buffer */
universe@42 362 n = 64;
universe@69 363 value = (char*) malloc(n);
universe@42 364 r = 0;
universe@43 365 do {
universe@42 366 if (c == '\n') break;
universe@43 367 if (r > n - 2) {
universe@42 368 n *= 2;
universe@69 369 value = (char*) realloc(value, n);
universe@42 370 }
universe@42 371 value[r] = c;
universe@42 372 r++;
universe@43 373 } while ((c = fgetc(f)) > 0);
universe@42 374 value[r] = 0;
universe@43 375 while (value[--r] < 33) value[r] = 0;
universe@46 376
universe@48 377 if (decoder) {
universe@48 378 size_t decodedSize;
universe@48 379 void *decoded = decoder(value, decdata, &decodedSize);
universe@46 380 free(value);
universe@69 381 value = (char*) decoded;
universe@48 382 r = decodedSize;
universe@48 383 } else {
universe@48 384 r += 2;
universe@69 385 value = (char*) realloc(value, r);
universe@48 386 }
universe@48 387
universe@48 388 if (allocator.pool) {
universe@48 389 void *pooledValue = allocator.malloc(allocator.pool, r);
universe@48 390 memcpy(pooledValue, value, r);
universe@48 391 free(value);
universe@69 392 value = (char*) pooledValue;
universe@46 393 }
universe@42 394
universe@42 395 ucx_map_cstr_put(map, key, value);
universe@42 396 free(key);
universe@42 397 }
universe@42 398
universe@42 399 return 0;
universe@42 400 }
universe@42 401
universe@48 402 int ucx_map_store_enc(UcxMap *map, FILE *f,
universe@48 403 ucx_map_coder encoder, void *encdata) {
universe@42 404 UcxMapIterator iter = ucx_map_iterator(map);
universe@42 405 char *k, *v;
universe@42 406 sstr_t key, value;
universe@95 407 size_t written;
universe@42 408
universe@42 409 UCX_MAP_FOREACH(v, iter) {
universe@42 410 k = (char*) iter.cur->key.data;
olaf@79 411 key = sstrn(k, iter.cur->key.len);
universe@48 412 if (encoder) {
universe@48 413 size_t encodedSize;
universe@48 414 void *encoded = encoder(v, encdata, &encodedSize);
universe@69 415 value = sstrn((char*) encoded,encodedSize - 1);
universe@48 416 } else {
universe@46 417 value = sstr(v);
universe@46 418 }
universe@42 419
universe@42 420 written = 0;
universe@42 421 written += fwrite(key.ptr, 1, key.length, f);
universe@42 422 written += fwrite(" = ", 1, 3, f);
universe@42 423 written += fwrite(value.ptr, 1, value.length, f);
universe@42 424 written += fwrite("\n", 1, 1, f);
universe@42 425
universe@48 426 if (encoder) {
universe@46 427 free(value.ptr);
universe@46 428 }
universe@46 429
universe@42 430 if (written != key.length + value.length + 4) return 1;
universe@42 431 }
universe@42 432
universe@42 433 return 0;
universe@42 434 }

mercurial