src/linked_list.c

changeset 677
b09aae58bba4
parent 670
4ad8ea3aee49
child 699
35b2b99ee523
     1.1 --- a/src/linked_list.c	Fri Apr 07 11:30:28 2023 +0200
     1.2 +++ b/src/linked_list.c	Sun Apr 09 19:03:58 2023 +0200
     1.3 @@ -60,7 +60,7 @@
     1.4          void const *start,
     1.5          ptrdiff_t loc_advance,
     1.6          ptrdiff_t loc_data,
     1.7 -        CxListComparator cmp_func,
     1.8 +        cx_compare_func cmp_func,
     1.9          void const *elem
    1.10  ) {
    1.11      assert(start != NULL);
    1.12 @@ -291,7 +291,7 @@
    1.13          void *ls,
    1.14          void *le,
    1.15          void *re,
    1.16 -        CxListComparator cmp_func
    1.17 +        cx_compare_func cmp_func
    1.18  ) {
    1.19      void *sbo[CX_LINKED_LIST_SORT_SBO_SIZE];
    1.20      void **sorted = length >= CX_LINKED_LIST_SORT_SBO_SIZE ?
    1.21 @@ -343,7 +343,7 @@
    1.22          ptrdiff_t loc_prev,
    1.23          ptrdiff_t loc_next,
    1.24          ptrdiff_t loc_data,
    1.25 -        CxListComparator cmp_func
    1.26 +        cx_compare_func cmp_func
    1.27  ) {
    1.28      assert(begin != NULL);
    1.29      assert(loc_next >= 0);
    1.30 @@ -403,7 +403,7 @@
    1.31          void const *begin_right,
    1.32          ptrdiff_t loc_advance,
    1.33          ptrdiff_t loc_data,
    1.34 -        CxListComparator cmp_func
    1.35 +        cx_compare_func cmp_func
    1.36  ) {
    1.37      void const *left = begin_left, *right = begin_right;
    1.38  
    1.39 @@ -494,14 +494,14 @@
    1.40  
    1.41      // create the new new_node
    1.42      cx_linked_list_node *new_node = cxMalloc(list->allocator,
    1.43 -                                             sizeof(cx_linked_list_node) + list->itemsize);
    1.44 +                                             sizeof(cx_linked_list_node) + list->item_size);
    1.45  
    1.46      // sortir if failed
    1.47      if (new_node == NULL) return 1;
    1.48  
    1.49      // initialize new new_node
    1.50      new_node->prev = new_node->next = NULL;
    1.51 -    memcpy(new_node->payload, elem, list->itemsize);
    1.52 +    memcpy(new_node->payload, elem, list->item_size);
    1.53  
    1.54      // insert
    1.55      cx_linked_list *ll = (cx_linked_list *) list;
    1.56 @@ -542,7 +542,7 @@
    1.57      // we can add the remaining nodes and immedately advance to the inserted node
    1.58      char const *source = array;
    1.59      for (size_t i = 1; i < n; i++) {
    1.60 -        source += list->itemsize;
    1.61 +        source += list->item_size;
    1.62          if (0 != cx_ll_insert_at(list, node, source)) {
    1.63              return i;
    1.64          }
    1.65 @@ -570,9 +570,7 @@
    1.66      if (node == NULL) return 1;
    1.67  
    1.68      // element destruction
    1.69 -    if (list->content_destructor_type != CX_DESTRUCTOR_NONE) {
    1.70 -        cx_list_invoke_destructor(list, node->payload);
    1.71 -    }
    1.72 +    cx_invoke_destructor(list, node->payload);
    1.73  
    1.74      // remove
    1.75      cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
    1.76 @@ -592,38 +590,12 @@
    1.77  
    1.78      cx_linked_list *ll = (cx_linked_list *) list;
    1.79      cx_linked_list_node *node = ll->begin;
    1.80 -
    1.81 -    // looks super redundant, but avoids repeatedly checking
    1.82 -    // the destructor type for each element
    1.83 -    switch (list->content_destructor_type) {
    1.84 -        case CX_DESTRUCTOR_SIMPLE: {
    1.85 -            while (node != NULL) {
    1.86 -                cx_list_invoke_simple_destructor(list, node->payload);
    1.87 -                cx_linked_list_node *next = node->next;
    1.88 -                cxFree(list->allocator, node);
    1.89 -                node = next;
    1.90 -            }
    1.91 -            break;
    1.92 -        }
    1.93 -        case CX_DESTRUCTOR_ADVANCED: {
    1.94 -            while (node != NULL) {
    1.95 -                cx_list_invoke_advanced_destructor(list, node->payload);
    1.96 -                cx_linked_list_node *next = node->next;
    1.97 -                cxFree(list->allocator, node);
    1.98 -                node = next;
    1.99 -            }
   1.100 -            break;
   1.101 -        }
   1.102 -        case CX_DESTRUCTOR_NONE: {
   1.103 -            while (node != NULL) {
   1.104 -                cx_linked_list_node *next = node->next;
   1.105 -                cxFree(list->allocator, node);
   1.106 -                node = next;
   1.107 -            }
   1.108 -            break;
   1.109 -        }
   1.110 +    while (node != NULL) {
   1.111 +        cx_invoke_destructor(list, node->payload);
   1.112 +        cx_linked_list_node *next = node->next;
   1.113 +        cxFree(list->allocator, node);
   1.114 +        node = next;
   1.115      }
   1.116 -
   1.117      ll->begin = ll->end = NULL;
   1.118      list->size = 0;
   1.119  }
   1.120 @@ -708,7 +680,7 @@
   1.121          }
   1.122      }
   1.123  
   1.124 -    if (list->itemsize > CX_LINKED_LIST_SWAP_SBO_SIZE || CX_DISABLE_LINKED_LIST_SWAP_SBO) {
   1.125 +    if (list->item_size > CX_LINKED_LIST_SWAP_SBO_SIZE || CX_DISABLE_LINKED_LIST_SWAP_SBO) {
   1.126          cx_linked_list_node *prev = nleft->prev;
   1.127          cx_linked_list_node *next = nright->next;
   1.128          cx_linked_list_node *midstart = nleft->next;
   1.129 @@ -740,9 +712,9 @@
   1.130      } else {
   1.131          // swap payloads to avoid relinking
   1.132          char buf[CX_LINKED_LIST_SWAP_SBO_SIZE];
   1.133 -        memcpy(buf, nleft->payload, list->itemsize);
   1.134 -        memcpy(nleft->payload, nright->payload, list->itemsize);
   1.135 -        memcpy(nright->payload, buf, list->itemsize);
   1.136 +        memcpy(buf, nleft->payload, list->item_size);
   1.137 +        memcpy(nleft->payload, nright->payload, list->item_size);
   1.138 +        memcpy(nright->payload, buf, list->item_size);
   1.139      }
   1.140  
   1.141      return 0;
   1.142 @@ -803,9 +775,7 @@
   1.143          cx_linked_list *ll = iter->src_handle;
   1.144          cx_linked_list_node *node = iter->elem_handle;
   1.145          iter->elem_handle = node->next;
   1.146 -        if (list->content_destructor_type != CX_DESTRUCTOR_NONE) {
   1.147 -            cx_list_invoke_destructor(list, node->payload);
   1.148 -        }
   1.149 +        cx_invoke_destructor(list, node->payload);
   1.150          cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   1.151                                CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   1.152          list->size--;
   1.153 @@ -828,9 +798,7 @@
   1.154          cx_linked_list_node *node = iter->elem_handle;
   1.155          iter->elem_handle = node->prev;
   1.156          iter->index--;
   1.157 -        if (list->content_destructor_type != CX_DESTRUCTOR_NONE) {
   1.158 -            cx_list_invoke_destructor(list, node->payload);
   1.159 -        }
   1.160 +        cx_invoke_destructor(list, node->payload);
   1.161          cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   1.162                                CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   1.163          list->size--;
   1.164 @@ -927,7 +895,7 @@
   1.165  
   1.166  CxList *cxLinkedListCreate(
   1.167          CxAllocator const *allocator,
   1.168 -        CxListComparator comparator,
   1.169 +        cx_compare_func comparator,
   1.170          size_t item_size
   1.171  ) {
   1.172      if (allocator == NULL) {
   1.173 @@ -940,10 +908,9 @@
   1.174      list->base.cl = &cx_linked_list_class;
   1.175      list->base.allocator = allocator;
   1.176      list->base.cmpfunc = comparator;
   1.177 -    list->base.capacity = SIZE_MAX;
   1.178  
   1.179      if (item_size > 0) {
   1.180 -        list->base.itemsize = item_size;
   1.181 +        list->base.item_size = item_size;
   1.182      } else {
   1.183          cxListStorePointers((CxList *) list);
   1.184      }

mercurial