add simple functions for creating lists

Thu, 23 Feb 2023 22:43:13 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 23 Feb 2023 22:43:13 +0100
changeset 662
d0d95740071b
parent 661
0a71ac9547fd
child 663
d50b5dc1e058

add simple functions for creating lists

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
     1.1 --- a/src/array_list.c	Thu Feb 23 22:27:41 2023 +0100
     1.2 +++ b/src/array_list.c	Thu Feb 23 22:43:13 2023 +0100
     1.3 @@ -460,7 +460,7 @@
     1.4          cx_arl_iterator,
     1.5  };
     1.6  
     1.7 -CxList *cxArrayListCreate(
     1.8 +static CxList *cx_array_list_create(
     1.9          CxAllocator const *allocator,
    1.10          CxListComparator comparator,
    1.11          size_t item_size,
    1.12 @@ -487,3 +487,21 @@
    1.13  
    1.14      return (CxList *) list;
    1.15  }
    1.16 +
    1.17 +CxList *cxArrayListCreate(
    1.18 +        CxAllocator const *allocator,
    1.19 +        CxListComparator comparator,
    1.20 +        size_t item_size,
    1.21 +        size_t initial_capacity
    1.22 +) {
    1.23 +    return cx_array_list_create(allocator, comparator,
    1.24 +                                item_size, initial_capacity);
    1.25 +}
    1.26 +
    1.27 +CxList *cxArrayListCreateSimple(
    1.28 +        size_t item_size,
    1.29 +        size_t initial_capacity
    1.30 +) {
    1.31 +    return cx_array_list_create(cxDefaultAllocator, NULL,
    1.32 +                                item_size, initial_capacity);
    1.33 +}
     2.1 --- a/src/cx/array_list.h	Thu Feb 23 22:27:41 2023 +0100
     2.2 +++ b/src/cx/array_list.h	Thu Feb 23 22:43:13 2023 +0100
     2.3 @@ -165,6 +165,22 @@
     2.4          size_t initial_capacity
     2.5  ) __attribute__((__nonnull__));
     2.6  
     2.7 +/**
     2.8 + * Allocates an array list for storing elements with \p item_size bytes each.
     2.9 + *
    2.10 + * The list will use the cxDefaultAllocator and \em NO compare function.
    2.11 + * If you want to call functions that need a compare function, you have to
    2.12 + * set it immediately after creation or use cxArrayListCreate().
    2.13 + *
    2.14 + * @param item_size the size of each element in bytes
    2.15 + * @param initial_capacity the initial number of elements the array can store
    2.16 + * @return the created list
    2.17 + */
    2.18 +CxList *cxArrayListCreateSimple(
    2.19 +        size_t item_size,
    2.20 +        size_t initial_capacity
    2.21 +);
    2.22 +
    2.23  
    2.24  #ifdef __cplusplus
    2.25  } // extern "C"
     3.1 --- a/src/cx/linked_list.h	Thu Feb 23 22:27:41 2023 +0100
     3.2 +++ b/src/cx/linked_list.h	Thu Feb 23 22:43:13 2023 +0100
     3.3 @@ -54,9 +54,6 @@
     3.4  /**
     3.5   * Allocates a linked list for storing elements with \p item_size bytes each.
     3.6   *
     3.7 - * @remark Elements added to the list are copied, therefore a possible destructor
     3.8 - * MUST NOT free the memory pointed to by its argument.
     3.9 - *
    3.10   * @param allocator the allocator for allocating the list nodes
    3.11   * @param comparator the comparator for the elements
    3.12   * @param item_size the size of each element in bytes
    3.13 @@ -69,6 +66,18 @@
    3.14  ) __attribute__((__nonnull__));
    3.15  
    3.16  /**
    3.17 + * Allocates a linked list for storing elements with \p item_size bytes each.
    3.18 + *
    3.19 + * The list will use cxDefaultAllocator and no comparator function. If you want
    3.20 + * to call functions that need a comparator, you must either set one immediately
    3.21 + * after list creation or use cxLinkedListCreate().
    3.22 + *
    3.23 + * @param item_size the size of each element in bytes
    3.24 + * @return the created list
    3.25 + */
    3.26 +CxList *cxLinkedListCreateSimple(size_t item_size);
    3.27 +
    3.28 +/**
    3.29   * Finds the node at a certain index.
    3.30   *
    3.31   * This function can be used to start at an arbitrary position within the list.
     4.1 --- a/src/linked_list.c	Thu Feb 23 22:27:41 2023 +0100
     4.2 +++ b/src/linked_list.c	Thu Feb 23 22:43:13 2023 +0100
     4.3 @@ -295,7 +295,7 @@
     4.4  ) {
     4.5      void *sbo[CX_LINKED_LIST_SORT_SBO_SIZE];
     4.6      void **sorted = length >= CX_LINKED_LIST_SORT_SBO_SIZE ?
     4.7 -            malloc(sizeof(void *) * length) : sbo;
     4.8 +                    malloc(sizeof(void *) * length) : sbo;
     4.9      if (sorted == NULL) abort();
    4.10      void *rc, *lc;
    4.11  
    4.12 @@ -870,7 +870,7 @@
    4.13          cx_ll_iterator,
    4.14  };
    4.15  
    4.16 -CxList *cxLinkedListCreate(
    4.17 +static CxList *cx_linked_list_create(
    4.18          CxAllocator const *allocator,
    4.19          CxListComparator comparator,
    4.20          size_t item_size
    4.21 @@ -886,3 +886,15 @@
    4.22  
    4.23      return (CxList *) list;
    4.24  }
    4.25 +
    4.26 +CxList *cxLinkedListCreate(
    4.27 +        CxAllocator const *allocator,
    4.28 +        CxListComparator comparator,
    4.29 +        size_t item_size
    4.30 +) {
    4.31 +    return cx_linked_list_create(allocator, comparator, item_size);
    4.32 +}
    4.33 +
    4.34 +CxList *cxLinkedListCreateSimple(size_t item_size) {
    4.35 +    return cx_linked_list_create(cxDefaultAllocator, NULL, item_size);
    4.36 +}
     5.1 --- a/tests/test_list.cpp	Thu Feb 23 22:27:41 2023 +0100
     5.2 +++ b/tests/test_list.cpp	Thu Feb 23 22:43:13 2023 +0100
     5.3 @@ -915,6 +915,15 @@
     5.4      verifyCreate(list);
     5.5  }
     5.6  
     5.7 +TEST_F(LinkedList, cxLinkedListCreateSimple) {
     5.8 +    CxList *list = autofree(cxLinkedListCreateSimple(sizeof(int)));
     5.9 +    ASSERT_NE(list, nullptr);
    5.10 +    EXPECT_EQ(list->itemsize, sizeof(int));
    5.11 +    EXPECT_EQ(list->capacity, (size_t) -1);
    5.12 +    EXPECT_EQ(list->cmpfunc, nullptr);
    5.13 +    EXPECT_EQ(list->allocator, cxDefaultAllocator);
    5.14 +}
    5.15 +
    5.16  TEST_F(ArrayList, cxArrayListCreate) {
    5.17      CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
    5.18      ASSERT_NE(list, nullptr);
    5.19 @@ -923,6 +932,15 @@
    5.20      verifyCreate(list);
    5.21  }
    5.22  
    5.23 +TEST_F(ArrayList, cxArrayListCreateSimple) {
    5.24 +    CxList *list = autofree(cxArrayListCreateSimple(sizeof(int), 8));
    5.25 +    ASSERT_NE(list, nullptr);
    5.26 +    EXPECT_EQ(list->cmpfunc, nullptr);
    5.27 +    EXPECT_EQ(list->allocator, cxDefaultAllocator);
    5.28 +    EXPECT_EQ(list->itemsize, sizeof(int));
    5.29 +    EXPECT_EQ(list->capacity, 8);
    5.30 +}
    5.31 +
    5.32  TEST_F(LinkedList, cxListAdd) {
    5.33      auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
    5.34      verifyAdd(list, false);

mercurial