--- a/ucx/list.c Sun Oct 19 21:20:08 2025 +0200 +++ b/ucx/list.c Mon Nov 10 21:52:51 2025 +0100 @@ -29,6 +29,7 @@ #include "cx/list.h" #include <string.h> +#include <assert.h> // <editor-fold desc="Store Pointers Functionality"> @@ -114,7 +115,7 @@ const void *elem, int prepend ) { - struct cx_list_s *list = iter->src_handle.m; + struct cx_list_s *list = iter->src_handle; return list->climpl->insert_iter(iter, &elem, prepend); } @@ -184,6 +185,10 @@ return ptr == NULL ? NULL : *ptr; } +static int cx_pl_change_capacity(struct cx_list_s *list, size_t cap) { + return list->climpl->change_capacity(list, cap); +} + static struct cx_iterator_s cx_pl_iterator( const struct cx_list_s *list, size_t index, @@ -210,6 +215,7 @@ cx_pl_sort, cx_pl_compare, cx_pl_reverse, + cx_pl_change_capacity, cx_pl_iterator, }; // </editor-fold> @@ -245,7 +251,7 @@ cx_attr_unused bool backwards ) { CxIterator iter = {0}; - iter.src_handle.c = list; + iter.src_handle = (void*) list; iter.index = index; iter.base.valid = cx_emptyl_iter_valid; return iter; @@ -266,6 +272,7 @@ cx_emptyl_noop, NULL, cx_emptyl_noop, + NULL, cx_emptyl_iterator, }; @@ -299,16 +306,17 @@ const void *data, size_t n ) { - size_t elem_size = list->collection.elem_size; const char *src = data; size_t i = 0; for (; i < n; i++) { if (NULL == invoke_list_func( - insert_element, list, index + i, - src + i * elem_size) + insert_element, list, index + i, src) ) { return i; // LCOV_EXCL_LINE } + if (src != NULL) { + src += list->collection.elem_size; + } } return i; } @@ -566,36 +574,174 @@ } } -CxIterator cxListMutIteratorAt( - CxList *list, - size_t index -) { - if (list == NULL) list = cxEmptyList; - CxIterator it = list->cl->iterator(list, index, false); - it.base.mutating = true; - return it; +size_t cxListSize(const CxList *list) { + return list->collection.size; +} + +int cxListAdd(CxList *list, const void *elem) { + list->collection.sorted = false; + return list->cl->insert_element(list, list->collection.size, elem) == NULL; +} + +size_t cxListAddArray(CxList *list, const void *array, size_t n) { + list->collection.sorted = false; + return list->cl->insert_array(list, list->collection.size, array, n); +} + +int cxListInsert(CxList *list, size_t index, const void *elem) { + list->collection.sorted = false; + return list->cl->insert_element(list, index, elem) == NULL; +} + +void *cxListEmplaceAt(CxList *list, size_t index) { + list->collection.sorted = false; + return list->cl->insert_element(list, index, NULL); +} + +void *cxListEmplace(CxList *list) { + list->collection.sorted = false; + return list->cl->insert_element(list, list->collection.size, NULL); +} + +static bool cx_list_emplace_iterator_valid(const void *it) { + const CxIterator *iter = it; + return iter->index < iter->elem_count; +} + +CxIterator cxListEmplaceArrayAt(CxList *list, size_t index, size_t n) { + list->collection.sorted = false; + size_t c = list->cl->insert_array(list, index, NULL, n); + CxIterator iter = list->cl->iterator(list, index, false); + // tweak the fields of this iterator + iter.elem_count = c; + iter.index = 0; + // replace the valid function to abort iteration when c is reached + iter.base.valid = cx_list_emplace_iterator_valid; + // if we are storing pointers, we want to return the pure pointers. + // therefore, we must unwrap the "current" method + if (list->collection.store_pointer) { + iter.base.current = iter.base.current_impl; + } + return iter; +} + +CxIterator cxListEmplaceArray(CxList *list, size_t n) { + return cxListEmplaceArrayAt(list, list->collection.size, n); +} + +int cxListInsertSorted(CxList *list, const void *elem) { + assert(cxCollectionSorted(list)); + list->collection.sorted = true; + const void *data = list->collection.store_pointer ? &elem : elem; + return list->cl->insert_sorted(list, data, 1) == 0; +} + +int cxListInsertUnique(CxList *list, const void *elem) { + if (cxCollectionSorted(list)) { + list->collection.sorted = true; + const void *data = list->collection.store_pointer ? &elem : elem; + return list->cl->insert_unique(list, data, 1) == 0; + } else { + if (cxListContains(list, elem)) { + return 0; + } else { + return cxListAdd(list, elem); + } + } +} + +size_t cxListInsertArray(CxList *list, size_t index, const void *array, size_t n) { + list->collection.sorted = false; + return list->cl->insert_array(list, index, array, n); } -CxIterator cxListMutBackwardsIteratorAt( - CxList *list, - size_t index -) { - if (list == NULL) list = cxEmptyList; - CxIterator it = list->cl->iterator(list, index, true); - it.base.mutating = true; - return it; +size_t cxListInsertSortedArray(CxList *list, const void *array, size_t n) { + assert(cxCollectionSorted(list)); + list->collection.sorted = true; + return list->cl->insert_sorted(list, array, n); +} + +size_t cxListInsertUniqueArray(CxList *list, const void *array, size_t n) { + if (cxCollectionSorted(list)) { + list->collection.sorted = true; + return list->cl->insert_unique(list, array, n); + } else { + const char *source = array; + for (size_t i = 0 ; i < n; i++) { + // note: this also checks elements added in a previous iteration + const void *data = list->collection.store_pointer ? + *((const void**)source) : source; + if (!cxListContains(list, data)) { + if (cxListAdd(list, data)) { + return i; // LCOV_EXCL_LINE + } + } + source += list->collection.elem_size; + } + return n; + } +} + +int cxListInsertAfter(CxIterator *iter, const void *elem) { + CxList* list = (CxList*)iter->src_handle; + list->collection.sorted = false; + return list->cl->insert_iter(iter, elem, 0); +} + +int cxListInsertBefore(CxIterator *iter, const void *elem) { + CxList* list = (CxList*)iter->src_handle; + list->collection.sorted = false; + return list->cl->insert_iter(iter, elem, 1); +} + +int cxListRemove(CxList *list, size_t index) { + return list->cl->remove(list, index, 1, NULL) == 0; } -void cxListFree(CxList *list) { - if (list == NULL) return; - list->cl->deallocate(list); +int cxListRemoveAndGet(CxList *list, size_t index, void *targetbuf) { + return list->cl->remove(list, index, 1, targetbuf) == 0; +} + +int cxListRemoveAndGetFirst(CxList *list, void *targetbuf) { + return list->cl->remove(list, 0, 1, targetbuf) == 0; +} + +int cxListRemoveAndGetLast(CxList *list, void *targetbuf) { + // note: index may wrap - member function will catch that + return list->cl->remove(list, list->collection.size - 1, 1, targetbuf) == 0; +} + +size_t cxListRemoveArray(CxList *list, size_t index, size_t num) { + return list->cl->remove(list, index, num, NULL); +} + +size_t cxListRemoveArrayAndGet(CxList *list, size_t index, size_t num, void *targetbuf) { + return list->cl->remove(list, index, num, targetbuf); } -int cxListSet( - CxList *list, - size_t index, - const void *elem -) { +void cxListClear(CxList *list) { + list->cl->clear(list); + list->collection.sorted = true; // empty lists are always sorted +} + +int cxListSwap(CxList *list, size_t i, size_t j) { + list->collection.sorted = false; + return list->cl->swap(list, i, j); +} + +void *cxListAt(const CxList *list, size_t index) { + return list->cl->at(list, index); +} + +void *cxListFirst(const CxList *list) { + return list->cl->at(list, 0); +} + +void *cxListLast(const CxList *list) { + return list->cl->at(list, list->collection.size - 1); +} + +int cxListSet(CxList *list, size_t index, const void *elem) { if (index >= list->collection.size) { return 1; } @@ -611,3 +757,371 @@ return 0; } + +CxIterator cxListIteratorAt(const CxList *list, size_t index) { + if (list == NULL) list = cxEmptyList; + return list->cl->iterator(list, index, false); +} + +CxIterator cxListBackwardsIteratorAt(const CxList *list, size_t index) { + if (list == NULL) list = cxEmptyList; + return list->cl->iterator(list, index, true); +} + +CxIterator cxListIterator(const CxList *list) { + if (list == NULL) list = cxEmptyList; + return list->cl->iterator(list, 0, false); +} + +CxIterator cxListBackwardsIterator(const CxList *list) { + if (list == NULL) list = cxEmptyList; + return list->cl->iterator(list, list->collection.size - 1, true); +} + +size_t cxListFind(const CxList *list, const void *elem) { + return list->cl->find_remove((CxList*)list, elem, false); +} + +bool cxListContains(const CxList* list, const void* elem) { + return list->cl->find_remove((CxList*)list, elem, false) < list->collection.size; +} + +bool cxListIndexValid(const CxList *list, size_t index) { + return index < list->collection.size; +} + +size_t cxListFindRemove(CxList *list, const void *elem) { + return list->cl->find_remove(list, elem, true); +} + +void cxListSort(CxList *list) { + if (list->collection.sorted) return; + list->cl->sort(list); + list->collection.sorted = true; +} + +void cxListReverse(CxList *list) { + // still sorted, but not according to the cmp_func + list->collection.sorted = false; + list->cl->reverse(list); +} + +void cxListFree(CxList *list) { + if (list == NULL) return; + list->cl->deallocate(list); +} + +static void cx_list_pop_uninitialized_elements(CxList *list, size_t n) { + cx_destructor_func destr_bak = list->collection.simple_destructor; + cx_destructor_func2 destr2_bak = list->collection.advanced_destructor; + list->collection.simple_destructor = NULL; + list->collection.advanced_destructor = NULL; + if (n == 1) { + cxListRemove(list, list->collection.size - 1); + } else { + cxListRemoveArray(list,list->collection.size - n, n); + } + list->collection.simple_destructor = destr_bak; + list->collection.advanced_destructor = destr2_bak; +} + +static void* cx_list_simple_clone_func(void *dst, const void *src, const CxAllocator *al, void *data) { + size_t elem_size = *(size_t*)data; + if (dst == NULL) dst = cxMalloc(al, elem_size); + if (dst != NULL) memcpy(dst, src, elem_size); + return dst; +} + +#define use_simple_clone_func(list) cx_list_simple_clone_func, NULL, (void*)&((list)->collection.elem_size) + +int cxListClone(CxList *dst, const CxList *src, cx_clone_func clone_func, + const CxAllocator *clone_allocator, void *data) { + if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; + + // remember the original size + size_t orig_size = dst->collection.size; + + // first, try to allocate the memory in the new list + CxIterator empl_iter = cxListEmplaceArray(dst, src->collection.size); + + // get an iterator over the source elements + CxIterator src_iter = cxListIterator(src); + + // now clone the elements + size_t cloned = empl_iter.elem_count; + 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); + } + + // if we could not clone everything, free the allocated memory + // (disable the destructors!) + if (cloned < src->collection.size) { + cx_list_pop_uninitialized_elements(dst, + dst->collection.size - cloned - orig_size); + return 1; + } + + // set the sorted flag when we know it's sorted + if (orig_size == 0 && src->collection.sorted) { + dst->collection.sorted = true; + } + + return 0; +} + +int cxListDifference(CxList *dst, + const CxList *minuend, const CxList *subtrahend, + cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { + if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; + + // optimize for sorted collections + if (cxCollectionSorted(minuend) && cxCollectionSorted(subtrahend)) { + bool dst_was_empty = cxCollectionSize(dst) == 0; + + CxIterator min_iter = cxListIterator(minuend); + CxIterator sub_iter = cxListIterator(subtrahend); + while (cxIteratorValid(min_iter)) { + void *min_elem = cxIteratorCurrent(min_iter); + void *sub_elem; + int d; + if (cxIteratorValid(sub_iter)) { + sub_elem = cxIteratorCurrent(sub_iter); + cx_compare_func cmp = subtrahend->collection.cmpfunc; + d = cmp(sub_elem, min_elem); + } else { + // no more elements in the subtrahend, + // i.e., the min_elem is larger than any elem of the subtrahend + d = 1; + } + if (d == 0) { + // is contained, so skip it + cxIteratorNext(min_iter); + } else if (d < 0) { + // subtrahend is smaller than minuend, + // check the next element + cxIteratorNext(sub_iter); + } else { + // subtrahend is larger than the dst element, + // clone the minuend and advance + void **dst_mem = cxListEmplace(dst); + void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; + void* dst_ptr = clone_func(target, min_elem, clone_allocator, data); + if (dst_ptr == NULL) { + cx_list_pop_uninitialized_elements(dst, 1); + return 1; + } + if (cxCollectionStoresPointers(dst)) { + *dst_mem = dst_ptr; + } + cxIteratorNext(min_iter); + } + } + + // if dst was empty, it is now guaranteed to be sorted + dst->collection.sorted = dst_was_empty; + } else { + CxIterator min_iter = cxListIterator(minuend); + cx_foreach(void *, elem, min_iter) { + if (cxListContains(subtrahend, elem)) { + continue; + } + void **dst_mem = cxListEmplace(dst); + void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; + void* dst_ptr = clone_func(target, elem, clone_allocator, data); + if (dst_ptr == NULL) { + cx_list_pop_uninitialized_elements(dst, 1); + return 1; + } + if (cxCollectionStoresPointers(dst)) { + *dst_mem = dst_ptr; + } + } + } + + return 0; +} + +int cxListIntersection(CxList *dst, + const CxList *src, const CxList *other, + cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { + if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; + + // optimize for sorted collections + if (cxCollectionSorted(src) && cxCollectionSorted(other)) { + bool dst_was_empty = cxCollectionSize(dst) == 0; + + CxIterator src_iter = cxListIterator(src); + CxIterator other_iter = cxListIterator(other); + while (cxIteratorValid(src_iter) && cxIteratorValid(other_iter)) { + void *src_elem = cxIteratorCurrent(src_iter); + void *other_elem = cxIteratorCurrent(other_iter); + int d = src->collection.cmpfunc(src_elem, other_elem); + if (d == 0) { + // is contained, clone it + void **dst_mem = cxListEmplace(dst); + void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; + void* dst_ptr = clone_func(target, src_elem, clone_allocator, data); + if (dst_ptr == NULL) { + cx_list_pop_uninitialized_elements(dst, 1); + return 1; + } + if (cxCollectionStoresPointers(dst)) { + *dst_mem = dst_ptr; + } + cxIteratorNext(src_iter); + } else if (d < 0) { + // the other element is larger, skip the source element + cxIteratorNext(src_iter); + } else { + // the source element is larger, try to find it in the other list + cxIteratorNext(other_iter); + } + } + + // if dst was empty, it is now guaranteed to be sorted + dst->collection.sorted = dst_was_empty; + } else { + CxIterator src_iter = cxListIterator(src); + cx_foreach(void *, elem, src_iter) { + if (!cxListContains(other, elem)) { + continue; + } + void **dst_mem = cxListEmplace(dst); + void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; + void* dst_ptr = clone_func(target, elem, clone_allocator, data); + if (dst_ptr == NULL) { + cx_list_pop_uninitialized_elements(dst, 1); + return 1; + } + if (cxCollectionStoresPointers(dst)) { + *dst_mem = dst_ptr; + } + } + } + + return 0; +} + +int cxListUnion(CxList *dst, + const CxList *src, const CxList *other, + cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { + if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; + + // optimize for sorted collections + if (cxCollectionSorted(src) && cxCollectionSorted(other)) { + bool dst_was_empty = cxCollectionSize(dst) == 0; + + CxIterator src_iter = cxListIterator(src); + CxIterator other_iter = cxListIterator(other); + while (cxIteratorValid(src_iter) || cxIteratorValid(other_iter)) { + void *src_elem, *other_elem; + int d; + if (!cxIteratorValid(src_iter)) { + other_elem = cxIteratorCurrent(other_iter); + d = 1; + } else if (!cxIteratorValid(other_iter)) { + src_elem = cxIteratorCurrent(src_iter); + d = -1; + } else { + src_elem = cxIteratorCurrent(src_iter); + other_elem = cxIteratorCurrent(other_iter); + d = src->collection.cmpfunc(src_elem, other_elem); + } + void *clone_from; + if (d < 0) { + // source element is smaller clone it + clone_from = src_elem; + cxIteratorNext(src_iter); + } else if (d == 0) { + // both elements are equal, clone from the source, skip other + clone_from = src_elem; + cxIteratorNext(src_iter); + cxIteratorNext(other_iter); + } else { + // the other element is smaller, clone it + clone_from = other_elem; + cxIteratorNext(other_iter); + } + void **dst_mem = cxListEmplace(dst); + void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; + void* dst_ptr = clone_func(target, clone_from, clone_allocator, data); + if (dst_ptr == NULL) { + cx_list_pop_uninitialized_elements(dst, 1); + return 1; + } + if (cxCollectionStoresPointers(dst)) { + *dst_mem = dst_ptr; + } + } + + // if dst was empty, it is now guaranteed to be sorted + dst->collection.sorted = dst_was_empty; + } else { + if (cxListClone(dst, src, clone_func, clone_allocator, data)) { + return 1; + } + CxIterator other_iter = cxListIterator(other); + cx_foreach(void *, elem, other_iter) { + if (cxListContains(src, elem)) { + continue; + } + void **dst_mem = cxListEmplace(dst); + void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; + void* dst_ptr = clone_func(target, elem, clone_allocator, data); + if (dst_ptr == NULL) { + cx_list_pop_uninitialized_elements(dst, 1); + return 1; + } + if (cxCollectionStoresPointers(dst)) { + *dst_mem = dst_ptr; + } + } + } + + return 0; +} + +int cxListCloneSimple(CxList *dst, const CxList *src) { + return cxListClone(dst, src, use_simple_clone_func(src)); +} + +int cxListDifferenceSimple(CxList *dst, const CxList *minuend, const CxList *subtrahend) { + return cxListDifference(dst, minuend, subtrahend, use_simple_clone_func(minuend)); +} + +int cxListIntersectionSimple(CxList *dst, const CxList *src, const CxList *other) { + return cxListIntersection(dst, src, other, use_simple_clone_func(src)); +} + +int cxListUnionSimple(CxList *dst, const CxList *src, const CxList *other) { + return cxListUnion(dst, src, other, use_simple_clone_func(src)); +} + +int cxListReserve(CxList *list, size_t capacity) { + if (list->cl->change_capacity == NULL) { + return 0; + } + if (capacity <= cxCollectionSize(list)) { + return 0; + } + return list->cl->change_capacity(list, capacity); +} + +int cxListShrink(CxList *list) { + if (list->cl->change_capacity == NULL) { + return 0; + } + return list->cl->change_capacity(list, cxCollectionSize(list)); +} \ No newline at end of file