Thu, 15 Oct 2015 12:34:10 +0200
added ucx_map_clean()
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2015 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 <stdlib.h>
30 #include <string.h>
32 #include "map.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_clear(UcxMap *map) {
86 ucx_map_free_elmlist_contents(map);
87 memset(map->map, 0, map->size*sizeof(UcxMapElement*));
88 map->count = 0;
89 }
91 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
92 copy_func fnc, void *data) {
93 UcxMapIterator i = ucx_map_iterator(from);
94 void *value;
95 UCX_MAP_FOREACH(key, value, i) {
96 if (ucx_map_put(to, key, fnc ? fnc(value, data) : value)) {
97 return 1;
98 }
99 }
100 return 0;
101 }
103 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) {
104 size_t bs = (map->count * 5) >> 1;
105 UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size);
106 if (!newmap) {
107 return NULL;
108 }
109 ucx_map_copy(map, newmap, fnc, data);
110 return newmap;
111 }
113 int ucx_map_rehash(UcxMap *map) {
114 size_t load = (map->size * 3) >> 2;
115 if (map->count > load) {
116 UcxMap oldmap;
117 oldmap.map = map->map;
118 oldmap.size = map->size;
119 oldmap.count = map->count;
120 oldmap.allocator = map->allocator;
122 map->size = (map->count * 5) >> 1;
123 map->map = (UcxMapElement**)alcalloc(
124 map->allocator, map->size, sizeof(UcxMapElement*));
125 if (!map->map) {
126 *map = oldmap;
127 return 1;
128 }
129 map->count = 0;
130 ucx_map_copy(&oldmap, map, NULL, NULL);
132 /* free the UcxMapElement list of oldmap */
133 ucx_map_free_elmlist_contents(&oldmap);
134 alfree(map->allocator, oldmap.map);
135 }
136 return 0;
137 }
139 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
140 UcxAllocator *allocator = map->allocator;
142 if (key.hash == 0) {
143 key.hash = ucx_hash((char*)key.data, key.len);
144 }
146 size_t slot = key.hash%map->size;
147 UcxMapElement *restrict elm = map->map[slot];
148 UcxMapElement *restrict prev = NULL;
150 while (elm && elm->key.hash < key.hash) {
151 prev = elm;
152 elm = elm->next;
153 }
155 if (!elm || elm->key.hash != key.hash) {
156 UcxMapElement *e = (UcxMapElement*)almalloc(
157 allocator, sizeof(UcxMapElement));
158 if (!e) {
159 return -1;
160 }
161 e->key.data = NULL;
162 if (prev) {
163 prev->next = e;
164 } else {
165 map->map[slot] = e;
166 }
167 e->next = elm;
168 elm = e;
169 }
171 if (!elm->key.data) {
172 void *kd = almalloc(allocator, key.len);
173 if (!kd) {
174 return -1;
175 }
176 memcpy(kd, key.data, key.len);
177 key.data = kd;
178 elm->key = key;
179 map->count++;
180 }
181 elm->data = data;
183 return 0;
184 }
186 void* ucx_map_get_and_remove(UcxMap *map, UcxKey key, _Bool remove) {
187 if(key.hash == 0) {
188 key.hash = ucx_hash((char*)key.data, key.len);
189 }
191 size_t slot = key.hash%map->size;
192 UcxMapElement *restrict elm = map->map[slot];
193 UcxMapElement *restrict pelm = NULL;
194 while (elm && elm->key.hash <= key.hash) {
195 if(elm->key.hash == key.hash) {
196 int n = (key.len > elm->key.len) ? elm->key.len : key.len;
197 if (memcmp(elm->key.data, key.data, n) == 0) {
198 void *data = elm->data;
199 if (remove) {
200 if (pelm) {
201 pelm->next = elm->next;
202 } else {
203 map->map[slot] = elm->next;
204 }
205 alfree(map->allocator, elm->key.data);
206 alfree(map->allocator, elm);
207 map->count--;
208 }
210 return data;
211 }
212 }
213 pelm = elm;
214 elm = pelm->next;
215 }
217 return NULL;
218 }
220 void *ucx_map_get(UcxMap *map, UcxKey key) {
221 return ucx_map_get_and_remove(map, key, 0);
222 }
224 void *ucx_map_remove(UcxMap *map, UcxKey key) {
225 return ucx_map_get_and_remove(map, key, 1);
226 }
228 UcxKey ucx_key(void *data, size_t len) {
229 UcxKey key;
230 key.data = data;
231 key.len = len;
232 key.hash = ucx_hash((const char*) data, len);
233 return key;
234 }
237 int ucx_hash(const char *data, size_t len) {
238 /* murmur hash 2 */
240 int m = 0x5bd1e995;
241 int r = 24;
243 int h = 25 ^ len;
245 int i = 0;
246 while (len >= 4) {
247 int k = data[i + 0] & 0xFF;
248 k |= (data[i + 1] & 0xFF) << 8;
249 k |= (data[i + 2] & 0xFF) << 16;
250 k |= (data[i + 3] & 0xFF) << 24;
252 k *= m;
253 k ^= k >> r;
254 k *= m;
256 h *= m;
257 h ^= k;
259 i += 4;
260 len -= 4;
261 }
263 switch (len) {
264 case 3: h ^= (data[i + 2] & 0xFF) << 16;
265 /* no break */
266 case 2: h ^= (data[i + 1] & 0xFF) << 8;
267 /* no break */
268 case 1: h ^= (data[i + 0] & 0xFF); h *= m;
269 /* no break */
270 }
272 h ^= h >> 13;
273 h *= m;
274 h ^= h >> 15;
276 return h;
277 }
279 UcxMapIterator ucx_map_iterator(UcxMap *map) {
280 UcxMapIterator i;
281 i.map = map;
282 i.cur = NULL;
283 i.index = 0;
284 return i;
285 }
287 int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm) {
288 UcxMapElement *e = i->cur;
290 if (e) {
291 e = e->next;
292 } else {
293 e = i->map->map[0];
294 }
296 while (i->index < i->map->size) {
297 if (e) {
298 if (e->data) {
299 i->cur = e;
300 *elm = e->data;
301 *key = e->key;
302 return 1;
303 }
305 e = e->next;
306 } else {
307 i->index++;
309 if (i->index < i->map->size) {
310 e = i->map->map[i->index];
311 }
312 }
313 }
315 return 0;
316 }