# HG changeset patch # User Mike Becker # Date 1702912447 -3600 # Node ID 741a2040fa332c7d00fac40d0c5d09082b151258 # Parent 4523f6d425129c8f1ca0c8ca055520a5e7320922 make cx_cmp_ptr default comparator for pointer lists - relates to #340 diff -r 4523f6d42512 -r 741a2040fa33 src/array_list.c --- 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 #include @@ -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); } diff -r 4523f6d42512 -r 741a2040fa33 src/cx/array_list.h --- 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 diff -r 4523f6d42512 -r 741a2040fa33 src/cx/linked_list.h --- 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 diff -r 4523f6d42512 -r 741a2040fa33 src/linked_list.c --- 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 #include @@ -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); } diff -r 4523f6d42512 -r 741a2040fa33 tests/test_list.cpp --- 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));