src/array_list.c

changeset 664
af5bf4603a5d
parent 662
d0d95740071b
child 666
b5dd654deb3b
     1.1 --- a/src/array_list.c	Sun Mar 05 10:55:32 2023 +0100
     1.2 +++ b/src/array_list.c	Tue Mar 14 20:25:24 2023 +0100
     1.3 @@ -263,11 +263,20 @@
     1.4          struct cx_list_s *list,
     1.5          size_t index
     1.6  ) {
     1.7 +    cx_array_list *arl = (cx_array_list *) list;
     1.8 +
     1.9      // out-of-bounds check
    1.10      if (index >= list->size) {
    1.11          return 1;
    1.12      }
    1.13  
    1.14 +    // content destruction
    1.15 +    if (list->content_destructor_type != CX_DESTRUCTOR_NONE) {
    1.16 +        char *ptr = arl->data;
    1.17 +        ptr += index * list->itemsize;
    1.18 +        cx_list_invoke_destructor(list, ptr);
    1.19 +    }
    1.20 +
    1.21      // short-circuit removal of last element
    1.22      if (index == list->size - 1) {
    1.23          list->size--;
    1.24 @@ -275,7 +284,6 @@
    1.25      }
    1.26  
    1.27      // just move the elements starting at index to the left
    1.28 -    cx_array_list *arl = (cx_array_list *) list;
    1.29      int result = cx_array_copy(
    1.30              &arl->data,
    1.31              &list->size,
    1.32 @@ -293,6 +301,33 @@
    1.33      return result;
    1.34  }
    1.35  
    1.36 +static void cx_arl_clear(struct cx_list_s *list) {
    1.37 +    if (list->size == 0) return;
    1.38 +
    1.39 +    cx_array_list *arl = (cx_array_list *) list;
    1.40 +    char *ptr = arl->data;
    1.41 +
    1.42 +    switch (list->content_destructor_type) {
    1.43 +        case CX_DESTRUCTOR_SIMPLE: {
    1.44 +            for (size_t i = 0; i < list->size; i++) {
    1.45 +                list->simple_destructor(ptr);
    1.46 +                ptr += list->itemsize;
    1.47 +            }
    1.48 +            break;
    1.49 +        }
    1.50 +        case CX_DESTRUCTOR_ADVANCED: {
    1.51 +            for (size_t i = 0; i < list->size; i++) {
    1.52 +                list->advanced_destructor.func(list->advanced_destructor.data,
    1.53 +                                               ptr);
    1.54 +                ptr += list->itemsize;
    1.55 +            }
    1.56 +            break;
    1.57 +        }
    1.58 +        case CX_DESTRUCTOR_NONE:
    1.59 +            break; // nothing
    1.60 +    }
    1.61 +}
    1.62 +
    1.63  static int cx_arl_swap(
    1.64          struct cx_list_s *list,
    1.65          size_t i,
    1.66 @@ -451,6 +486,7 @@
    1.67          cx_arl_insert_array,
    1.68          cx_arl_insert_iter,
    1.69          cx_arl_remove,
    1.70 +        cx_arl_clear,
    1.71          cx_arl_swap,
    1.72          cx_arl_at,
    1.73          cx_arl_find,

mercurial