# HG changeset patch # User Mike Becker # Date 1677188593 -3600 # Node ID d0d95740071b5ccc913ce29409c490c6fb56187e # Parent 0a71ac9547fd0bfc3ed92ffe2a4cbf9f212dcd2c add simple functions for creating lists diff -r 0a71ac9547fd -r d0d95740071b src/array_list.c --- a/src/array_list.c Thu Feb 23 22:27:41 2023 +0100 +++ b/src/array_list.c Thu Feb 23 22:43:13 2023 +0100 @@ -460,7 +460,7 @@ cx_arl_iterator, }; -CxList *cxArrayListCreate( +static CxList *cx_array_list_create( CxAllocator const *allocator, CxListComparator comparator, size_t item_size, @@ -487,3 +487,21 @@ return (CxList *) list; } + +CxList *cxArrayListCreate( + CxAllocator const *allocator, + CxListComparator comparator, + size_t item_size, + size_t initial_capacity +) { + return cx_array_list_create(allocator, comparator, + item_size, initial_capacity); +} + +CxList *cxArrayListCreateSimple( + size_t item_size, + size_t initial_capacity +) { + return cx_array_list_create(cxDefaultAllocator, NULL, + item_size, initial_capacity); +} diff -r 0a71ac9547fd -r d0d95740071b src/cx/array_list.h --- a/src/cx/array_list.h Thu Feb 23 22:27:41 2023 +0100 +++ b/src/cx/array_list.h Thu Feb 23 22:43:13 2023 +0100 @@ -165,6 +165,22 @@ size_t initial_capacity ) __attribute__((__nonnull__)); +/** + * Allocates an array list for storing elements with \p item_size bytes each. + * + * The list will use the cxDefaultAllocator and \em NO compare function. + * If you want to call functions that need a compare function, you have to + * set it immediately after creation or use cxArrayListCreate(). + * + * @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 + */ +CxList *cxArrayListCreateSimple( + size_t item_size, + size_t initial_capacity +); + #ifdef __cplusplus } // extern "C" diff -r 0a71ac9547fd -r d0d95740071b src/cx/linked_list.h --- a/src/cx/linked_list.h Thu Feb 23 22:27:41 2023 +0100 +++ b/src/cx/linked_list.h Thu Feb 23 22:43:13 2023 +0100 @@ -54,9 +54,6 @@ /** * Allocates a linked list for storing elements with \p item_size bytes each. * - * @remark Elements added to the list are copied, therefore a possible destructor - * MUST NOT free the memory pointed to by its argument. - * * @param allocator the allocator for allocating the list nodes * @param comparator the comparator for the elements * @param item_size the size of each element in bytes @@ -69,6 +66,18 @@ ) __attribute__((__nonnull__)); /** + * Allocates a linked list for storing elements with \p item_size bytes each. + * + * The list will use cxDefaultAllocator and no comparator function. If you want + * to call functions that need a comparator, you must either set one immediately + * after list creation or use cxLinkedListCreate(). + * + * @param item_size the size of each element in bytes + * @return the created list + */ +CxList *cxLinkedListCreateSimple(size_t item_size); + +/** * Finds the node at a certain index. * * This function can be used to start at an arbitrary position within the list. diff -r 0a71ac9547fd -r d0d95740071b src/linked_list.c --- a/src/linked_list.c Thu Feb 23 22:27:41 2023 +0100 +++ b/src/linked_list.c Thu Feb 23 22:43:13 2023 +0100 @@ -295,7 +295,7 @@ ) { void *sbo[CX_LINKED_LIST_SORT_SBO_SIZE]; void **sorted = length >= CX_LINKED_LIST_SORT_SBO_SIZE ? - malloc(sizeof(void *) * length) : sbo; + malloc(sizeof(void *) * length) : sbo; if (sorted == NULL) abort(); void *rc, *lc; @@ -870,7 +870,7 @@ cx_ll_iterator, }; -CxList *cxLinkedListCreate( +static CxList *cx_linked_list_create( CxAllocator const *allocator, CxListComparator comparator, size_t item_size @@ -886,3 +886,15 @@ return (CxList *) list; } + +CxList *cxLinkedListCreate( + CxAllocator const *allocator, + CxListComparator comparator, + size_t item_size +) { + return cx_linked_list_create(allocator, comparator, item_size); +} + +CxList *cxLinkedListCreateSimple(size_t item_size) { + return cx_linked_list_create(cxDefaultAllocator, NULL, item_size); +} diff -r 0a71ac9547fd -r d0d95740071b tests/test_list.cpp --- a/tests/test_list.cpp Thu Feb 23 22:27:41 2023 +0100 +++ b/tests/test_list.cpp Thu Feb 23 22:43:13 2023 +0100 @@ -915,6 +915,15 @@ verifyCreate(list); } +TEST_F(LinkedList, cxLinkedListCreateSimple) { + CxList *list = autofree(cxLinkedListCreateSimple(sizeof(int))); + ASSERT_NE(list, nullptr); + EXPECT_EQ(list->itemsize, sizeof(int)); + EXPECT_EQ(list->capacity, (size_t) -1); + EXPECT_EQ(list->cmpfunc, nullptr); + EXPECT_EQ(list->allocator, cxDefaultAllocator); +} + TEST_F(ArrayList, cxArrayListCreate) { CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8)); ASSERT_NE(list, nullptr); @@ -923,6 +932,15 @@ verifyCreate(list); } +TEST_F(ArrayList, cxArrayListCreateSimple) { + CxList *list = autofree(cxArrayListCreateSimple(sizeof(int), 8)); + ASSERT_NE(list, nullptr); + EXPECT_EQ(list->cmpfunc, nullptr); + EXPECT_EQ(list->allocator, cxDefaultAllocator); + EXPECT_EQ(list->itemsize, sizeof(int)); + EXPECT_EQ(list->capacity, 8); +} + TEST_F(LinkedList, cxListAdd) { auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); verifyAdd(list, false);