src/map.c

changeset 1437
dde4903c0fd7
parent 1429
6e0c3a8a914a
child 1438
2ca9e03ceeec
--- a/src/map.c	Tue Oct 21 17:06:17 2025 +0200
+++ b/src/map.c	Wed Oct 22 23:28:07 2025 +0200
@@ -127,3 +127,39 @@
     if (map == NULL) return;
     map->cl->deallocate(map);
 }
+
+size_t cxMapClone(CxMap *dst, const CxMap *src, cx_clone_func clone_func,
+        const CxAllocator *clone_allocator, void *data) {
+    CxMapIterator src_iter = cxMapIterator(src);
+    size_t i = 0;
+    if (cxCollectionStoresPointers(dst)) {
+        for (; i < cxMapSize(src); i++) {
+            const CxMapEntry *entry = cxIteratorCurrent(src_iter);
+            void **dst_mem = cxMapEmplace(dst, *(entry->key));
+            if (dst_mem == NULL) {
+                return i;
+            }
+            void *dst_ptr = clone_func(NULL, entry->value, clone_allocator, data);
+            if (dst_ptr == NULL) {
+                // TODO: remove the entry to avoid calling destructors on uninitialized memory
+                return i;
+            }
+            *dst_mem = dst_ptr;
+            cxIteratorNext(src_iter);
+        }
+    } else {
+        for (; i < cxMapSize(src); i++) {
+            const CxMapEntry *entry = cxIteratorCurrent(src_iter);
+            void *dst_mem = cxMapEmplace(dst, *(entry->key));
+            if (dst_mem == NULL) {
+                return i;
+            }
+            if (clone_func(dst_mem, entry->value, clone_allocator, data) == NULL) {
+                // TODO: remove the entry to avoid calling destructors on uninitialized memory
+                return i;
+            }
+            cxIteratorNext(src_iter);
+        }
+    }
+    return i;
+}
\ No newline at end of file

mercurial