Mon, 14 May 2018 17:58:06 +0200
closes constsstr branch
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 #include "ucx/map.h"
31 #include <stdlib.h>
32 #include <string.h>
34 UcxMap *ucx_map_new(size_t size) {
35 return ucx_map_new_a(NULL, size);
36 }
38 UcxMap *ucx_map_new_a(UcxAllocator *allocator, size_t size) {
39 if(size == 0) {
40 size = 16;
41 }
43 if(!allocator) {
44 allocator = ucx_default_allocator();
45 }
47 UcxMap *map = (UcxMap*)almalloc(allocator, sizeof(UcxMap));
48 if (!map) {
49 return NULL;
50 }
52 map->allocator = allocator;
53 map->map = (UcxMapElement**)alcalloc(
54 allocator, size, sizeof(UcxMapElement*));
55 if(map->map == NULL) {
56 alfree(allocator, map);
57 return NULL;
58 }
59 map->size = size;
60 map->count = 0;
62 return map;
63 }
65 static void ucx_map_free_elmlist_contents(UcxMap *map) {
66 for (size_t n = 0 ; n < map->size ; n++) {
67 UcxMapElement *elem = map->map[n];
68 if (elem != NULL) {
69 do {
70 UcxMapElement *next = elem->next;
71 alfree(map->allocator, elem->key.data);
72 alfree(map->allocator, elem);
73 elem = next;
74 } while (elem != NULL);
75 }
76 }
77 }
79 void ucx_map_free(UcxMap *map) {
80 ucx_map_free_elmlist_contents(map);
81 alfree(map->allocator, map->map);
82 alfree(map->allocator, map);
83 }
85 void ucx_map_free_content(UcxMap *map, ucx_destructor destr) {
86 UcxMapIterator iter = ucx_map_iterator(map);
87 void *val;
88 UCX_MAP_FOREACH(key, val, iter) {
89 destr(val);
90 }
91 }
93 void ucx_map_clear(UcxMap *map) {
94 if (map->count == 0) {
95 return; // nothing to do
96 }
97 ucx_map_free_elmlist_contents(map);
98 memset(map->map, 0, map->size*sizeof(UcxMapElement*));
99 map->count = 0;
100 }
102 int ucx_map_copy(UcxMap *from, UcxMap *to, copy_func fnc, void *data) {
103 UcxMapIterator i = ucx_map_iterator(from);
104 void *value;
105 UCX_MAP_FOREACH(key, value, i) {
106 if (ucx_map_put(to, key, fnc ? fnc(value, data) : value)) {
107 return 1;
108 }
109 }
110 return 0;
111 }
113 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) {
114 size_t bs = (map->count * 5) >> 1;
115 UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size);
116 if (!newmap) {
117 return NULL;
118 }
119 ucx_map_copy(map, newmap, fnc, data);
120 return newmap;
121 }
123 int ucx_map_rehash(UcxMap *map) {
124 size_t load = (map->size * 3) >> 2;
125 if (map->count > load) {
126 UcxMap oldmap;
127 oldmap.map = map->map;
128 oldmap.size = map->size;
129 oldmap.count = map->count;
130 oldmap.allocator = map->allocator;
132 map->size = (map->count * 5) >> 1;
133 map->map = (UcxMapElement**)alcalloc(
134 map->allocator, map->size, sizeof(UcxMapElement*));
135 if (!map->map) {
136 *map = oldmap;
137 return 1;
138 }
139 map->count = 0;
140 ucx_map_copy(&oldmap, map, NULL, NULL);
142 /* free the UcxMapElement list of oldmap */
143 ucx_map_free_elmlist_contents(&oldmap);
144 alfree(map->allocator, oldmap.map);
145 }
146 return 0;
147 }
149 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
150 UcxAllocator *allocator = map->allocator;
152 if (key.hash == 0) {
153 key.hash = ucx_hash((char*)key.data, key.len);
154 }
156 size_t slot = key.hash%map->size;
157 UcxMapElement *elm = map->map[slot];
158 UcxMapElement *prev = NULL;
160 while (elm && elm->key.hash < key.hash) {
161 prev = elm;
162 elm = elm->next;
163 }
165 if (!elm || elm->key.hash != key.hash) {
166 UcxMapElement *e = (UcxMapElement*)almalloc(
167 allocator, sizeof(UcxMapElement));
168 if (!e) {
169 return -1;
170 }
171 e->key.data = NULL;
172 if (prev) {
173 prev->next = e;
174 } else {
175 map->map[slot] = e;
176 }
177 e->next = elm;
178 elm = e;
179 }
181 if (!elm->key.data) {
182 void *kd = almalloc(allocator, key.len);
183 if (!kd) {
184 return -1;
185 }
186 memcpy(kd, key.data, key.len);
187 key.data = kd;
188 elm->key = key;
189 map->count++;
190 }
191 elm->data = data;
193 return 0;
194 }
196 static void* ucx_map_get_and_remove(UcxMap *map, UcxKey key, int remove) {
197 if(key.hash == 0) {
198 key.hash = ucx_hash((char*)key.data, key.len);
199 }
201 size_t slot = key.hash%map->size;
202 UcxMapElement *elm = map->map[slot];
203 UcxMapElement *pelm = NULL;
204 while (elm && elm->key.hash <= key.hash) {
205 if(elm->key.hash == key.hash) {
206 int n = (key.len > elm->key.len) ? elm->key.len : key.len;
207 if (memcmp(elm->key.data, key.data, n) == 0) {
208 void *data = elm->data;
209 if (remove) {
210 if (pelm) {
211 pelm->next = elm->next;
212 } else {
213 map->map[slot] = elm->next;
214 }
215 alfree(map->allocator, elm->key.data);
216 alfree(map->allocator, elm);
217 map->count--;
218 }
220 return data;
221 }
222 }
223 pelm = elm;
224 elm = pelm->next;
225 }
227 return NULL;
228 }
230 void *ucx_map_get(UcxMap *map, UcxKey key) {
231 return ucx_map_get_and_remove(map, key, 0);
232 }
234 void *ucx_map_remove(UcxMap *map, UcxKey key) {
235 return ucx_map_get_and_remove(map, key, 1);
236 }
238 UcxKey ucx_key(void *data, size_t len) {
239 UcxKey key;
240 key.data = data;
241 key.len = len;
242 key.hash = ucx_hash((const char*) data, len);
243 return key;
244 }
247 int ucx_hash(const char *data, size_t len) {
248 /* murmur hash 2 */
250 int m = 0x5bd1e995;
251 int r = 24;
253 int h = 25 ^ len;
255 int i = 0;
256 while (len >= 4) {
257 int k = data[i + 0] & 0xFF;
258 k |= (data[i + 1] & 0xFF) << 8;
259 k |= (data[i + 2] & 0xFF) << 16;
260 k |= (data[i + 3] & 0xFF) << 24;
262 k *= m;
263 k ^= k >> r;
264 k *= m;
266 h *= m;
267 h ^= k;
269 i += 4;
270 len -= 4;
271 }
273 switch (len) {
274 case 3: h ^= (data[i + 2] & 0xFF) << 16;
275 /* no break */
276 case 2: h ^= (data[i + 1] & 0xFF) << 8;
277 /* no break */
278 case 1: h ^= (data[i + 0] & 0xFF); h *= m;
279 /* no break */
280 }
282 h ^= h >> 13;
283 h *= m;
284 h ^= h >> 15;
286 return h;
287 }
289 UcxMapIterator ucx_map_iterator(UcxMap *map) {
290 UcxMapIterator i;
291 i.map = map;
292 i.cur = NULL;
293 i.index = 0;
294 return i;
295 }
297 int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm) {
298 UcxMapElement *e = i->cur;
300 if (e) {
301 e = e->next;
302 } else {
303 e = i->map->map[0];
304 }
306 while (i->index < i->map->size) {
307 if (e) {
308 if (e->data) {
309 i->cur = e;
310 *elm = e->data;
311 *key = e->key;
312 return 1;
313 }
315 e = e->next;
316 } else {
317 i->index++;
319 if (i->index < i->map->size) {
320 e = i->map->map[i->index];
321 }
322 }
323 }
325 return 0;
326 }