Sat, 25 Oct 2025 21:12:59 +0200
make clone functions return int instead of size_t
relates to #743
relates to #744
| docs/Writerside/topics/list.h.md | file | annotate | diff | comparison | revisions | |
| docs/Writerside/topics/map.h.md | file | annotate | diff | comparison | revisions | |
| src/cx/list.h | file | annotate | diff | comparison | revisions | |
| src/cx/map.h | file | annotate | diff | comparison | revisions | |
| src/list.c | file | annotate | diff | comparison | revisions | |
| src/map.c | file | annotate | diff | comparison | revisions | |
| tests/test_hash_map.c | file | annotate | diff | comparison | revisions | |
| tests/test_list.c | file | annotate | diff | comparison | revisions |
--- a/docs/Writerside/topics/list.h.md Fri Oct 24 21:15:14 2025 +0200 +++ b/docs/Writerside/topics/list.h.md Sat Oct 25 21:12:59 2025 +0200 @@ -364,7 +364,7 @@ #include <cx/list.h> -size_t cxListClone(CxList *dst, const CxList *src, +int cxListClone(CxList *dst, const CxList *src, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data); @@ -377,8 +377,7 @@ Refer to the documentation of the [clone-function callback](allocator.h.md#clone-function) to learn how to implement it. -The function returns the number of elements successfully cloned. -If an allocation error occurs, this might be smaller than the size of the source list. +The function returns zero if and only if all clone operations were successful. > It is perfectly possible to clone items into a list of a different type. > For example, you can clone elements from a list that is just storing pointers (`CX_STORE_POINTERS`) to a list that
--- a/docs/Writerside/topics/map.h.md Fri Oct 24 21:15:14 2025 +0200 +++ b/docs/Writerside/topics/map.h.md Sat Oct 25 21:12:59 2025 +0200 @@ -291,7 +291,7 @@ #include <cx/map.h> -size_t cxMapClone(CxMap *dst, const CxMap *src, +int cxMapClone(CxMap *dst, const CxMap *src, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data); @@ -304,8 +304,7 @@ Refer to the documentation of the [clone-function callback](allocator.h.md#clone-function) to learn how to implement it. -The function returns the number of elements successfully cloned. -If an allocation error occurs, this might be smaller than the size of the source map. +The function returns zero if and only if all clone operations were successful. > It is perfectly possible to clone items into a map of a different type. > For example, you can clone entries from a map that is just storing pointers (`CX_STORE_POINTERS`) to a map that
--- a/src/cx/list.h Fri Oct 24 21:15:14 2025 +0200 +++ b/src/cx/list.h Sat Oct 25 21:12:59 2025 +0200 @@ -976,10 +976,11 @@ * @param clone_func the clone function for the elements * @param clone_allocator the allocator that is passed to the clone function * @param data optional additional data that is passed to the clone function - * @return the number of elements that have been successfully cloned + * @retval zero when all elements were successfully cloned + * @retval non-zero when an allocation error occurred */ cx_attr_nonnull_arg(1, 2, 3) -CX_EXPORT size_t cxListClone(CxList *dst, const CxList *src, +CX_EXPORT int cxListClone(CxList *dst, const CxList *src, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data); #ifdef __cplusplus
--- a/src/cx/map.h Fri Oct 24 21:15:14 2025 +0200 +++ b/src/cx/map.h Sat Oct 25 21:12:59 2025 +0200 @@ -484,10 +484,11 @@ * @param clone_func the clone function for the values * @param clone_allocator the allocator that is passed to the clone function * @param data optional additional data that is passed to the clone function - * @return the number of elements that have been successfully cloned + * @retval zero when all elements were successfully cloned + * @retval non-zero when an allocation error occurred */ cx_attr_nonnull_arg(1, 2, 3) -CX_EXPORT size_t cxMapClone(CxMap *dst, const CxMap *src, +CX_EXPORT int cxMapClone(CxMap *dst, const CxMap *src, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data); #ifdef __cplusplus
--- a/src/list.c Fri Oct 24 21:15:14 2025 +0200 +++ b/src/list.c Sat Oct 25 21:12:59 2025 +0200 @@ -805,7 +805,7 @@ list->cl->deallocate(list); } -size_t cxListClone(CxList *dst, const CxList *src, cx_clone_func clone_func, +int cxListClone(CxList *dst, const CxList *src, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { // remember the original size @@ -857,8 +857,8 @@ dst->collection.size - cloned - orig_size); dst->collection.simple_destructor = destr_bak; dst->collection.advanced_destructor = destr2_bak; + return 1; } - // return how many elements we have cloned - return cloned; + return 0; } \ No newline at end of file
--- a/src/map.c Fri Oct 24 21:15:14 2025 +0200 +++ b/src/map.c Sat Oct 25 21:12:59 2025 +0200 @@ -138,38 +138,37 @@ map->collection.advanced_destructor = destr2_bak; } -size_t cxMapClone(CxMap *dst, const CxMap *src, cx_clone_func clone_func, +int 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++) { + 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 i; + 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 i; + return 1; } *dst_mem = dst_ptr; cxIteratorNext(src_iter); } } else { - for (; i < cxMapSize(src); i++) { + 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 i; + return 1; } if (clone_func(dst_mem, entry->value, clone_allocator, data) == NULL) { cx_map_remove_uninitialized_entry(dst, *(entry->key)); - return i; + return 1; } cxIteratorNext(src_iter); } } - return i; -} \ No newline at end of file + return 0; +}
--- a/tests/test_hash_map.c Fri Oct 24 21:15:14 2025 +0200 +++ b/tests/test_hash_map.c Sat Oct 25 21:12:59 2025 +0200 @@ -539,8 +539,7 @@ CX_TEST_DO { int c = 4; test_hash_map_clone_func_max_clones = 100; // no limit - size_t cloned = cxMapClone(dst, src, test_hash_map_clone_func, NULL, &c); - CX_TEST_ASSERT(cloned == 3); + CX_TEST_ASSERT(0 == cxMapClone(dst, src, test_hash_map_clone_func, NULL, &c)); CX_TEST_ASSERT(cxMapSize(dst) == 5); CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 1); CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k2")) == 13); @@ -570,8 +569,7 @@ CX_TEST_DO { int c = 4; test_hash_map_clone_func_max_clones = 2; - size_t cloned = cxMapClone(dst, src, test_hash_map_clone_func, NULL, &c); - CX_TEST_ASSERT(cloned == 2); + CX_TEST_ASSERT(0 != cxMapClone(dst, src, test_hash_map_clone_func, NULL, &c)); CX_TEST_ASSERT(cxMapSize(dst) == 4); CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 1); CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k2")) == 13); @@ -608,8 +606,7 @@ CX_TEST_DO { int c = 4; test_hash_map_clone_func_max_clones = 100; // no limit - size_t cloned = cxMapClone(dst, src, test_hash_map_clone_func, allocator, &c); - CX_TEST_ASSERT(cloned == 3); + CX_TEST_ASSERT(0 == cxMapClone(dst, src, test_hash_map_clone_func, allocator, &c)); CX_TEST_ASSERT(cxMapSize(dst) == 5); CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 1); CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k2")) == 13);
--- a/tests/test_list.c Fri Oct 24 21:15:14 2025 +0200 +++ b/tests/test_list.c Sat Oct 25 21:12:59 2025 +0200 @@ -2574,8 +2574,7 @@ int expected_data[12] = array_init(1, 2, 3, 4, 1, 3, 5, 7, 9, 11, 13, 15); int c = 0; test_clone_func_max_clones = 100; // no limit - size_t cloned = cxListClone(target, source, test_clone_func, testing_alloc, &c); - CX_TEST_ASSERT(cloned == 8); + CX_TEST_ASSERT(0 == cxListClone(target, source, test_clone_func, testing_alloc, &c)); CX_TEST_ASSERT(c == 8); CX_TEST_ASSERT(cxListSize(target) == 12); CX_TEST_ASSERT(cxListSize(source) == 8); @@ -2623,8 +2622,7 @@ int expected_data[9] = array_init(1, 2, 3, 4, 1, 3, 5, 7, 9); int c = 0; test_clone_func_max_clones = 5; - size_t cloned = cxListClone(target, source, test_clone_func, testing_alloc, &c); - CX_TEST_ASSERT(cloned == 5); + CX_TEST_ASSERT(0 != cxListClone(target, source, test_clone_func, testing_alloc, &c)); CX_TEST_ASSERT(c == 5); CX_TEST_ASSERT(cxListSize(target) == 9); CX_TEST_ASSERT(cxListSize(source) == 8);