ucx/map.c

Fri, 05 Oct 2012 16:59:14 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 05 Oct 2012 16:59:14 +0200
changeset 52
34f50d0bada4
parent 51
1c78cd19fb6b
child 53
e533c170bfb8
permissions
-rw-r--r--

added ucx_map_copy and fixed ucx_map_rehash

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@29 109 if (prev == NULL) {
universe@29 110 map->map[slot] = e;
universe@29 111 } else {
universe@29 112 prev->next = 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
olaf@20 133 void* ucx_map_get(UcxMap *map, UcxKey key) {
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@29 138 UcxMapElement *elm = map->map[key.hash%map->size];
universe@29 139 while (elm != NULL && elm->key.hash <= key.hash) {
olaf@20 140 if(elm->key.hash == key.hash) {
olaf@20 141 int n = (key.len > elm->key.len) ? elm->key.len : key.len;
universe@29 142 if (memcmp(elm->key.data, key.data, n) == 0) {
olaf@20 143 return elm->data;
olaf@20 144 }
olaf@20 145 }
olaf@20 146 elm = elm->next;
olaf@20 147 }
olaf@20 148
olaf@20 149 return NULL;
olaf@20 150 }
olaf@20 151
olaf@20 152 UcxKey ucx_key(void *data, size_t len) {
olaf@20 153 UcxKey key;
olaf@20 154 key.data = data;
olaf@20 155 key.len = len;
olaf@20 156 key.hash = ucx_hash(data, len);
olaf@20 157 return key;
olaf@20 158 }
olaf@20 159
olaf@20 160
olaf@20 161 int ucx_hash(char *data, size_t len) {
olaf@20 162 /* murmur hash 2 */
olaf@20 163
olaf@20 164 int m = 0x5bd1e995;
olaf@20 165 int r = 24;
olaf@20 166
olaf@20 167 int h = 25 ^ len;
olaf@20 168
olaf@20 169 int i = 0;
olaf@20 170 while (len >= 4) {
olaf@20 171 int k = data[i + 0] & 0xFF;
olaf@20 172 k |= (data[i + 1] & 0xFF) << 8;
olaf@20 173 k |= (data[i + 2] & 0xFF) << 16;
olaf@20 174 k |= (data[i + 3] & 0xFF) << 24;
olaf@20 175
olaf@20 176 k *= m;
olaf@20 177 k ^= k >> r;
olaf@20 178 k *= m;
olaf@20 179
olaf@20 180 h *= m;
olaf@20 181 h ^= k;
olaf@20 182
olaf@20 183 i += 4;
olaf@20 184 len -= 4;
olaf@20 185 }
olaf@20 186
olaf@20 187 switch (len) {
olaf@20 188 case 3: h ^= (data[i + 2] & 0xFF) << 16;
universe@38 189 /* no break */
olaf@20 190 case 2: h ^= (data[i + 1] & 0xFF) << 8;
universe@38 191 /* no break */
olaf@20 192 case 1: h ^= (data[i + 0] & 0xFF); h *= m;
universe@38 193 /* no break */
olaf@20 194 }
olaf@20 195
olaf@20 196 h ^= h >> 13;
olaf@20 197 h *= m;
olaf@20 198 h ^= h >> 15;
olaf@20 199
olaf@20 200 return h;
olaf@20 201 }
olaf@31 202
olaf@31 203 UcxMapIterator ucx_map_iterator(UcxMap *map) {
olaf@31 204 UcxMapIterator i;
olaf@31 205 i.map = map;
olaf@31 206 i.cur = NULL;
olaf@31 207 i.index = 0;
olaf@31 208 return i;
olaf@31 209 }
olaf@31 210
olaf@31 211 int ucx_map_iter_next(UcxMapIterator *i, void **elm) {
olaf@31 212 UcxMapElement *e = i->cur;
olaf@31 213
olaf@31 214 if(e == NULL) {
olaf@31 215 e = i->map->map[0];
olaf@31 216 } else {
olaf@31 217 e = e->next;
olaf@31 218 }
olaf@31 219
olaf@31 220 while(i->index < i->map->size) {
olaf@31 221 if(e != NULL) {
olaf@31 222 if(e->data != NULL) {
olaf@31 223 i->cur = e;
olaf@31 224 *elm = e->data;
olaf@31 225 return 0;
olaf@31 226 }
olaf@31 227
olaf@31 228 e = e->next;
olaf@31 229 } else {
olaf@31 230 i->index++;
olaf@31 231
olaf@31 232 if(i->index < i->map->size) {
olaf@31 233 e = i->map->map[i->index];
olaf@31 234 }
olaf@31 235 }
olaf@31 236 }
olaf@31 237
olaf@31 238 return 1;
olaf@31 239 }
universe@42 240
universe@48 241 int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator,
universe@48 242 ucx_map_coder decoder, void* decdata) {
universe@42 243
universe@43 244 int c; int r, n;
universe@42 245
universe@42 246 char *key, *value;
universe@42 247
universe@43 248 while ((c = fgetc(f)) > 0) {
universe@42 249 /* Discard leading spaces and comments */
universe@43 250 if (c < 33) continue;
universe@42 251 if (c == '#' || c == '!') {
universe@42 252 while ((c = (char) fgetc(f)) > 0) {
universe@42 253 if (c == '\n') break;
universe@42 254 }
universe@42 255 continue;
universe@42 256 }
universe@42 257
universe@42 258 /* read into key buffer */
universe@42 259 n = 16;
universe@42 260 key = malloc(n);
universe@42 261 r = 0;
universe@42 262 do {
universe@42 263 if (c == '=') break;
universe@42 264 if (r > n - 2) {
universe@42 265 n *= 2;
universe@42 266 key = realloc(key, n);
universe@42 267 }
universe@42 268 key[r] = c;
universe@42 269 r++;
universe@43 270 } while ((c = fgetc(f)) > 0);
universe@43 271 if (c <= 0) {
universe@42 272 free(key);
universe@42 273 return 1;
universe@42 274 }
universe@42 275 key[r] = 0;
universe@43 276 while (key[--r] == ' ') key[r] = 0;
universe@43 277
universe@43 278 /* skip whitespaces */
universe@43 279 while ((c = fgetc(f)) > 0) {
universe@43 280 if (c > 32) break;
universe@43 281 }
universe@43 282 if (c <= 0) {
universe@43 283 free(key);
universe@43 284 return 1;
universe@43 285 }
universe@42 286
universe@42 287 /* read into value buffer */
universe@42 288 n = 64;
universe@42 289 value = malloc(n);
universe@42 290 r = 0;
universe@43 291 do {
universe@42 292 if (c == '\n') break;
universe@43 293 if (r > n - 2) {
universe@42 294 n *= 2;
universe@42 295 value = realloc(value, n);
universe@42 296 }
universe@42 297 value[r] = c;
universe@42 298 r++;
universe@43 299 } while ((c = fgetc(f)) > 0);
universe@42 300 value[r] = 0;
universe@43 301 while (value[--r] < 33) value[r] = 0;
universe@46 302
universe@48 303 if (decoder) {
universe@48 304 size_t decodedSize;
universe@48 305 void *decoded = decoder(value, decdata, &decodedSize);
universe@46 306 free(value);
universe@46 307 value = decoded;
universe@48 308 r = decodedSize;
universe@48 309 } else {
universe@48 310 r += 2;
universe@48 311 value = realloc(value, r);
universe@48 312 }
universe@48 313
universe@48 314 if (allocator.pool) {
universe@48 315 void *pooledValue = allocator.malloc(allocator.pool, r);
universe@48 316 memcpy(pooledValue, value, r);
universe@48 317 free(value);
universe@48 318 value = pooledValue;
universe@46 319 }
universe@42 320
universe@42 321 ucx_map_cstr_put(map, key, value);
universe@42 322 free(key);
universe@42 323 }
universe@42 324
universe@42 325 return 0;
universe@42 326 }
universe@42 327
universe@48 328 int ucx_map_store_enc(UcxMap *map, FILE *f,
universe@48 329 ucx_map_coder encoder, void *encdata) {
universe@42 330 UcxMapIterator iter = ucx_map_iterator(map);
universe@42 331 char *k, *v;
universe@42 332 sstr_t key, value;
universe@42 333 int written;
universe@42 334
universe@42 335 UCX_MAP_FOREACH(v, iter) {
universe@42 336 k = (char*) iter.cur->key.data;
universe@46 337 key = sstr(k);
universe@48 338 if (encoder) {
universe@48 339 size_t encodedSize;
universe@48 340 void *encoded = encoder(v, encdata, &encodedSize);
universe@48 341 value = sstrn(encoded,encodedSize - 1);
universe@48 342 } else {
universe@46 343 value = sstr(v);
universe@46 344 }
universe@42 345
universe@42 346 written = 0;
universe@42 347 written += fwrite(key.ptr, 1, key.length, f);
universe@42 348 written += fwrite(" = ", 1, 3, f);
universe@42 349 written += fwrite(value.ptr, 1, value.length, f);
universe@42 350 written += fwrite("\n", 1, 1, f);
universe@42 351
universe@48 352 if (encoder) {
universe@46 353 free(value.ptr);
universe@46 354 }
universe@46 355
universe@42 356 if (written != key.length + value.length + 4) return 1;
universe@42 357 }
universe@42 358
universe@42 359 return 0;
universe@42 360 }

mercurial