Fri, 11 May 2018 18:13:54 +0200
adds ucx_sstrcmp() compare function to utils.h
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 if (destr) {
90 destr(val);
91 } else {
92 alfree(map->allocator, val);
93 }
94 }
95 }
97 void ucx_map_clear(UcxMap *map) {
98 if (map->count == 0) {
99 return; // nothing to do
100 }
101 ucx_map_free_elmlist_contents(map);
102 memset(map->map, 0, map->size*sizeof(UcxMapElement*));
103 map->count = 0;
104 }
106 int ucx_map_copy(UcxMap *from, UcxMap *to, copy_func fnc, void *data) {
107 UcxMapIterator i = ucx_map_iterator(from);
108 void *value;
109 UCX_MAP_FOREACH(key, value, i) {
110 if (ucx_map_put(to, key, fnc ? fnc(value, data) : value)) {
111 return 1;
112 }
113 }
114 return 0;
115 }
117 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) {
118 size_t bs = (map->count * 5) >> 1;
119 UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size);
120 if (!newmap) {
121 return NULL;
122 }
123 ucx_map_copy(map, newmap, fnc, data);
124 return newmap;
125 }
127 int ucx_map_rehash(UcxMap *map) {
128 size_t load = (map->size * 3) >> 2;
129 if (map->count > load) {
130 UcxMap oldmap;
131 oldmap.map = map->map;
132 oldmap.size = map->size;
133 oldmap.count = map->count;
134 oldmap.allocator = map->allocator;
136 map->size = (map->count * 5) >> 1;
137 map->map = (UcxMapElement**)alcalloc(
138 map->allocator, map->size, sizeof(UcxMapElement*));
139 if (!map->map) {
140 *map = oldmap;
141 return 1;
142 }
143 map->count = 0;
144 ucx_map_copy(&oldmap, map, NULL, NULL);
146 /* free the UcxMapElement list of oldmap */
147 ucx_map_free_elmlist_contents(&oldmap);
148 alfree(map->allocator, oldmap.map);
149 }
150 return 0;
151 }
153 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
154 UcxAllocator *allocator = map->allocator;
156 if (key.hash == 0) {
157 key.hash = ucx_hash((char*)key.data, key.len);
158 }
160 size_t slot = key.hash%map->size;
161 UcxMapElement *elm = map->map[slot];
162 UcxMapElement *prev = NULL;
164 while (elm && elm->key.hash < key.hash) {
165 prev = elm;
166 elm = elm->next;
167 }
169 if (!elm || elm->key.hash != key.hash) {
170 UcxMapElement *e = (UcxMapElement*)almalloc(
171 allocator, sizeof(UcxMapElement));
172 if (!e) {
173 return -1;
174 }
175 e->key.data = NULL;
176 if (prev) {
177 prev->next = e;
178 } else {
179 map->map[slot] = e;
180 }
181 e->next = elm;
182 elm = e;
183 }
185 if (!elm->key.data) {
186 void *kd = almalloc(allocator, key.len);
187 if (!kd) {
188 return -1;
189 }
190 memcpy(kd, key.data, key.len);
191 key.data = kd;
192 elm->key = key;
193 map->count++;
194 }
195 elm->data = data;
197 return 0;
198 }
200 static void* ucx_map_get_and_remove(UcxMap *map, UcxKey key, int remove) {
201 if(key.hash == 0) {
202 key.hash = ucx_hash((char*)key.data, key.len);
203 }
205 size_t slot = key.hash%map->size;
206 UcxMapElement *elm = map->map[slot];
207 UcxMapElement *pelm = NULL;
208 while (elm && elm->key.hash <= key.hash) {
209 if(elm->key.hash == key.hash) {
210 int n = (key.len > elm->key.len) ? elm->key.len : key.len;
211 if (memcmp(elm->key.data, key.data, n) == 0) {
212 void *data = elm->data;
213 if (remove) {
214 if (pelm) {
215 pelm->next = elm->next;
216 } else {
217 map->map[slot] = elm->next;
218 }
219 alfree(map->allocator, elm->key.data);
220 alfree(map->allocator, elm);
221 map->count--;
222 }
224 return data;
225 }
226 }
227 pelm = elm;
228 elm = pelm->next;
229 }
231 return NULL;
232 }
234 void *ucx_map_get(UcxMap *map, UcxKey key) {
235 return ucx_map_get_and_remove(map, key, 0);
236 }
238 void *ucx_map_remove(UcxMap *map, UcxKey key) {
239 return ucx_map_get_and_remove(map, key, 1);
240 }
242 UcxKey ucx_key(void *data, size_t len) {
243 UcxKey key;
244 key.data = data;
245 key.len = len;
246 key.hash = ucx_hash((const char*) data, len);
247 return key;
248 }
251 int ucx_hash(const char *data, size_t len) {
252 /* murmur hash 2 */
254 int m = 0x5bd1e995;
255 int r = 24;
257 int h = 25 ^ len;
259 int i = 0;
260 while (len >= 4) {
261 int k = data[i + 0] & 0xFF;
262 k |= (data[i + 1] & 0xFF) << 8;
263 k |= (data[i + 2] & 0xFF) << 16;
264 k |= (data[i + 3] & 0xFF) << 24;
266 k *= m;
267 k ^= k >> r;
268 k *= m;
270 h *= m;
271 h ^= k;
273 i += 4;
274 len -= 4;
275 }
277 switch (len) {
278 case 3: h ^= (data[i + 2] & 0xFF) << 16;
279 /* no break */
280 case 2: h ^= (data[i + 1] & 0xFF) << 8;
281 /* no break */
282 case 1: h ^= (data[i + 0] & 0xFF); h *= m;
283 /* no break */
284 }
286 h ^= h >> 13;
287 h *= m;
288 h ^= h >> 15;
290 return h;
291 }
293 UcxMapIterator ucx_map_iterator(UcxMap *map) {
294 UcxMapIterator i;
295 i.map = map;
296 i.cur = NULL;
297 i.index = 0;
298 return i;
299 }
301 int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm) {
302 UcxMapElement *e = i->cur;
304 if (e) {
305 e = e->next;
306 } else {
307 e = i->map->map[0];
308 }
310 while (i->index < i->map->size) {
311 if (e) {
312 if (e->data) {
313 i->cur = e;
314 *elm = e->data;
315 *key = e->key;
316 return 1;
317 }
319 e = e->next;
320 } else {
321 i->index++;
323 if (i->index < i->map->size) {
324 e = i->map->map[i->index];
325 }
326 }
327 }
329 return 0;
330 }