ucx/hash_map.c

changeset 31
287484519844
parent 23
b26390e77237
--- a/ucx/hash_map.c	Fri Dec 12 10:42:53 2025 +0100
+++ b/ucx/hash_map.c	Fri Dec 19 17:22:03 2025 +0100
@@ -78,7 +78,7 @@
     cxFree(map->collection.allocator, map);
 }
 
-static void *cx_hash_map_put(
+static CxMapEntry cx_hash_map_put(
         CxMap *map,
         CxHashKey key,
         void *value
@@ -117,7 +117,7 @@
                 allocator,
                 sizeof(struct cx_hash_map_element_s) + map->collection.elem_size
         );
-        if (e == NULL) return NULL; // LCOV_EXCL_LINE
+        if (e == NULL) return (CxMapEntry){NULL, NULL}; // LCOV_EXCL_LINE
 
         // write the value
         if (value == NULL) {
@@ -132,7 +132,7 @@
         void *kd = cxMalloc(allocator, key.len);
         if (kd == NULL) { // LCOV_EXCL_START
             cxFree(allocator, e);
-            return NULL;
+            return (CxMapEntry){NULL, NULL};
         } // LCOV_EXCL_STOP
         memcpy(kd, key.data, key.len);
         e->key.data = kd;
@@ -152,8 +152,8 @@
         map->collection.size++;
     }
 
-    // return pointer to the element
-    return elm->data;
+    // return the entry
+    return (CxMapEntry){&elm->key, elm->data};
 }
 
 static void cx_hash_map_unlink(
@@ -414,8 +414,7 @@
         buckets = 16;
     }
 
-    struct cx_hash_map_s *map = cxCalloc(allocator, 1,
-                                         sizeof(struct cx_hash_map_s));
+    struct cx_hash_map_s *map = cxZalloc(allocator, sizeof(struct cx_hash_map_s));
     if (map == NULL) return NULL;
 
     // initialize hash map members
@@ -433,9 +432,12 @@
 
     if (itemsize > 0) {
         map->base.collection.elem_size = itemsize;
+        map->base.collection.advanced_cmp = cx_ccmp_memcmp;
+        map->base.collection.cmp_data = &map->base.collection.elem_size;
     } else {
         map->base.collection.elem_size = sizeof(void *);
         map->base.collection.store_pointer = true;
+        map->base.collection.simple_cmp = cx_cmp_ptr;
     }
 
     return (CxMap *) map;

mercurial