Fri, 20 Dec 2019 14:29:41 +0100
adds proper cmake build targets
olaf@20 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
olaf@20 | 3 | * |
universe@259 | 4 | * Copyright 2017 Mike Becker, 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 | |
universe@251 | 29 | #include "ucx/map.h" |
universe@251 | 30 | |
olaf@20 | 31 | #include <stdlib.h> |
olaf@20 | 32 | #include <string.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 | |
universe@173 | 47 | UcxMap *map = (UcxMap*)almalloc(allocator, sizeof(UcxMap)); |
universe@139 | 48 | if (!map) { |
olaf@20 | 49 | return NULL; |
olaf@20 | 50 | } |
olaf@107 | 51 | |
olaf@107 | 52 | map->allocator = allocator; |
universe@173 | 53 | map->map = (UcxMapElement**)alcalloc( |
universe@173 | 54 | allocator, size, sizeof(UcxMapElement*)); |
olaf@20 | 55 | if(map->map == NULL) { |
universe@173 | 56 | alfree(allocator, map); |
olaf@20 | 57 | return NULL; |
olaf@20 | 58 | } |
olaf@20 | 59 | map->size = size; |
olaf@45 | 60 | map->count = 0; |
olaf@20 | 61 | |
olaf@20 | 62 | return map; |
olaf@20 | 63 | } |
olaf@20 | 64 | |
universe@206 | 65 | static void ucx_map_free_elmlist_contents(UcxMap *map) { |
universe@29 | 66 | for (size_t n = 0 ; n < map->size ; n++) { |
universe@29 | 67 | UcxMapElement *elem = map->map[n]; |
universe@29 | 68 | if (elem != NULL) { |
universe@29 | 69 | do { |
universe@29 | 70 | UcxMapElement *next = elem->next; |
universe@173 | 71 | alfree(map->allocator, elem->key.data); |
universe@173 | 72 | alfree(map->allocator, elem); |
universe@29 | 73 | elem = next; |
universe@29 | 74 | } while (elem != NULL); |
universe@29 | 75 | } |
universe@29 | 76 | } |
olaf@70 | 77 | } |
olaf@70 | 78 | |
olaf@70 | 79 | void ucx_map_free(UcxMap *map) { |
universe@206 | 80 | ucx_map_free_elmlist_contents(map); |
universe@206 | 81 | alfree(map->allocator, map->map); |
universe@173 | 82 | alfree(map->allocator, map); |
universe@29 | 83 | } |
universe@29 | 84 | |
universe@209 | 85 | void ucx_map_free_content(UcxMap *map, ucx_destructor destr) { |
universe@208 | 86 | UcxMapIterator iter = ucx_map_iterator(map); |
universe@208 | 87 | void *val; |
universe@208 | 88 | UCX_MAP_FOREACH(key, val, iter) { |
universe@277 | 89 | if (destr) { |
universe@277 | 90 | destr(val); |
universe@277 | 91 | } else { |
universe@287 | 92 | alfree(map->allocator, val); |
universe@277 | 93 | } |
universe@208 | 94 | } |
universe@208 | 95 | } |
universe@208 | 96 | |
universe@206 | 97 | void ucx_map_clear(UcxMap *map) { |
universe@207 | 98 | if (map->count == 0) { |
universe@207 | 99 | return; // nothing to do |
universe@207 | 100 | } |
universe@206 | 101 | ucx_map_free_elmlist_contents(map); |
universe@206 | 102 | memset(map->map, 0, map->size*sizeof(UcxMapElement*)); |
universe@206 | 103 | map->count = 0; |
universe@206 | 104 | } |
universe@206 | 105 | |
universe@374 | 106 | int ucx_map_copy(UcxMap const *from, UcxMap *to, copy_func fnc, void *data) { |
olaf@52 | 107 | UcxMapIterator i = ucx_map_iterator(from); |
olaf@52 | 108 | void *value; |
olaf@111 | 109 | UCX_MAP_FOREACH(key, value, i) { |
universe@138 | 110 | if (ucx_map_put(to, key, fnc ? fnc(value, data) : value)) { |
olaf@52 | 111 | return 1; |
olaf@52 | 112 | } |
olaf@52 | 113 | } |
olaf@52 | 114 | return 0; |
olaf@52 | 115 | } |
olaf@52 | 116 | |
universe@374 | 117 | UcxMap *ucx_map_clone(UcxMap const *map, copy_func fnc, void *data) { |
universe@374 | 118 | return ucx_map_clone_a(ucx_default_allocator(), map, fnc, data); |
universe@374 | 119 | } |
universe@374 | 120 | |
universe@374 | 121 | UcxMap *ucx_map_clone_a(UcxAllocator *allocator, |
universe@374 | 122 | UcxMap const *map, copy_func fnc, void *data) { |
universe@51 | 123 | size_t bs = (map->count * 5) >> 1; |
universe@374 | 124 | UcxMap *newmap = ucx_map_new_a(allocator, bs > map->size ? bs : map->size); |
universe@138 | 125 | if (!newmap) { |
olaf@52 | 126 | return NULL; |
olaf@44 | 127 | } |
olaf@52 | 128 | ucx_map_copy(map, newmap, fnc, data); |
olaf@44 | 129 | return newmap; |
olaf@44 | 130 | } |
olaf@44 | 131 | |
olaf@52 | 132 | int ucx_map_rehash(UcxMap *map) { |
universe@51 | 133 | size_t load = (map->size * 3) >> 2; |
universe@51 | 134 | if (map->count > load) { |
olaf@52 | 135 | UcxMap oldmap; |
olaf@52 | 136 | oldmap.map = map->map; |
olaf@52 | 137 | oldmap.size = map->size; |
olaf@52 | 138 | oldmap.count = map->count; |
olaf@107 | 139 | oldmap.allocator = map->allocator; |
olaf@52 | 140 | |
olaf@52 | 141 | map->size = (map->count * 5) >> 1; |
universe@173 | 142 | map->map = (UcxMapElement**)alcalloc( |
universe@173 | 143 | map->allocator, map->size, sizeof(UcxMapElement*)); |
universe@138 | 144 | if (!map->map) { |
olaf@52 | 145 | *map = oldmap; |
olaf@52 | 146 | return 1; |
olaf@52 | 147 | } |
olaf@52 | 148 | map->count = 0; |
olaf@52 | 149 | ucx_map_copy(&oldmap, map, NULL, NULL); |
olaf@70 | 150 | |
olaf@70 | 151 | /* free the UcxMapElement list of oldmap */ |
universe@206 | 152 | ucx_map_free_elmlist_contents(&oldmap); |
universe@206 | 153 | alfree(map->allocator, oldmap.map); |
universe@51 | 154 | } |
olaf@52 | 155 | return 0; |
universe@51 | 156 | } |
universe@51 | 157 | |
olaf@20 | 158 | int ucx_map_put(UcxMap *map, UcxKey key, void *data) { |
olaf@107 | 159 | UcxAllocator *allocator = map->allocator; |
olaf@107 | 160 | |
universe@138 | 161 | if (key.hash == 0) { |
universe@328 | 162 | key.hash = ucx_hash((const char*)key.data, key.len); |
olaf@20 | 163 | } |
universe@327 | 164 | |
universe@327 | 165 | struct UcxMapKey mapkey; |
universe@327 | 166 | mapkey.hash = key.hash; |
olaf@20 | 167 | |
universe@327 | 168 | size_t slot = mapkey.hash%map->size; |
universe@253 | 169 | UcxMapElement *elm = map->map[slot]; |
universe@253 | 170 | UcxMapElement *prev = NULL; |
universe@29 | 171 | |
universe@327 | 172 | while (elm && elm->key.hash < mapkey.hash) { |
universe@29 | 173 | prev = elm; |
universe@29 | 174 | elm = elm->next; |
universe@29 | 175 | } |
universe@29 | 176 | |
universe@327 | 177 | if (!elm || elm->key.hash != mapkey.hash) { |
universe@173 | 178 | UcxMapElement *e = (UcxMapElement*)almalloc( |
universe@173 | 179 | allocator, sizeof(UcxMapElement)); |
universe@138 | 180 | if (!e) { |
olaf@20 | 181 | return -1; |
olaf@20 | 182 | } |
olaf@30 | 183 | e->key.data = NULL; |
universe@53 | 184 | if (prev) { |
universe@53 | 185 | prev->next = e; |
universe@53 | 186 | } else { |
universe@29 | 187 | map->map[slot] = e; |
universe@29 | 188 | } |
universe@29 | 189 | e->next = elm; |
olaf@20 | 190 | elm = e; |
olaf@20 | 191 | } |
universe@29 | 192 | |
universe@138 | 193 | if (!elm->key.data) { |
universe@173 | 194 | void *kd = almalloc(allocator, key.len); |
universe@138 | 195 | if (!kd) { |
olaf@30 | 196 | return -1; |
olaf@30 | 197 | } |
olaf@30 | 198 | memcpy(kd, key.data, key.len); |
universe@327 | 199 | mapkey.data = kd; |
universe@327 | 200 | mapkey.len = key.len; |
universe@327 | 201 | elm->key = mapkey; |
olaf@45 | 202 | map->count++; |
olaf@30 | 203 | } |
olaf@20 | 204 | elm->data = data; |
olaf@20 | 205 | |
olaf@20 | 206 | return 0; |
olaf@20 | 207 | } |
olaf@20 | 208 | |
universe@253 | 209 | static void* ucx_map_get_and_remove(UcxMap *map, UcxKey key, int remove) { |
olaf@20 | 210 | if(key.hash == 0) { |
universe@328 | 211 | key.hash = ucx_hash((const char*)key.data, key.len); |
olaf@20 | 212 | } |
olaf@20 | 213 | |
universe@53 | 214 | size_t slot = key.hash%map->size; |
universe@253 | 215 | UcxMapElement *elm = map->map[slot]; |
universe@253 | 216 | UcxMapElement *pelm = NULL; |
universe@53 | 217 | while (elm && elm->key.hash <= key.hash) { |
olaf@20 | 218 | if(elm->key.hash == key.hash) { |
olaf@20 | 219 | int n = (key.len > elm->key.len) ? elm->key.len : key.len; |
universe@29 | 220 | if (memcmp(elm->key.data, key.data, n) == 0) { |
universe@53 | 221 | void *data = elm->data; |
universe@53 | 222 | if (remove) { |
universe@53 | 223 | if (pelm) { |
universe@53 | 224 | pelm->next = elm->next; |
universe@53 | 225 | } else { |
universe@53 | 226 | map->map[slot] = elm->next; |
universe@53 | 227 | } |
universe@173 | 228 | alfree(map->allocator, elm->key.data); |
universe@173 | 229 | alfree(map->allocator, elm); |
universe@53 | 230 | map->count--; |
universe@53 | 231 | } |
universe@53 | 232 | |
universe@53 | 233 | return data; |
olaf@20 | 234 | } |
olaf@20 | 235 | } |
universe@53 | 236 | pelm = elm; |
universe@53 | 237 | elm = pelm->next; |
olaf@20 | 238 | } |
olaf@20 | 239 | |
olaf@20 | 240 | return NULL; |
olaf@20 | 241 | } |
olaf@20 | 242 | |
universe@374 | 243 | void *ucx_map_get(UcxMap const *map, UcxKey key) { |
universe@374 | 244 | return ucx_map_get_and_remove((UcxMap *)map, key, 0); |
universe@53 | 245 | } |
universe@53 | 246 | |
universe@53 | 247 | void *ucx_map_remove(UcxMap *map, UcxKey key) { |
universe@53 | 248 | return ucx_map_get_and_remove(map, key, 1); |
universe@53 | 249 | } |
universe@53 | 250 | |
universe@327 | 251 | UcxKey ucx_key(const void *data, size_t len) { |
olaf@20 | 252 | UcxKey key; |
olaf@20 | 253 | key.data = data; |
olaf@20 | 254 | key.len = len; |
universe@328 | 255 | key.hash = ucx_hash((const char*)data, len); |
olaf@20 | 256 | return key; |
olaf@20 | 257 | } |
olaf@20 | 258 | |
olaf@20 | 259 | |
universe@67 | 260 | int ucx_hash(const char *data, size_t len) { |
olaf@20 | 261 | /* murmur hash 2 */ |
olaf@20 | 262 | |
olaf@20 | 263 | int m = 0x5bd1e995; |
olaf@20 | 264 | int r = 24; |
olaf@20 | 265 | |
olaf@20 | 266 | int h = 25 ^ len; |
olaf@20 | 267 | |
olaf@20 | 268 | int i = 0; |
olaf@20 | 269 | while (len >= 4) { |
olaf@20 | 270 | int k = data[i + 0] & 0xFF; |
olaf@20 | 271 | k |= (data[i + 1] & 0xFF) << 8; |
olaf@20 | 272 | k |= (data[i + 2] & 0xFF) << 16; |
olaf@20 | 273 | k |= (data[i + 3] & 0xFF) << 24; |
olaf@20 | 274 | |
olaf@20 | 275 | k *= m; |
olaf@20 | 276 | k ^= k >> r; |
olaf@20 | 277 | k *= m; |
olaf@20 | 278 | |
olaf@20 | 279 | h *= m; |
olaf@20 | 280 | h ^= k; |
olaf@20 | 281 | |
olaf@20 | 282 | i += 4; |
olaf@20 | 283 | len -= 4; |
olaf@20 | 284 | } |
olaf@20 | 285 | |
olaf@20 | 286 | switch (len) { |
olaf@20 | 287 | case 3: h ^= (data[i + 2] & 0xFF) << 16; |
universe@38 | 288 | /* no break */ |
olaf@20 | 289 | case 2: h ^= (data[i + 1] & 0xFF) << 8; |
universe@38 | 290 | /* no break */ |
olaf@20 | 291 | case 1: h ^= (data[i + 0] & 0xFF); h *= m; |
universe@38 | 292 | /* no break */ |
olaf@20 | 293 | } |
olaf@20 | 294 | |
olaf@20 | 295 | h ^= h >> 13; |
olaf@20 | 296 | h *= m; |
olaf@20 | 297 | h ^= h >> 15; |
olaf@20 | 298 | |
olaf@20 | 299 | return h; |
olaf@20 | 300 | } |
olaf@31 | 301 | |
universe@374 | 302 | UcxMapIterator ucx_map_iterator(UcxMap const *map) { |
olaf@31 | 303 | UcxMapIterator i; |
olaf@31 | 304 | i.map = map; |
olaf@31 | 305 | i.cur = NULL; |
olaf@31 | 306 | i.index = 0; |
olaf@31 | 307 | return i; |
olaf@31 | 308 | } |
olaf@31 | 309 | |
olaf@111 | 310 | int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm) { |
olaf@31 | 311 | UcxMapElement *e = i->cur; |
olaf@31 | 312 | |
universe@138 | 313 | if (e) { |
universe@138 | 314 | e = e->next; |
universe@138 | 315 | } else { |
olaf@31 | 316 | e = i->map->map[0]; |
olaf@31 | 317 | } |
olaf@31 | 318 | |
universe@138 | 319 | while (i->index < i->map->size) { |
universe@138 | 320 | if (e) { |
universe@138 | 321 | if (e->data) { |
olaf@31 | 322 | i->cur = e; |
olaf@31 | 323 | *elm = e->data; |
universe@327 | 324 | key->data = e->key.data; |
universe@327 | 325 | key->hash = e->key.hash; |
universe@327 | 326 | key->len = e->key.len; |
universe@138 | 327 | return 1; |
olaf@31 | 328 | } |
olaf@31 | 329 | |
olaf@31 | 330 | e = e->next; |
olaf@31 | 331 | } else { |
olaf@31 | 332 | i->index++; |
olaf@31 | 333 | |
universe@138 | 334 | if (i->index < i->map->size) { |
olaf@31 | 335 | e = i->map->map[i->index]; |
olaf@31 | 336 | } |
olaf@31 | 337 | } |
olaf@31 | 338 | } |
olaf@31 | 339 | |
universe@138 | 340 | return 0; |
olaf@31 | 341 | } |
universe@42 | 342 | |
universe@374 | 343 | UcxMap* ucx_map_union(const UcxMap *first, const UcxMap *second, |
universe@374 | 344 | copy_func cpfnc, void* cpdata) { |
universe@374 | 345 | return ucx_map_union_a(ucx_default_allocator(), |
universe@374 | 346 | first, second, cpfnc, cpdata); |
universe@374 | 347 | } |
universe@374 | 348 | |
universe@374 | 349 | UcxMap* ucx_map_union_a(UcxAllocator *allocator, |
universe@374 | 350 | const UcxMap *first, const UcxMap *second, |
universe@374 | 351 | copy_func cpfnc, void* cpdata) { |
universe@374 | 352 | UcxMap* result = ucx_map_clone_a(allocator, first, cpfnc, cpdata); |
universe@374 | 353 | ucx_map_copy(second, result, cpfnc, cpdata); |
universe@374 | 354 | return result; |
universe@374 | 355 | } |
universe@374 | 356 | |
universe@374 | 357 | UcxMap* ucx_map_intersection(const UcxMap *first, const UcxMap *second, |
universe@374 | 358 | copy_func cpfnc, void* cpdata) { |
universe@374 | 359 | return ucx_map_intersection_a(ucx_default_allocator(), |
universe@374 | 360 | first, second, cpfnc, cpdata); |
universe@374 | 361 | } |
universe@374 | 362 | |
universe@374 | 363 | UcxMap* ucx_map_intersection_a(UcxAllocator *allocator, |
universe@374 | 364 | const UcxMap *first, const UcxMap *second, |
universe@374 | 365 | copy_func cpfnc, void* cpdata) { |
universe@374 | 366 | UcxMap *result = ucx_map_new_a(allocator, first->size < second->size ? |
universe@374 | 367 | first->size : second->size); |
universe@374 | 368 | |
universe@374 | 369 | UcxMapIterator iter = ucx_map_iterator(first); |
universe@374 | 370 | void* value; |
universe@374 | 371 | UCX_MAP_FOREACH(key, value, iter) { |
universe@374 | 372 | if (ucx_map_get(second, key)) { |
universe@374 | 373 | ucx_map_put(result, key, cpfnc ? cpfnc(value, cpdata) : value); |
universe@374 | 374 | } |
universe@374 | 375 | } |
universe@374 | 376 | |
universe@374 | 377 | return result; |
universe@374 | 378 | } |
universe@374 | 379 | |
universe@374 | 380 | UcxMap* ucx_map_difference(const UcxMap *first, const UcxMap *second, |
universe@374 | 381 | copy_func cpfnc, void* cpdata) { |
universe@374 | 382 | return ucx_map_difference_a(ucx_default_allocator(), |
universe@374 | 383 | first, second, cpfnc, cpdata); |
universe@374 | 384 | } |
universe@374 | 385 | |
universe@374 | 386 | UcxMap* ucx_map_difference_a(UcxAllocator *allocator, |
universe@374 | 387 | const UcxMap *first, const UcxMap *second, |
universe@374 | 388 | copy_func cpfnc, void* cpdata) { |
universe@374 | 389 | |
universe@374 | 390 | UcxMap *result = ucx_map_new_a(allocator, first->size - second->count); |
universe@374 | 391 | |
universe@374 | 392 | UcxMapIterator iter = ucx_map_iterator(first); |
universe@374 | 393 | void* value; |
universe@374 | 394 | UCX_MAP_FOREACH(key, value, iter) { |
universe@374 | 395 | if (!ucx_map_get(second, key)) { |
universe@374 | 396 | ucx_map_put(result, key, cpfnc ? cpfnc(value, cpdata) : value); |
universe@374 | 397 | } |
universe@374 | 398 | } |
universe@374 | 399 | |
universe@374 | 400 | ucx_map_rehash(result); |
universe@374 | 401 | return result; |
universe@374 | 402 | } |