ucx/map.c

Tue, 10 Jun 2014 15:43:13 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 10 Jun 2014 15:43:13 +0200
changeset 173
31a8682fffb7
parent 147
1aa598f36872
child 177
11ad03783baf
permissions
-rw-r--r--

fixed some sstring issues + added allocator macros

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

mercurial