make cx_cmp_ptr default comparator for pointer lists - relates to #340

Mon, 18 Dec 2023 16:14:07 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Dec 2023 16:14:07 +0100
changeset 763
741a2040fa33
parent 762
4523f6d42512
child 764
ccbdbd088455

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
     1.1 --- a/src/array_list.c	Mon Dec 18 16:04:21 2023 +0100
     1.2 +++ b/src/array_list.c	Mon Dec 18 16:14:07 2023 +0100
     1.3 @@ -27,6 +27,7 @@
     1.4   */
     1.5  
     1.6  #include "cx/array_list.h"
     1.7 +#include "cx/compare.h"
     1.8  #include <assert.h>
     1.9  #include <string.h>
    1.10  
    1.11 @@ -522,13 +523,14 @@
    1.12  
    1.13      list->base.cl = &cx_array_list_class;
    1.14      list->base.allocator = allocator;
    1.15 -    list->base.cmpfunc = comparator;
    1.16      list->capacity = initial_capacity;
    1.17  
    1.18      if (item_size > 0) {
    1.19          list->base.item_size = item_size;
    1.20 +        list->base.cmpfunc = comparator;
    1.21      } else {
    1.22          item_size = sizeof(void *);
    1.23 +        list->base.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator;
    1.24          cxListStorePointers((CxList *) list);
    1.25      }
    1.26  
     2.1 --- a/src/cx/array_list.h	Mon Dec 18 16:04:21 2023 +0100
     2.2 +++ b/src/cx/array_list.h	Mon Dec 18 16:14:07 2023 +0100
     2.3 @@ -152,12 +152,14 @@
     2.4   * Allocates an array list for storing elements with \p item_size bytes each.
     2.5   *
     2.6   * If \p item_size is CX_STORE_POINTERS, the created list will be created as if
     2.7 - * cxListStorePointers() was called immediately after creation.
     2.8 + * cxListStorePointers() was called immediately after creation and the compare
     2.9 + * function will be automatically set to cx_cmp_ptr(), if none is given.
    2.10   *
    2.11   * @param allocator the allocator for allocating the list memory
    2.12   * (if \c NULL the cxDefaultAllocator will be used)
    2.13   * @param comparator the comparator for the elements
    2.14 - * (if \c NULL sort and find functions will not work)
    2.15 + * (if \c NULL, and the list is not storing pointers, sort and find
    2.16 + * functions will not work)
    2.17   * @param item_size the size of each element in bytes
    2.18   * @param initial_capacity the initial number of elements the array can store
    2.19   * @return the created list
    2.20 @@ -177,7 +179,8 @@
    2.21   * set it immediately after creation or use cxArrayListCreate().
    2.22   *
    2.23   * If \p item_size is CX_STORE_POINTERS, the created list will be created as if
    2.24 - * cxListStorePointers() was called immediately after creation.
    2.25 + * cxListStorePointers() was called immediately after creation and the compare
    2.26 + * function will be automatically set to cx_cmp_ptr().
    2.27   *
    2.28   * @param item_size the size of each element in bytes
    2.29   * @param initial_capacity the initial number of elements the array can store
     3.1 --- a/src/cx/linked_list.h	Mon Dec 18 16:04:21 2023 +0100
     3.2 +++ b/src/cx/linked_list.h	Mon Dec 18 16:14:07 2023 +0100
     3.3 @@ -54,12 +54,14 @@
     3.4   * Allocates a linked list for storing elements with \p item_size bytes each.
     3.5   *
     3.6   * If \p item_size is CX_STORE_POINTERS, the created list will be created as if
     3.7 - * cxListStorePointers() was called immediately after creation.
     3.8 + * cxListStorePointers() was called immediately after creation and the compare
     3.9 + * function will be automatically set to cx_cmp_ptr(), if none is given.
    3.10   *
    3.11   * @param allocator the allocator for allocating the list nodes
    3.12   * (if \c NULL the cxDefaultAllocator will be used)
    3.13   * @param comparator the comparator for the elements
    3.14 - * (if \c NULL sort and find functions will not work)
    3.15 + * (if \c NULL, and the list is not storing pointers, sort and find
    3.16 + * functions will not work)
    3.17   * @param item_size the size of each element in bytes
    3.18   * @return the created list
    3.19   */
    3.20 @@ -77,7 +79,8 @@
    3.21   * after list creation or use cxLinkedListCreate().
    3.22   *
    3.23   * If \p item_size is CX_STORE_POINTERS, the created list will be created as if
    3.24 - * cxListStorePointers() was called immediately after creation.
    3.25 + * cxListStorePointers() was called immediately after creation and the compare
    3.26 + * function will be automatically set to cx_cmp_ptr().
    3.27   *
    3.28   * @param item_size the size of each element in bytes
    3.29   * @return the created list
     4.1 --- a/src/linked_list.c	Mon Dec 18 16:04:21 2023 +0100
     4.2 +++ b/src/linked_list.c	Mon Dec 18 16:14:07 2023 +0100
     4.3 @@ -28,6 +28,7 @@
     4.4  
     4.5  #include "cx/linked_list.h"
     4.6  #include "cx/utils.h"
     4.7 +#include "cx/compare.h"
     4.8  #include <string.h>
     4.9  #include <assert.h>
    4.10  
    4.11 @@ -915,11 +916,12 @@
    4.12  
    4.13      list->base.cl = &cx_linked_list_class;
    4.14      list->base.allocator = allocator;
    4.15 -    list->base.cmpfunc = comparator;
    4.16  
    4.17      if (item_size > 0) {
    4.18          list->base.item_size = item_size;
    4.19 +        list->base.cmpfunc = comparator;
    4.20      } else {
    4.21 +        list->base.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator;
    4.22          cxListStorePointers((CxList *) list);
    4.23      }
    4.24  
     5.1 --- a/tests/test_list.cpp	Mon Dec 18 16:04:21 2023 +0100
     5.2 +++ b/tests/test_list.cpp	Mon Dec 18 16:14:07 2023 +0100
     5.3 @@ -1021,7 +1021,7 @@
     5.4      CxList *list = autofree(cxLinkedListCreateSimple(CX_STORE_POINTERS));
     5.5      ASSERT_NE(list, nullptr);
     5.6      EXPECT_EQ(list->item_size, sizeof(void *));
     5.7 -    EXPECT_EQ(list->cmpfunc, nullptr);
     5.8 +    EXPECT_EQ(list->cmpfunc, cx_cmp_ptr);
     5.9      EXPECT_EQ(list->allocator, cxDefaultAllocator);
    5.10      EXPECT_EQ(list->simple_destructor, nullptr);
    5.11      EXPECT_EQ(list->advanced_destructor, nullptr);
    5.12 @@ -1059,7 +1059,7 @@
    5.13  TEST_F(PointerArrayList, cxArrayListCreateSimpleForPointers) {
    5.14      CxList *list = autofree(cxArrayListCreateSimple(CX_STORE_POINTERS, 8));
    5.15      ASSERT_NE(list, nullptr);
    5.16 -    EXPECT_EQ(list->cmpfunc, nullptr);
    5.17 +    EXPECT_EQ(list->cmpfunc, cx_cmp_ptr);
    5.18      EXPECT_EQ(list->allocator, cxDefaultAllocator);
    5.19      EXPECT_EQ(list->item_size, sizeof(void *));
    5.20      EXPECT_TRUE(cxListIsStoringPointers(list));

mercurial