# HG changeset patch # User Mike Becker # Date 1761479443 -3600 # Node ID bbca398783eddefde214ebfe45658fdf5ef62277 # Parent 0f0fe7311b768572916daa16ac2770ac9a41e20e simplify clone implementations diff -r 0f0fe7311b76 -r bbca398783ed src/list.c --- a/src/list.c Sun Oct 26 12:44:33 2025 +0100 +++ b/src/list.c Sun Oct 26 12:50:43 2025 +0100 @@ -820,30 +820,20 @@ // now clone the elements size_t cloned = empl_iter.elem_count; - if (cxCollectionStoresPointers(dst)) { - for (size_t i = 0 ; i < empl_iter.elem_count; i++) { - void *src_elem = cxIteratorCurrent(src_iter); - void **dest_memory = cxIteratorCurrent(empl_iter); - void *dest_ptr = clone_func(NULL, src_elem, clone_allocator, data); - if (dest_ptr == NULL) { - cloned = i; - break; - } + for (size_t i = 0 ; i < empl_iter.elem_count; i++) { + void *src_elem = cxIteratorCurrent(src_iter); + void **dest_memory = cxIteratorCurrent(empl_iter); + void *target = cxCollectionStoresPointers(dst) ? NULL : dest_memory; + void *dest_ptr = clone_func(target, src_elem, clone_allocator, data); + if (dest_ptr == NULL) { + cloned = i; + break; + } + if (cxCollectionStoresPointers(dst)) { *dest_memory = dest_ptr; - cxIteratorNext(src_iter); - cxIteratorNext(empl_iter); } - } else { - for (size_t i = 0 ; i < empl_iter.elem_count; i++) { - void *src_elem = cxIteratorCurrent(src_iter); - void *dest_memory = cxIteratorCurrent(empl_iter); - if (clone_func(dest_memory, src_elem, clone_allocator, data) == NULL) { - cloned = i; - break; - } - cxIteratorNext(src_iter); - cxIteratorNext(empl_iter); - } + cxIteratorNext(src_iter); + cxIteratorNext(empl_iter); } // if we could not clone everything, free the allocated memory diff -r 0f0fe7311b76 -r bbca398783ed src/map.c --- 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; }