ucx/map.c

Mon, 12 Aug 2013 14:43:22 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 12 Aug 2013 14:43:22 +0200
changeset 139
dddb9348ea42
parent 138
7800811078b8
parent 137
81a02ad99388
child 147
1aa598f36872
permissions
-rw-r--r--

8-) f**k

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@137 35 return ucx_map_new_a(NULL, size);
olaf@107 36 }
olaf@107 37
olaf@137 38 UcxMap *ucx_map_new_a(UcxAllocator *allocator, size_t size) {
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));
universe@139 48 if (!map) {
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
universe@116 67 static 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);
olaf@112 74 map->allocator->free(map->allocator->pool, 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@111 91 UCX_MAP_FOREACH(key, value, i) {
universe@138 92 if (ucx_map_put(to, key, fnc ? fnc(value, data) : value)) {
olaf@52 93 return 1;
olaf@52 94 }
olaf@52 95 }
olaf@52 96 return 0;
olaf@52 97 }
olaf@52 98
olaf@44 99 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) {
universe@51 100 size_t bs = (map->count * 5) >> 1;
olaf@45 101 UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size);
universe@138 102 if (!newmap) {
olaf@52 103 return NULL;
olaf@44 104 }
olaf@52 105 ucx_map_copy(map, newmap, fnc, data);
olaf@44 106 return newmap;
olaf@44 107 }
olaf@44 108
olaf@52 109 int ucx_map_rehash(UcxMap *map) {
universe@51 110 size_t load = (map->size * 3) >> 2;
universe@51 111 if (map->count > load) {
olaf@52 112 UcxMap oldmap;
olaf@52 113 oldmap.map = map->map;
olaf@52 114 oldmap.size = map->size;
olaf@52 115 oldmap.count = map->count;
olaf@107 116 oldmap.allocator = map->allocator;
olaf@52 117
olaf@52 118 map->size = (map->count * 5) >> 1;
olaf@107 119 map->map = (UcxMapElement**)map->allocator->calloc(
olaf@107 120 map->allocator->pool,
olaf@107 121 map->size,
olaf@107 122 sizeof(UcxMapElement*));
universe@138 123 if (!map->map) {
olaf@52 124 *map = oldmap;
olaf@52 125 return 1;
olaf@52 126 }
olaf@52 127 map->count = 0;
olaf@52 128 ucx_map_copy(&oldmap, map, NULL, NULL);
olaf@70 129
olaf@70 130 /* free the UcxMapElement list of oldmap */
olaf@70 131 ucx_map_free_elmlist(&oldmap);
universe@51 132 }
olaf@52 133 return 0;
universe@51 134 }
universe@51 135
olaf@20 136 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
olaf@107 137 UcxAllocator *allocator = map->allocator;
olaf@107 138
universe@138 139 if (key.hash == 0) {
olaf@20 140 key.hash = ucx_hash((char*)key.data, key.len);
olaf@20 141 }
olaf@20 142
universe@29 143 size_t slot = key.hash%map->size;
universe@67 144 UcxMapElement *restrict elm = map->map[slot];
universe@67 145 UcxMapElement *restrict prev = NULL;
universe@29 146
universe@138 147 while (elm && elm->key.hash < key.hash) {
universe@29 148 prev = elm;
universe@29 149 elm = elm->next;
universe@29 150 }
universe@29 151
universe@138 152 if (!elm || elm->key.hash != key.hash) {
olaf@107 153 UcxMapElement *e = (UcxMapElement*)allocator->malloc(
olaf@107 154 allocator->pool,
olaf@107 155 sizeof(UcxMapElement));
universe@138 156 if (!e) {
olaf@20 157 return -1;
olaf@20 158 }
olaf@30 159 e->key.data = NULL;
universe@53 160 if (prev) {
universe@53 161 prev->next = e;
universe@53 162 } else {
universe@29 163 map->map[slot] = e;
universe@29 164 }
universe@29 165 e->next = elm;
olaf@20 166 elm = e;
olaf@20 167 }
universe@29 168
universe@138 169 if (!elm->key.data) {
olaf@107 170 void *kd = allocator->malloc(allocator->pool, key.len);
universe@138 171 if (!kd) {
olaf@30 172 return -1;
olaf@30 173 }
olaf@30 174 memcpy(kd, key.data, key.len);
olaf@30 175 key.data = kd;
olaf@30 176 elm->key = key;
olaf@45 177 map->count++;
olaf@30 178 }
olaf@20 179 elm->data = data;
olaf@20 180
olaf@20 181 return 0;
olaf@20 182 }
olaf@20 183
universe@53 184 void* ucx_map_get_and_remove(UcxMap *map, UcxKey key, _Bool remove) {
olaf@20 185 if(key.hash == 0) {
olaf@20 186 key.hash = ucx_hash((char*)key.data, key.len);
olaf@20 187 }
olaf@20 188
universe@53 189 size_t slot = key.hash%map->size;
universe@67 190 UcxMapElement *restrict elm = map->map[slot];
universe@67 191 UcxMapElement *restrict pelm = NULL;
universe@53 192 while (elm && elm->key.hash <= key.hash) {
olaf@20 193 if(elm->key.hash == key.hash) {
olaf@20 194 int n = (key.len > elm->key.len) ? elm->key.len : key.len;
universe@29 195 if (memcmp(elm->key.data, key.data, n) == 0) {
universe@53 196 void *data = elm->data;
universe@53 197 if (remove) {
universe@53 198 if (pelm) {
universe@53 199 pelm->next = elm->next;
universe@53 200 } else {
universe@53 201 map->map[slot] = elm->next;
universe@53 202 }
olaf@107 203 map->allocator->free(map->allocator->pool, elm);
universe@53 204 map->count--;
universe@53 205 }
universe@53 206
universe@53 207 return data;
olaf@20 208 }
olaf@20 209 }
universe@53 210 pelm = elm;
universe@53 211 elm = pelm->next;
olaf@20 212 }
olaf@20 213
olaf@20 214 return NULL;
olaf@20 215 }
olaf@20 216
universe@53 217 void *ucx_map_get(UcxMap *map, UcxKey key) {
universe@53 218 return ucx_map_get_and_remove(map, key, 0);
universe@53 219 }
universe@53 220
universe@53 221 void *ucx_map_remove(UcxMap *map, UcxKey key) {
universe@53 222 return ucx_map_get_and_remove(map, key, 1);
universe@53 223 }
universe@53 224
olaf@20 225 UcxKey ucx_key(void *data, size_t len) {
olaf@20 226 UcxKey key;
olaf@20 227 key.data = data;
olaf@20 228 key.len = len;
universe@69 229 key.hash = ucx_hash((const char*) data, len);
olaf@20 230 return key;
olaf@20 231 }
olaf@20 232
olaf@20 233
universe@67 234 int ucx_hash(const char *data, size_t len) {
olaf@20 235 /* murmur hash 2 */
olaf@20 236
olaf@20 237 int m = 0x5bd1e995;
olaf@20 238 int r = 24;
olaf@20 239
olaf@20 240 int h = 25 ^ len;
olaf@20 241
olaf@20 242 int i = 0;
olaf@20 243 while (len >= 4) {
olaf@20 244 int k = data[i + 0] & 0xFF;
olaf@20 245 k |= (data[i + 1] & 0xFF) << 8;
olaf@20 246 k |= (data[i + 2] & 0xFF) << 16;
olaf@20 247 k |= (data[i + 3] & 0xFF) << 24;
olaf@20 248
olaf@20 249 k *= m;
olaf@20 250 k ^= k >> r;
olaf@20 251 k *= m;
olaf@20 252
olaf@20 253 h *= m;
olaf@20 254 h ^= k;
olaf@20 255
olaf@20 256 i += 4;
olaf@20 257 len -= 4;
olaf@20 258 }
olaf@20 259
olaf@20 260 switch (len) {
olaf@20 261 case 3: h ^= (data[i + 2] & 0xFF) << 16;
universe@38 262 /* no break */
olaf@20 263 case 2: h ^= (data[i + 1] & 0xFF) << 8;
universe@38 264 /* no break */
olaf@20 265 case 1: h ^= (data[i + 0] & 0xFF); h *= m;
universe@38 266 /* no break */
olaf@20 267 }
olaf@20 268
olaf@20 269 h ^= h >> 13;
olaf@20 270 h *= m;
olaf@20 271 h ^= h >> 15;
olaf@20 272
olaf@20 273 return h;
olaf@20 274 }
olaf@31 275
olaf@31 276 UcxMapIterator ucx_map_iterator(UcxMap *map) {
olaf@31 277 UcxMapIterator i;
olaf@31 278 i.map = map;
olaf@31 279 i.cur = NULL;
olaf@31 280 i.index = 0;
olaf@31 281 return i;
olaf@31 282 }
olaf@31 283
olaf@111 284 int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm) {
olaf@31 285 UcxMapElement *e = i->cur;
olaf@31 286
universe@138 287 if (e) {
universe@138 288 e = e->next;
universe@138 289 } else {
olaf@31 290 e = i->map->map[0];
olaf@31 291 }
olaf@31 292
universe@138 293 while (i->index < i->map->size) {
universe@138 294 if (e) {
universe@138 295 if (e->data) {
olaf@31 296 i->cur = e;
olaf@31 297 *elm = e->data;
olaf@111 298 *key = e->key;
universe@138 299 return 1;
olaf@31 300 }
olaf@31 301
olaf@31 302 e = e->next;
olaf@31 303 } else {
olaf@31 304 i->index++;
olaf@31 305
universe@138 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
universe@138 312 return 0;
olaf@31 313 }
universe@42 314

mercurial