ucx/map.c

Fri, 05 Oct 2012 11:52:53 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 05 Oct 2012 11:52:53 +0200
changeset 48
621a4430c404
parent 46
48ca036d7d9c
child 51
1c78cd19fb6b
permissions
-rw-r--r--

map can now load values from file into pooled memory

use with care when using a decoder that also allocates memory

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

mercurial