src/array_list.c

changeset 677
b09aae58bba4
parent 676
d0680a23d850
child 678
78f943d76f50
     1.1 --- a/src/array_list.c	Fri Apr 07 11:30:28 2023 +0200
     1.2 +++ b/src/array_list.c	Sun Apr 09 19:03:58 2023 +0200
     1.3 @@ -150,6 +150,7 @@
     1.4  typedef struct {
     1.5      struct cx_list_s base;
     1.6      void *data;
     1.7 +    size_t capacity;
     1.8      struct cx_array_reallocator_s reallocator;
     1.9  } cx_array_list;
    1.10  
    1.11 @@ -186,17 +187,17 @@
    1.12      // do we need to move some elements?
    1.13      if (index < list->size) {
    1.14          char const *first_to_move = (char const *) arl->data;
    1.15 -        first_to_move += index * list->itemsize;
    1.16 +        first_to_move += index * list->item_size;
    1.17          size_t elems_to_move = list->size - index;
    1.18          size_t start_of_moved = index + n;
    1.19  
    1.20          if (CX_ARRAY_COPY_SUCCESS != cx_array_copy(
    1.21                  &arl->data,
    1.22                  &list->size,
    1.23 -                &list->capacity,
    1.24 +                &arl->capacity,
    1.25                  start_of_moved,
    1.26                  first_to_move,
    1.27 -                list->itemsize,
    1.28 +                list->item_size,
    1.29                  elems_to_move,
    1.30                  &arl->reallocator
    1.31          )) {
    1.32 @@ -213,10 +214,10 @@
    1.33      if (CX_ARRAY_COPY_SUCCESS == cx_array_copy(
    1.34              &arl->data,
    1.35              &list->size,
    1.36 -            &list->capacity,
    1.37 +            &arl->capacity,
    1.38              index,
    1.39              array,
    1.40 -            list->itemsize,
    1.41 +            list->item_size,
    1.42              n,
    1.43              &arl->reallocator
    1.44      )) {
    1.45 @@ -249,7 +250,7 @@
    1.46          );
    1.47          if (result == 0 && prepend != 0) {
    1.48              iter->index++;
    1.49 -            iter->elem_handle = ((char *) iter->elem_handle) + list->itemsize;
    1.50 +            iter->elem_handle = ((char *) iter->elem_handle) + list->item_size;
    1.51          }
    1.52          return result;
    1.53      } else {
    1.54 @@ -271,11 +272,7 @@
    1.55      }
    1.56  
    1.57      // content destruction
    1.58 -    if (list->content_destructor_type != CX_DESTRUCTOR_NONE) {
    1.59 -        char *ptr = arl->data;
    1.60 -        ptr += index * list->itemsize;
    1.61 -        cx_list_invoke_destructor(list, ptr);
    1.62 -    }
    1.63 +    cx_invoke_destructor(list, ((char*)arl->data) + index * list->item_size);
    1.64  
    1.65      // short-circuit removal of last element
    1.66      if (index == list->size - 1) {
    1.67 @@ -287,10 +284,10 @@
    1.68      int result = cx_array_copy(
    1.69              &arl->data,
    1.70              &list->size,
    1.71 -            &list->capacity,
    1.72 +            &arl->capacity,
    1.73              index,
    1.74 -            ((char *) arl->data) + (index + 1) * list->itemsize,
    1.75 -            list->itemsize,
    1.76 +            ((char *) arl->data) + (index + 1) * list->item_size,
    1.77 +            list->item_size,
    1.78              list->size - index - 1,
    1.79              &arl->reallocator
    1.80      );
    1.81 @@ -307,26 +304,20 @@
    1.82      cx_array_list *arl = (cx_array_list *) list;
    1.83      char *ptr = arl->data;
    1.84  
    1.85 -    switch (list->content_destructor_type) {
    1.86 -        case CX_DESTRUCTOR_SIMPLE: {
    1.87 -            for (size_t i = 0; i < list->size; i++) {
    1.88 -                cx_list_invoke_simple_destructor(list, ptr);
    1.89 -                ptr += list->itemsize;
    1.90 -            }
    1.91 -            break;
    1.92 +    if (list->simple_destructor) {
    1.93 +        for (size_t i = 0; i < list->size; i++) {
    1.94 +            cx_invoke_simple_destructor(list, ptr);
    1.95 +            ptr += list->item_size;
    1.96          }
    1.97 -        case CX_DESTRUCTOR_ADVANCED: {
    1.98 -            for (size_t i = 0; i < list->size; i++) {
    1.99 -                cx_list_invoke_advanced_destructor(list, ptr);
   1.100 -                ptr += list->itemsize;
   1.101 -            }
   1.102 -            break;
   1.103 +    }
   1.104 +    if (list->advanced_destructor) {
   1.105 +        for (size_t i = 0; i < list->size; i++) {
   1.106 +            cx_invoke_advanced_destructor(list, ptr);
   1.107 +            ptr += list->item_size;
   1.108          }
   1.109 -        case CX_DESTRUCTOR_NONE:
   1.110 -            break; // nothing
   1.111      }
   1.112  
   1.113 -    memset(arl->data, 0, list->size * list->itemsize);
   1.114 +    memset(arl->data, 0, list->size * list->item_size);
   1.115      list->size = 0;
   1.116  }
   1.117  
   1.118 @@ -337,7 +328,7 @@
   1.119  ) {
   1.120      if (i >= list->size || j >= list->size) return 1;
   1.121      cx_array_list *arl = (cx_array_list *) list;
   1.122 -    cx_array_swap(arl->data, list->itemsize, i, j);
   1.123 +    cx_array_swap(arl->data, list->item_size, i, j);
   1.124      return 0;
   1.125  }
   1.126  
   1.127 @@ -348,7 +339,7 @@
   1.128      if (index < list->size) {
   1.129          cx_array_list const *arl = (cx_array_list const *) list;
   1.130          char *space = arl->data;
   1.131 -        return space + index * list->itemsize;
   1.132 +        return space + index * list->item_size;
   1.133      } else {
   1.134          return NULL;
   1.135      }
   1.136 @@ -365,7 +356,7 @@
   1.137          if (0 == list->cmpfunc(elem, cur)) {
   1.138              return i;
   1.139          }
   1.140 -        cur += list->itemsize;
   1.141 +        cur += list->item_size;
   1.142      }
   1.143  
   1.144      return list->size;
   1.145 @@ -375,7 +366,7 @@
   1.146      assert(list->cmpfunc != NULL);
   1.147      qsort(((cx_array_list *) list)->data,
   1.148            list->size,
   1.149 -          list->itemsize,
   1.150 +          list->item_size,
   1.151            list->cmpfunc
   1.152      );
   1.153  }
   1.154 @@ -393,8 +384,8 @@
   1.155              if (d != 0) {
   1.156                  return d;
   1.157              }
   1.158 -            left += list->itemsize;
   1.159 -            right += other->itemsize;
   1.160 +            left += list->item_size;
   1.161 +            right += other->item_size;
   1.162          }
   1.163          return 0;
   1.164      } else {
   1.165 @@ -407,7 +398,7 @@
   1.166      void *data = ((cx_array_list const *) list)->data;
   1.167      size_t half = list->size / 2;
   1.168      for (size_t i = 0; i < half; i++) {
   1.169 -        cx_array_swap(data, list->itemsize, i, list->size - 1 - i);
   1.170 +        cx_array_swap(data, list->item_size, i, list->size - 1 - i);
   1.171      }
   1.172  }
   1.173  
   1.174 @@ -433,7 +424,7 @@
   1.175          iter->index++;
   1.176          iter->elem_handle =
   1.177                  ((char *) iter->elem_handle)
   1.178 -                + ((struct cx_list_s const *) iter->src_handle)->itemsize;
   1.179 +                + ((struct cx_list_s const *) iter->src_handle)->item_size;
   1.180      }
   1.181  }
   1.182  
   1.183 @@ -448,7 +439,7 @@
   1.184      iter->index--;
   1.185      if (iter->index < list->base.size) {
   1.186          iter->elem_handle = ((char *) list->data)
   1.187 -                            + iter->index * list->base.itemsize;
   1.188 +                            + iter->index * list->base.item_size;
   1.189      }
   1.190  }
   1.191  
   1.192 @@ -500,7 +491,7 @@
   1.193  
   1.194  CxList *cxArrayListCreate(
   1.195          CxAllocator const *allocator,
   1.196 -        CxListComparator comparator,
   1.197 +        cx_compare_func comparator,
   1.198          size_t item_size,
   1.199          size_t initial_capacity
   1.200  ) {
   1.201 @@ -514,10 +505,10 @@
   1.202      list->base.cl = &cx_array_list_class;
   1.203      list->base.allocator = allocator;
   1.204      list->base.cmpfunc = comparator;
   1.205 -    list->base.capacity = initial_capacity;
   1.206 +    list->capacity = initial_capacity;
   1.207  
   1.208      if (item_size > 0) {
   1.209 -        list->base.itemsize = item_size;
   1.210 +        list->base.item_size = item_size;
   1.211      } else {
   1.212          item_size = sizeof(void*);
   1.213          cxListStorePointers((CxList *) list);

mercurial