Mon, 21 Jul 2014 13:18:32 +0200
added some string tests
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2014 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(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 alfree(map->allocator, map->map);
78 }
80 void ucx_map_free(UcxMap *map) {
81 ucx_map_free_elmlist(map);
82 alfree(map->allocator, map);
83 }
85 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
86 copy_func fnc, void *data) {
87 UcxMapIterator i = ucx_map_iterator(from);
88 void *value;
89 UCX_MAP_FOREACH(key, value, i) {
90 if (ucx_map_put(to, key, fnc ? fnc(value, data) : value)) {
91 return 1;
92 }
93 }
94 return 0;
95 }
97 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) {
98 size_t bs = (map->count * 5) >> 1;
99 UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size);
100 if (!newmap) {
101 return NULL;
102 }
103 ucx_map_copy(map, newmap, fnc, data);
104 return newmap;
105 }
107 int ucx_map_rehash(UcxMap *map) {
108 size_t load = (map->size * 3) >> 2;
109 if (map->count > load) {
110 UcxMap oldmap;
111 oldmap.map = map->map;
112 oldmap.size = map->size;
113 oldmap.count = map->count;
114 oldmap.allocator = map->allocator;
116 map->size = (map->count * 5) >> 1;
117 map->map = (UcxMapElement**)alcalloc(
118 map->allocator, map->size, sizeof(UcxMapElement*));
119 if (!map->map) {
120 *map = oldmap;
121 return 1;
122 }
123 map->count = 0;
124 ucx_map_copy(&oldmap, map, NULL, NULL);
126 /* free the UcxMapElement list of oldmap */
127 ucx_map_free_elmlist(&oldmap);
128 }
129 return 0;
130 }
132 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
133 UcxAllocator *allocator = map->allocator;
135 if (key.hash == 0) {
136 key.hash = ucx_hash((char*)key.data, key.len);
137 }
139 size_t slot = key.hash%map->size;
140 UcxMapElement *restrict elm = map->map[slot];
141 UcxMapElement *restrict prev = NULL;
143 while (elm && elm->key.hash < key.hash) {
144 prev = elm;
145 elm = elm->next;
146 }
148 if (!elm || elm->key.hash != key.hash) {
149 UcxMapElement *e = (UcxMapElement*)almalloc(
150 allocator, sizeof(UcxMapElement));
151 if (!e) {
152 return -1;
153 }
154 e->key.data = NULL;
155 if (prev) {
156 prev->next = e;
157 } else {
158 map->map[slot] = e;
159 }
160 e->next = elm;
161 elm = e;
162 }
164 if (!elm->key.data) {
165 void *kd = almalloc(allocator, key.len);
166 if (!kd) {
167 return -1;
168 }
169 memcpy(kd, key.data, key.len);
170 key.data = kd;
171 elm->key = key;
172 map->count++;
173 }
174 elm->data = data;
176 return 0;
177 }
179 void* ucx_map_get_and_remove(UcxMap *map, UcxKey key, _Bool remove) {
180 if(key.hash == 0) {
181 key.hash = ucx_hash((char*)key.data, key.len);
182 }
184 size_t slot = key.hash%map->size;
185 UcxMapElement *restrict elm = map->map[slot];
186 UcxMapElement *restrict pelm = NULL;
187 while (elm && elm->key.hash <= key.hash) {
188 if(elm->key.hash == key.hash) {
189 int n = (key.len > elm->key.len) ? elm->key.len : key.len;
190 if (memcmp(elm->key.data, key.data, n) == 0) {
191 void *data = elm->data;
192 if (remove) {
193 if (pelm) {
194 pelm->next = elm->next;
195 } else {
196 map->map[slot] = elm->next;
197 }
198 alfree(map->allocator, elm->key.data);
199 alfree(map->allocator, elm);
200 map->count--;
201 }
203 return data;
204 }
205 }
206 pelm = elm;
207 elm = pelm->next;
208 }
210 return NULL;
211 }
213 void *ucx_map_get(UcxMap *map, UcxKey key) {
214 return ucx_map_get_and_remove(map, key, 0);
215 }
217 void *ucx_map_remove(UcxMap *map, UcxKey key) {
218 return ucx_map_get_and_remove(map, key, 1);
219 }
221 UcxKey ucx_key(void *data, size_t len) {
222 UcxKey key;
223 key.data = data;
224 key.len = len;
225 key.hash = ucx_hash((const char*) data, len);
226 return key;
227 }
230 int ucx_hash(const char *data, size_t len) {
231 /* murmur hash 2 */
233 int m = 0x5bd1e995;
234 int r = 24;
236 int h = 25 ^ len;
238 int i = 0;
239 while (len >= 4) {
240 int k = data[i + 0] & 0xFF;
241 k |= (data[i + 1] & 0xFF) << 8;
242 k |= (data[i + 2] & 0xFF) << 16;
243 k |= (data[i + 3] & 0xFF) << 24;
245 k *= m;
246 k ^= k >> r;
247 k *= m;
249 h *= m;
250 h ^= k;
252 i += 4;
253 len -= 4;
254 }
256 switch (len) {
257 case 3: h ^= (data[i + 2] & 0xFF) << 16;
258 /* no break */
259 case 2: h ^= (data[i + 1] & 0xFF) << 8;
260 /* no break */
261 case 1: h ^= (data[i + 0] & 0xFF); h *= m;
262 /* no break */
263 }
265 h ^= h >> 13;
266 h *= m;
267 h ^= h >> 15;
269 return h;
270 }
272 UcxMapIterator ucx_map_iterator(UcxMap *map) {
273 UcxMapIterator i;
274 i.map = map;
275 i.cur = NULL;
276 i.index = 0;
277 return i;
278 }
280 int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm) {
281 UcxMapElement *e = i->cur;
283 if (e) {
284 e = e->next;
285 } else {
286 e = i->map->map[0];
287 }
289 while (i->index < i->map->size) {
290 if (e) {
291 if (e->data) {
292 i->cur = e;
293 *elm = e->data;
294 *key = e->key;
295 return 1;
296 }
298 e = e->next;
299 } else {
300 i->index++;
302 if (i->index < i->map->size) {
303 e = i->map->map[i->index];
304 }
305 }
306 }
308 return 0;
309 }