Mon, 18 Dec 2023 16:14:07 +0100
make cx_cmp_ptr default comparator for pointer lists - relates to #340
src/array_list.c | file | annotate | diff | comparison | revisions | |
src/cx/array_list.h | file | annotate | diff | comparison | revisions | |
src/cx/linked_list.h | file | annotate | diff | comparison | revisions | |
src/linked_list.c | file | annotate | diff | comparison | revisions | |
tests/test_list.cpp | file | annotate | diff | comparison | revisions |
--- a/src/array_list.c Mon Dec 18 16:04:21 2023 +0100 +++ b/src/array_list.c Mon Dec 18 16:14:07 2023 +0100 @@ -27,6 +27,7 @@ */ #include "cx/array_list.h" +#include "cx/compare.h" #include <assert.h> #include <string.h> @@ -522,13 +523,14 @@ list->base.cl = &cx_array_list_class; list->base.allocator = allocator; - list->base.cmpfunc = comparator; list->capacity = initial_capacity; if (item_size > 0) { list->base.item_size = item_size; + list->base.cmpfunc = comparator; } else { item_size = sizeof(void *); + list->base.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator; cxListStorePointers((CxList *) list); }
--- a/src/cx/array_list.h Mon Dec 18 16:04:21 2023 +0100 +++ b/src/cx/array_list.h Mon Dec 18 16:14:07 2023 +0100 @@ -152,12 +152,14 @@ * Allocates an array list for storing elements with \p item_size bytes each. * * If \p item_size is CX_STORE_POINTERS, the created list will be created as if - * cxListStorePointers() was called immediately after creation. + * cxListStorePointers() was called immediately after creation and the compare + * function will be automatically set to cx_cmp_ptr(), if none is given. * * @param allocator the allocator for allocating the list memory * (if \c NULL the cxDefaultAllocator will be used) * @param comparator the comparator for the elements - * (if \c NULL sort and find functions will not work) + * (if \c NULL, and the list is not storing pointers, sort and find + * functions will not work) * @param item_size the size of each element in bytes * @param initial_capacity the initial number of elements the array can store * @return the created list @@ -177,7 +179,8 @@ * set it immediately after creation or use cxArrayListCreate(). * * If \p item_size is CX_STORE_POINTERS, the created list will be created as if - * cxListStorePointers() was called immediately after creation. + * cxListStorePointers() was called immediately after creation and the compare + * function will be automatically set to cx_cmp_ptr(). * * @param item_size the size of each element in bytes * @param initial_capacity the initial number of elements the array can store
--- a/src/cx/linked_list.h Mon Dec 18 16:04:21 2023 +0100 +++ b/src/cx/linked_list.h Mon Dec 18 16:14:07 2023 +0100 @@ -54,12 +54,14 @@ * Allocates a linked list for storing elements with \p item_size bytes each. * * If \p item_size is CX_STORE_POINTERS, the created list will be created as if - * cxListStorePointers() was called immediately after creation. + * cxListStorePointers() was called immediately after creation and the compare + * function will be automatically set to cx_cmp_ptr(), if none is given. * * @param allocator the allocator for allocating the list nodes * (if \c NULL the cxDefaultAllocator will be used) * @param comparator the comparator for the elements - * (if \c NULL sort and find functions will not work) + * (if \c NULL, and the list is not storing pointers, sort and find + * functions will not work) * @param item_size the size of each element in bytes * @return the created list */ @@ -77,7 +79,8 @@ * after list creation or use cxLinkedListCreate(). * * If \p item_size is CX_STORE_POINTERS, the created list will be created as if - * cxListStorePointers() was called immediately after creation. + * cxListStorePointers() was called immediately after creation and the compare + * function will be automatically set to cx_cmp_ptr(). * * @param item_size the size of each element in bytes * @return the created list
--- a/src/linked_list.c Mon Dec 18 16:04:21 2023 +0100 +++ b/src/linked_list.c Mon Dec 18 16:14:07 2023 +0100 @@ -28,6 +28,7 @@ #include "cx/linked_list.h" #include "cx/utils.h" +#include "cx/compare.h" #include <string.h> #include <assert.h> @@ -915,11 +916,12 @@ list->base.cl = &cx_linked_list_class; list->base.allocator = allocator; - list->base.cmpfunc = comparator; if (item_size > 0) { list->base.item_size = item_size; + list->base.cmpfunc = comparator; } else { + list->base.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator; cxListStorePointers((CxList *) list); }
--- a/tests/test_list.cpp Mon Dec 18 16:04:21 2023 +0100 +++ b/tests/test_list.cpp Mon Dec 18 16:14:07 2023 +0100 @@ -1021,7 +1021,7 @@ CxList *list = autofree(cxLinkedListCreateSimple(CX_STORE_POINTERS)); ASSERT_NE(list, nullptr); EXPECT_EQ(list->item_size, sizeof(void *)); - EXPECT_EQ(list->cmpfunc, nullptr); + EXPECT_EQ(list->cmpfunc, cx_cmp_ptr); EXPECT_EQ(list->allocator, cxDefaultAllocator); EXPECT_EQ(list->simple_destructor, nullptr); EXPECT_EQ(list->advanced_destructor, nullptr); @@ -1059,7 +1059,7 @@ TEST_F(PointerArrayList, cxArrayListCreateSimpleForPointers) { CxList *list = autofree(cxArrayListCreateSimple(CX_STORE_POINTERS, 8)); ASSERT_NE(list, nullptr); - EXPECT_EQ(list->cmpfunc, nullptr); + EXPECT_EQ(list->cmpfunc, cx_cmp_ptr); EXPECT_EQ(list->allocator, cxDefaultAllocator); EXPECT_EQ(list->item_size, sizeof(void *)); EXPECT_TRUE(cxListIsStoringPointers(list));