diff -r d50b5dc1e058 -r af5bf4603a5d src/array_list.c --- a/src/array_list.c Sun Mar 05 10:55:32 2023 +0100 +++ b/src/array_list.c Tue Mar 14 20:25:24 2023 +0100 @@ -263,11 +263,20 @@ struct cx_list_s *list, size_t index ) { + cx_array_list *arl = (cx_array_list *) list; + // out-of-bounds check if (index >= list->size) { return 1; } + // content destruction + if (list->content_destructor_type != CX_DESTRUCTOR_NONE) { + char *ptr = arl->data; + ptr += index * list->itemsize; + cx_list_invoke_destructor(list, ptr); + } + // short-circuit removal of last element if (index == list->size - 1) { list->size--; @@ -275,7 +284,6 @@ } // just move the elements starting at index to the left - cx_array_list *arl = (cx_array_list *) list; int result = cx_array_copy( &arl->data, &list->size, @@ -293,6 +301,33 @@ return result; } +static void cx_arl_clear(struct cx_list_s *list) { + if (list->size == 0) return; + + cx_array_list *arl = (cx_array_list *) list; + char *ptr = arl->data; + + switch (list->content_destructor_type) { + case CX_DESTRUCTOR_SIMPLE: { + for (size_t i = 0; i < list->size; i++) { + list->simple_destructor(ptr); + ptr += list->itemsize; + } + break; + } + case CX_DESTRUCTOR_ADVANCED: { + for (size_t i = 0; i < list->size; i++) { + list->advanced_destructor.func(list->advanced_destructor.data, + ptr); + ptr += list->itemsize; + } + break; + } + case CX_DESTRUCTOR_NONE: + break; // nothing + } +} + static int cx_arl_swap( struct cx_list_s *list, size_t i, @@ -451,6 +486,7 @@ cx_arl_insert_array, cx_arl_insert_iter, cx_arl_remove, + cx_arl_clear, cx_arl_swap, cx_arl_at, cx_arl_find,