Thu, 15 Oct 2015 12:39:50 +0200
optimized ucx_map_clear (noop for count == 0)
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 if (map->count == 0) {
87 return; // nothing to do
88 }
89 ucx_map_free_elmlist_contents(map);
90 memset(map->map, 0, map->size*sizeof(UcxMapElement*));
91 map->count = 0;
92 }
94 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
95 copy_func fnc, void *data) {
96 UcxMapIterator i = ucx_map_iterator(from);
97 void *value;
98 UCX_MAP_FOREACH(key, value, i) {
99 if (ucx_map_put(to, key, fnc ? fnc(value, data) : value)) {
100 return 1;
101 }
102 }
103 return 0;
104 }
106 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) {
107 size_t bs = (map->count * 5) >> 1;
108 UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size);
109 if (!newmap) {
110 return NULL;
111 }
112 ucx_map_copy(map, newmap, fnc, data);
113 return newmap;
114 }
116 int ucx_map_rehash(UcxMap *map) {
117 size_t load = (map->size * 3) >> 2;
118 if (map->count > load) {
119 UcxMap oldmap;
120 oldmap.map = map->map;
121 oldmap.size = map->size;
122 oldmap.count = map->count;
123 oldmap.allocator = map->allocator;
125 map->size = (map->count * 5) >> 1;
126 map->map = (UcxMapElement**)alcalloc(
127 map->allocator, map->size, sizeof(UcxMapElement*));
128 if (!map->map) {
129 *map = oldmap;
130 return 1;
131 }
132 map->count = 0;
133 ucx_map_copy(&oldmap, map, NULL, NULL);
135 /* free the UcxMapElement list of oldmap */
136 ucx_map_free_elmlist_contents(&oldmap);
137 alfree(map->allocator, oldmap.map);
138 }
139 return 0;
140 }
142 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
143 UcxAllocator *allocator = map->allocator;
145 if (key.hash == 0) {
146 key.hash = ucx_hash((char*)key.data, key.len);
147 }
149 size_t slot = key.hash%map->size;
150 UcxMapElement *restrict elm = map->map[slot];
151 UcxMapElement *restrict prev = NULL;
153 while (elm && elm->key.hash < key.hash) {
154 prev = elm;
155 elm = elm->next;
156 }
158 if (!elm || elm->key.hash != key.hash) {
159 UcxMapElement *e = (UcxMapElement*)almalloc(
160 allocator, sizeof(UcxMapElement));
161 if (!e) {
162 return -1;
163 }
164 e->key.data = NULL;
165 if (prev) {
166 prev->next = e;
167 } else {
168 map->map[slot] = e;
169 }
170 e->next = elm;
171 elm = e;
172 }
174 if (!elm->key.data) {
175 void *kd = almalloc(allocator, key.len);
176 if (!kd) {
177 return -1;
178 }
179 memcpy(kd, key.data, key.len);
180 key.data = kd;
181 elm->key = key;
182 map->count++;
183 }
184 elm->data = data;
186 return 0;
187 }
189 void* ucx_map_get_and_remove(UcxMap *map, UcxKey key, _Bool remove) {
190 if(key.hash == 0) {
191 key.hash = ucx_hash((char*)key.data, key.len);
192 }
194 size_t slot = key.hash%map->size;
195 UcxMapElement *restrict elm = map->map[slot];
196 UcxMapElement *restrict pelm = NULL;
197 while (elm && elm->key.hash <= key.hash) {
198 if(elm->key.hash == key.hash) {
199 int n = (key.len > elm->key.len) ? elm->key.len : key.len;
200 if (memcmp(elm->key.data, key.data, n) == 0) {
201 void *data = elm->data;
202 if (remove) {
203 if (pelm) {
204 pelm->next = elm->next;
205 } else {
206 map->map[slot] = elm->next;
207 }
208 alfree(map->allocator, elm->key.data);
209 alfree(map->allocator, elm);
210 map->count--;
211 }
213 return data;
214 }
215 }
216 pelm = elm;
217 elm = pelm->next;
218 }
220 return NULL;
221 }
223 void *ucx_map_get(UcxMap *map, UcxKey key) {
224 return ucx_map_get_and_remove(map, key, 0);
225 }
227 void *ucx_map_remove(UcxMap *map, UcxKey key) {
228 return ucx_map_get_and_remove(map, key, 1);
229 }
231 UcxKey ucx_key(void *data, size_t len) {
232 UcxKey key;
233 key.data = data;
234 key.len = len;
235 key.hash = ucx_hash((const char*) data, len);
236 return key;
237 }
240 int ucx_hash(const char *data, size_t len) {
241 /* murmur hash 2 */
243 int m = 0x5bd1e995;
244 int r = 24;
246 int h = 25 ^ len;
248 int i = 0;
249 while (len >= 4) {
250 int k = data[i + 0] & 0xFF;
251 k |= (data[i + 1] & 0xFF) << 8;
252 k |= (data[i + 2] & 0xFF) << 16;
253 k |= (data[i + 3] & 0xFF) << 24;
255 k *= m;
256 k ^= k >> r;
257 k *= m;
259 h *= m;
260 h ^= k;
262 i += 4;
263 len -= 4;
264 }
266 switch (len) {
267 case 3: h ^= (data[i + 2] & 0xFF) << 16;
268 /* no break */
269 case 2: h ^= (data[i + 1] & 0xFF) << 8;
270 /* no break */
271 case 1: h ^= (data[i + 0] & 0xFF); h *= m;
272 /* no break */
273 }
275 h ^= h >> 13;
276 h *= m;
277 h ^= h >> 15;
279 return h;
280 }
282 UcxMapIterator ucx_map_iterator(UcxMap *map) {
283 UcxMapIterator i;
284 i.map = map;
285 i.cur = NULL;
286 i.index = 0;
287 return i;
288 }
290 int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm) {
291 UcxMapElement *e = i->cur;
293 if (e) {
294 e = e->next;
295 } else {
296 e = i->map->map[0];
297 }
299 while (i->index < i->map->size) {
300 if (e) {
301 if (e->data) {
302 i->cur = e;
303 *elm = e->data;
304 *key = e->key;
305 return 1;
306 }
308 e = e->next;
309 } else {
310 i->index++;
312 if (i->index < i->map->size) {
313 e = i->map->map[i->index];
314 }
315 }
316 }
318 return 0;
319 }