src/map.c

changeset 1449
bbca398783ed
parent 1446
5732947cbcc2
child 1450
09a73312d5ec
--- a/src/map.c	Sun Oct 26 12:44:33 2025 +0100
+++ b/src/map.c	Sun Oct 26 12:50:43 2025 +0100
@@ -144,34 +144,22 @@
         const CxAllocator *clone_allocator, void *data) {
     if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator;
     CxMapIterator src_iter = cxMapIterator(src);
-    if (cxCollectionStoresPointers(dst)) {
-        for (size_t i = 0; i < cxMapSize(src); i++) {
-            const CxMapEntry *entry = cxIteratorCurrent(src_iter);
-            void **dst_mem = cxMapEmplace(dst, *(entry->key));
-            if (dst_mem == NULL) {
-                return 1;
-            }
-            void *dst_ptr = clone_func(NULL, entry->value, clone_allocator, data);
-            if (dst_ptr == NULL) {
-                cx_map_remove_uninitialized_entry(dst, *(entry->key));
-                return 1;
-            }
+    for (size_t i = 0; i < cxMapSize(src); i++) {
+        const CxMapEntry *entry = cxIteratorCurrent(src_iter);
+        void **dst_mem = cxMapEmplace(dst, *(entry->key));
+        if (dst_mem == NULL) {
+            return 1;
+        }
+        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;
-            cxIteratorNext(src_iter);
         }
-    } else {
-        for (size_t i = 0; i < cxMapSize(src); i++) {
-            const CxMapEntry *entry = cxIteratorCurrent(src_iter);
-            void *dst_mem = cxMapEmplace(dst, *(entry->key));
-            if (dst_mem == NULL) {
-                return 1;
-            }
-            if (clone_func(dst_mem, entry->value, clone_allocator, data) == NULL) {
-                cx_map_remove_uninitialized_entry(dst, *(entry->key));
-                return 1;
-            }
-            cxIteratorNext(src_iter);
-        }
+        cxIteratorNext(src_iter);
     }
     return 0;
 }
@@ -182,43 +170,26 @@
 
     const bool map_was_not_empty = cxMapSize(dst) > 0;
     CxMapIterator src_iter = cxMapIterator(minuend);
-    if (cxCollectionStoresPointers(dst)) {
-        cx_foreach(const CxMapEntry *, entry, src_iter) {
-            if (cxMapContains(subtrahend, *entry->key)) {
-                if (map_was_not_empty) {
-                    cxMapRemove(dst, *entry->key);
-                }
-            } else {
-                void** dst_mem = cxMapEmplace(dst, *entry->key);
-                if (dst_mem == NULL) {
-                    return 1;
-                }
-
-                void* dst_ptr = clone_func(NULL, entry->value, clone_allocator, data);
-                if (dst_ptr == NULL) {
-                    cx_map_remove_uninitialized_entry(dst, *(entry->key));
-                    return 1;
-                }
+    cx_foreach(const CxMapEntry *, entry, src_iter) {
+        if (cxMapContains(subtrahend, *entry->key)) {
+            if (map_was_not_empty) {
+                cxMapRemove(dst, *entry->key);
+            }
+        } else {
+            void** dst_mem = cxMapEmplace(dst, *entry->key);
+            if (dst_mem == NULL) {
+                return 1;
+            }
+            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;
             }
         }
-    } else {
-        cx_foreach(const CxMapEntry *, entry, src_iter) {
-            if (cxMapContains(subtrahend, *entry->key)) {
-                if (map_was_not_empty) {
-                    cxMapRemove(dst, *entry->key);
-                }
-            } else {
-                void* dst_mem = cxMapEmplace(dst, *entry->key);
-                if (dst_mem == NULL) {
-                    return 1;
-                }
-                if (NULL == clone_func(dst_mem, entry->value, clone_allocator, data)) {
-                    cx_map_remove_uninitialized_entry(dst, *entry->key);
-                    return 1;
-                }
-            }
-        }
     }
     return 0;
 }
@@ -227,43 +198,26 @@
         cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) {
     const bool map_was_not_empty = cxMapSize(dst) > 0;
     CxMapIterator src_iter = cxMapIterator(src);
-    if (cxCollectionStoresPointers(dst)) {
-        cx_foreach(const CxMapEntry *, entry, src_iter) {
-            if (cxListContains(keys, entry->key)) {
-                if (map_was_not_empty) {
-                    cxMapRemove(dst, *entry->key);
-                }
-            } else {
-                void** dst_mem = cxMapEmplace(dst, *entry->key);
-                if (dst_mem == NULL) {
-                    return 1;
-                }
-
-                void* dst_ptr = clone_func(NULL, entry->value, clone_allocator, data);
-                if (dst_ptr == NULL) {
-                    cx_map_remove_uninitialized_entry(dst, *(entry->key));
-                    return 1;
-                }
+    cx_foreach(const CxMapEntry *, entry, src_iter) {
+        if (cxListContains(keys, entry->key)) {
+            if (map_was_not_empty) {
+                cxMapRemove(dst, *entry->key);
+            }
+        } else {
+            void** dst_mem = cxMapEmplace(dst, *entry->key);
+            if (dst_mem == NULL) {
+                return 1;
+            }
+            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;
             }
         }
-    } else {
-        cx_foreach(const CxMapEntry *, entry, src_iter) {
-            if (cxListContains(keys, entry->key)) {
-                if (map_was_not_empty) {
-                    cxMapRemove(dst, *entry->key);
-                }
-            } else {
-                void* dst_mem = cxMapEmplace(dst, *entry->key);
-                if (dst_mem == NULL) {
-                    return 1;
-                }
-                if (NULL == clone_func(dst_mem, entry->value, clone_allocator, data)) {
-                    cx_map_remove_uninitialized_entry(dst, *entry->key);
-                    return 1;
-                }
-            }
-        }
     }
     return 0;
 }

mercurial