ucx/hash_map.c

changeset 888
af685cc9d623
parent 854
1c8401ece69e
--- a/ucx/hash_map.c	Sun Aug 31 14:39:13 2025 +0200
+++ b/ucx/hash_map.c	Sat Nov 08 23:06:11 2025 +0100
@@ -78,7 +78,7 @@
     cxFree(map->collection.allocator, map);
 }
 
-static int cx_hash_map_put(
+static void *cx_hash_map_put(
         CxMap *map,
         CxHashKey key,
         void *value
@@ -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,11 +101,12 @@
         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 (map->collection.store_pointer) {
+        if (value == NULL) {
+            memset(elm->data, 0, map->collection.elem_size);
+        } else if (map->collection.store_pointer) {
             memcpy(elm->data, &value, sizeof(void *));
         } else {
             memcpy(elm->data, value, map->collection.elem_size);
@@ -116,10 +117,12 @@
                 allocator,
                 sizeof(struct cx_hash_map_element_s) + map->collection.elem_size
         );
-        if (e == NULL) return -1;
+        if (e == NULL) return NULL;
 
         // write the value
-        if (map->collection.store_pointer) {
+        if (value == NULL) {
+            memset(e->data, 0, map->collection.elem_size);
+        } else if (map->collection.store_pointer) {
             memcpy(e->data, &value, sizeof(void *));
         } else {
             memcpy(e->data, value, map->collection.elem_size);
@@ -127,7 +130,10 @@
 
         // copy the key
         void *kd = cxMalloc(allocator, key.len);
-        if (kd == NULL) return -1;
+        if (kd == NULL) {
+            cxFree(allocator, e);
+            return NULL;
+        }
         memcpy(kd, key.data, key.len);
         e->key.data = kd;
         e->key.len = key.len;
@@ -140,12 +146,14 @@
             prev->next = e;
         }
         e->next = elm;
+        elm = e;
 
         // increase the size
         map->collection.size++;
     }
 
-    return 0;
+    // return pointer to the element
+    return elm->data;
 }
 
 static void cx_hash_map_unlink(
@@ -195,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;
@@ -205,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;
@@ -260,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) {
@@ -282,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;
 
@@ -309,6 +308,7 @@
 
         // unlink
         cx_hash_map_unlink(hmap, iter->slot, prev, elm);
+        iter->elem_count--;
 
         // advance
         elm = next;
@@ -329,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;
@@ -343,7 +343,7 @@
 ) {
     CxMapIterator iter;
 
-    iter.map.c = map;
+    iter.map = (CxMap*) map;
     iter.elem_count = map->collection.size;
 
     switch (type) {
@@ -366,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