2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
29 #include "cx/hash_map.h"
35 struct cx_hash_map_element_s {
36 /** A pointer to the next element in the current bucket. */
37 struct cx_hash_map_element_s *next;
39 /** The corresponding key. */
42 /** The value data. */
46 static void cx_hash_map_clear(struct cx_map_s *map) {
47 struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
48 cx_for_n(i, hash_map->bucket_count) {
49 struct cx_hash_map_element_s *elem = hash_map->buckets[i];
52 struct cx_hash_map_element_s *next = elem->next;
53 // invoke the destructor
54 cx_invoke_destructor(map, elem->data);
56 cxFree(map->allocator, (void *) elem->key.data);
58 cxFree(map->allocator, elem);
61 } while (elem != NULL);
63 // do not leave a dangling pointer
64 hash_map->buckets[i] = NULL;
70 static void cx_hash_map_destructor(struct cx_map_s *map) {
71 struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
74 cx_hash_map_clear(map);
75 cxFree(map->allocator, hash_map->buckets);
77 // free the map structure
78 cxFree(map->allocator, map);
81 static int cx_hash_map_put(
86 struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
87 CxAllocator const *allocator = map->allocator;
89 unsigned hash = key.hash;
95 size_t slot = hash % hash_map->bucket_count;
96 struct cx_hash_map_element_s *elm = hash_map->buckets[slot];
97 struct cx_hash_map_element_s *prev = NULL;
99 while (elm != NULL && elm->key.hash < hash) {
104 if (elm != NULL && elm->key.hash == hash && elm->key.len == key.len &&
105 memcmp(elm->key.data, key.data, key.len) == 0) {
106 // overwrite existing element
107 if (map->store_pointer) {
108 memcpy(elm->data, &value, sizeof(void *));
110 memcpy(elm->data, value, map->item_size);
113 // allocate new element
114 struct cx_hash_map_element_s *e = cxMalloc(
116 sizeof(struct cx_hash_map_element_s) + map->item_size
123 if (map->store_pointer) {
124 memcpy(e->data, &value, sizeof(void *));
126 memcpy(e->data, value, map->item_size);
130 void *kd = cxMalloc(allocator, key.len);
134 memcpy(kd, key.data, key.len);
136 e->key.len = key.len;
139 // insert the element into the linked list
141 hash_map->buckets[slot] = e;
154 static void cx_hash_map_unlink(
155 struct cx_hash_map_s *hash_map,
157 struct cx_hash_map_element_s *prev,
158 struct cx_hash_map_element_s *elm
162 hash_map->buckets[slot] = elm->next;
164 prev->next = elm->next;
167 cxFree(hash_map->base.allocator, (void *) elm->key.data);
168 cxFree(hash_map->base.allocator, elm);
170 hash_map->base.size--;
174 * Helper function to avoid code duplication.
177 * @param key the key to look up
178 * @param remove flag indicating whether the looked up entry shall be removed
179 * @param destroy flag indicating whether the destructor shall be invoked
180 * @return a pointer to the value corresponding to the key or \c NULL
182 static void *cx_hash_map_get_remove(
188 struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
190 unsigned hash = key.hash;
192 cx_hash_murmur(&key);
196 size_t slot = hash % hash_map->bucket_count;
197 struct cx_hash_map_element_s *elm = hash_map->buckets[slot];
198 struct cx_hash_map_element_s *prev = NULL;
199 while (elm && elm->key.hash <= hash) {
200 if (elm->key.hash == hash && elm->key.len == key.len) {
201 if (memcmp(elm->key.data, key.data, key.len) == 0) {
204 cx_invoke_destructor(map, elm->data);
206 if (map->store_pointer) {
207 data = *(void **) elm->data;
213 cx_hash_map_unlink(hash_map, slot, prev, elm);
225 static void *cx_hash_map_get(
229 // we can safely cast, because we know the map stays untouched
230 return cx_hash_map_get_remove((CxMap *) map, key, false, false);
233 static void *cx_hash_map_remove(
238 return cx_hash_map_get_remove(map, key, true, destroy);
241 static void *cx_hash_map_iter_current_entry(void const *it) {
242 struct cx_iterator_s const *iter = it;
243 // struct has to have a compatible signature
244 return (struct cx_map_entry_s *) &(iter->kv_data);
247 static void *cx_hash_map_iter_current_key(void const *it) {
248 struct cx_iterator_s const *iter = it;
249 struct cx_hash_map_element_s *elm = iter->elem_handle;
253 static void *cx_hash_map_iter_current_value(void const *it) {
254 struct cx_iterator_s const *iter = it;
255 struct cx_hash_map_s const *map = iter->src_handle;
256 struct cx_hash_map_element_s *elm = iter->elem_handle;
257 if (map->base.store_pointer) {
258 return *(void **) elm->data;
264 static bool cx_hash_map_iter_valid(void const *it) {
265 struct cx_iterator_s const *iter = it;
266 return iter->elem_handle != NULL;
269 static void cx_hash_map_iter_next(void *it) {
270 struct cx_iterator_s *iter = it;
271 struct cx_hash_map_element_s *elm = iter->elem_handle;
273 // remove current element, if asked
274 if (iter->base.remove) {
275 // obtain mutable pointer to the map
276 struct cx_mut_iterator_s *miter = it;
277 struct cx_hash_map_s *map = miter->src_handle;
280 iter->base.remove = false;
282 // determine the next element
283 struct cx_hash_map_element_s *next = elm->next;
285 // search the previous element
286 struct cx_hash_map_element_s *prev = NULL;
287 if (map->buckets[iter->slot] != elm) {
288 prev = map->buckets[iter->slot];
289 while (prev->next != elm) {
295 cx_invoke_destructor((struct cx_map_s *) map, elm->data);
298 cx_hash_map_unlink(map, iter->slot, prev, elm);
308 // search the next bucket, if required
309 struct cx_hash_map_s const *map = iter->src_handle;
310 while (elm == NULL && ++iter->slot < map->bucket_count) {
311 elm = map->buckets[iter->slot];
314 // fill the struct with the next element
315 iter->elem_handle = elm;
317 iter->kv_data.key = NULL;
318 iter->kv_data.value = NULL;
320 iter->kv_data.key = &elm->key;
321 if (map->base.store_pointer) {
322 iter->kv_data.value = *(void **) elm->data;
324 iter->kv_data.value = elm->data;
329 static bool cx_hash_map_iter_flag_rm(void *it) {
330 struct cx_iterator_base_s *iter = it;
331 if (iter->mutating) {
339 static CxIterator cx_hash_map_iterator(
341 enum cx_map_iterator_type type
345 iter.src_handle = map;
346 iter.base.valid = cx_hash_map_iter_valid;
347 iter.base.next = cx_hash_map_iter_next;
350 case CX_MAP_ITERATOR_PAIRS:
351 iter.base.current = cx_hash_map_iter_current_entry;
353 case CX_MAP_ITERATOR_KEYS:
354 iter.base.current = cx_hash_map_iter_current_key;
356 case CX_MAP_ITERATOR_VALUES:
357 iter.base.current = cx_hash_map_iter_current_value;
363 iter.base.flag_removal = cx_hash_map_iter_flag_rm;
364 iter.base.remove = false;
365 iter.base.mutating = false;
371 struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
372 struct cx_hash_map_element_s *elm = hash_map->buckets[0];
373 while (elm == NULL) {
374 elm = hash_map->buckets[++iter.slot];
376 iter.elem_handle = elm;
377 iter.kv_data.key = &elm->key;
378 if (map->store_pointer) {
379 iter.kv_data.value = *(void **) elm->data;
381 iter.kv_data.value = elm->data;
384 iter.elem_handle = NULL;
385 iter.kv_data.key = NULL;
386 iter.kv_data.value = NULL;
392 static cx_map_class cx_hash_map_class = {
393 cx_hash_map_destructor,
398 cx_hash_map_iterator,
401 CxMap *cxHashMapCreate(
402 CxAllocator const *allocator,
407 // implementation defined default
411 struct cx_hash_map_s *map = cxCalloc(allocator, 1,
412 sizeof(struct cx_hash_map_s));
413 if (map == NULL) return NULL;
415 // initialize hash map members
416 map->bucket_count = buckets;
417 map->buckets = cxCalloc(allocator, buckets,
418 sizeof(struct cx_hash_map_element_s *));
419 if (map->buckets == NULL) {
420 cxFree(allocator, map);
424 // initialize base members
425 map->base.cl = &cx_hash_map_class;
426 map->base.allocator = allocator;
429 map->base.store_pointer = false;
430 map->base.item_size = itemsize;
432 map->base.store_pointer = true;
433 map->base.item_size = sizeof(void *);
436 return (CxMap *) map;
439 int cxMapRehash(CxMap *map) {
440 struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
441 if (map->size > ((hash_map->bucket_count * 3) >> 2)) {
443 size_t new_bucket_count = (map->size * 5) >> 1;
444 struct cx_hash_map_element_s **new_buckets = cxCalloc(
446 new_bucket_count, sizeof(struct cx_hash_map_element_s *)
449 if (new_buckets == NULL) {
453 // iterate through the elements and assign them to their new slots
454 cx_for_n(slot, hash_map->bucket_count) {
455 struct cx_hash_map_element_s *elm = hash_map->buckets[slot];
456 while (elm != NULL) {
457 struct cx_hash_map_element_s *next = elm->next;
458 size_t new_slot = elm->key.hash % new_bucket_count;
460 // find position where to insert
461 struct cx_hash_map_element_s *bucket_next = new_buckets[new_slot];
462 struct cx_hash_map_element_s *bucket_prev = NULL;
463 while (bucket_next != NULL &&
464 bucket_next->key.hash < elm->key.hash) {
465 bucket_prev = bucket_next;
466 bucket_next = bucket_next->next;
470 if (bucket_prev == NULL) {
471 elm->next = new_buckets[new_slot];
472 new_buckets[new_slot] = elm;
474 bucket_prev->next = elm;
475 elm->next = bucket_next;
483 // assign result to the map
484 hash_map->bucket_count = new_bucket_count;
485 cxFree(map->allocator, hash_map->buckets);
486 hash_map->buckets = new_buckets;