src/map.c

changeset 1474
84de0ba791af
parent 1467
bb6f04c35310
--- a/src/map.c	Wed Nov 05 22:39:39 2025 +0100
+++ b/src/map.c	Wed Nov 05 23:04:46 2025 +0100
@@ -267,3 +267,29 @@
     }
     return 0;
 }
+
+int cxMapUnion(CxMap *dst, const CxMap *src,
+        cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) {
+    if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator;
+
+    CxMapIterator src_iter = cxMapIterator(src);
+    cx_foreach(const CxMapEntry *, entry, src_iter) {
+        if (cxMapContains(dst, *entry->key)) {
+            continue;
+        }
+        void** dst_mem = cxMapEmplace(dst, *entry->key);
+        if (dst_mem == NULL) {
+            return 1; // LCOV_EXCL_LINE
+        }
+        void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem;
+        void* dst_ptr = clone_func(target, entry->value, clone_allocator, data);
+        if (dst_ptr == NULL) {
+            cx_map_remove_uninitialized_entry(dst, *(entry->key));
+            return 1;
+        }
+        if (cxCollectionStoresPointers(dst)) {
+            *dst_mem = dst_ptr;
+        }
+    }
+    return 0;
+}

mercurial