ucx/hash_map.c

branch
dav-2
changeset 889
42cdbf9bbd49
parent 886
da79af4baec8
--- a/ucx/hash_map.c	Tue Oct 14 21:02:26 2025 +0200
+++ b/ucx/hash_map.c	Sat Nov 08 23:06:11 2025 +0100
@@ -86,7 +86,7 @@
     struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
     const CxAllocator *allocator = map->collection.allocator;
 
-    unsigned hash = key.hash;
+    uint64_t hash = key.hash;
     if (hash == 0) {
         cx_hash_murmur(&key);
         hash = key.hash;
@@ -101,8 +101,7 @@
         elm = elm->next;
     }
 
-    if (elm != NULL && elm->key.hash == hash && elm->key.len == key.len &&
-        memcmp(elm->key.data, key.data, key.len) == 0) {
+    if (elm != NULL && cx_hash_key_cmp(&elm->key, &key) == 0) {
         // overwrite existing element, but call destructors first
         cx_invoke_destructor(map, elm->data);
         if (value == NULL) {
@@ -204,7 +203,7 @@
 ) {
     struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
 
-    unsigned hash = key.hash;
+    uint64_t hash = key.hash;
     if (hash == 0) {
         cx_hash_murmur(&key);
         hash = key.hash;
@@ -214,27 +213,25 @@
     struct cx_hash_map_element_s *elm = hash_map->buckets[slot];
     struct cx_hash_map_element_s *prev = NULL;
     while (elm && elm->key.hash <= hash) {
-        if (elm->key.hash == hash && elm->key.len == key.len) {
-            if (memcmp(elm->key.data, key.data, key.len) == 0) {
-                if (remove) {
-                    if (targetbuf == NULL) {
-                        cx_invoke_destructor(map, elm->data);
-                    } else {
-                        memcpy(targetbuf, elm->data, map->collection.elem_size);
-                    }
-                    cx_hash_map_unlink(hash_map, slot, prev, elm);
+        if (cx_hash_key_cmp(&elm->key, &key) == 0) {
+            if (remove) {
+                if (targetbuf == NULL) {
+                    cx_invoke_destructor(map, elm->data);
                 } else {
-                    assert(targetbuf != NULL);
-                    void *data = NULL;
-                    if (map->collection.store_pointer) {
-                        data = *(void **) elm->data;
-                    } else {
-                        data = elm->data;
-                    }
-                    memcpy(targetbuf, &data, sizeof(void *));
+                    memcpy(targetbuf, elm->data, map->collection.elem_size);
                 }
-                return 0;
+                cx_hash_map_unlink(hash_map, slot, prev, elm);
+            } else {
+                assert(targetbuf != NULL);
+                void *data = NULL;
+                if (map->collection.store_pointer) {
+                    data = *(void **) elm->data;
+                } else {
+                    data = elm->data;
+                }
+                memcpy(targetbuf, &data, sizeof(void *));
             }
+            return 0;
         }
         prev = elm;
         elm = prev->next;
@@ -269,19 +266,12 @@
 
 static void *cx_hash_map_iter_current_key(const void *it) {
     const CxMapIterator *iter = it;
-    struct cx_hash_map_element_s *elm = iter->elem;
-    return &elm->key;
+    return (void*) iter->entry.key;
 }
 
 static void *cx_hash_map_iter_current_value(const void *it) {
     const CxMapIterator *iter = it;
-    const CxMap *map = iter->map.c;
-    struct cx_hash_map_element_s *elm = iter->elem;
-    if (map->collection.store_pointer) {
-        return *(void **) elm->data;
-    } else {
-        return elm->data;
-    }
+    return iter->entry.value;
 }
 
 static bool cx_hash_map_iter_valid(const void *it) {
@@ -291,7 +281,7 @@
 
 static void cx_hash_map_iter_next(void *it) {
     CxMapIterator *iter = it;
-    CxMap *map = iter->map.m;
+    CxMap *map = iter->map;
     struct cx_hash_map_s *hmap = (struct cx_hash_map_s *) map;
     struct cx_hash_map_element_s *elm = iter->elem;
 
@@ -318,6 +308,7 @@
 
         // unlink
         cx_hash_map_unlink(hmap, iter->slot, prev, elm);
+        iter->elem_count--;
 
         // advance
         elm = next;
@@ -338,7 +329,7 @@
     // must not modify the iterator (the parameter is const)
     if (elm != NULL) {
         iter->entry.key = &elm->key;
-        if (iter->map.c->collection.store_pointer) {
+        if (map->collection.store_pointer) {
             iter->entry.value = *(void **) elm->data;
         } else {
             iter->entry.value = elm->data;
@@ -352,7 +343,7 @@
 ) {
     CxMapIterator iter;
 
-    iter.map.c = map;
+    iter.map = (CxMap*) map;
     iter.elem_count = map->collection.size;
 
     switch (type) {
@@ -375,7 +366,7 @@
     iter.base.valid = cx_hash_map_iter_valid;
     iter.base.next = cx_hash_map_iter_next;
     iter.base.remove = false;
-    iter.base.mutating = false;
+    iter.base.allow_remove = true;
 
     iter.slot = 0;
     iter.index = 0;

mercurial