Thu, 17 Nov 2022 18:32:59 +0100
#219 array list: implement remove
src/array_list.c | file | annotate | diff | comparison | revisions | |
test/test_list.cpp | file | annotate | diff | comparison | revisions |
--- a/src/array_list.c Thu Nov 17 18:29:59 2022 +0100 +++ b/src/array_list.c Thu Nov 17 18:32:59 2022 +0100 @@ -190,7 +190,29 @@ struct cx_list_s *list, size_t index ) { - return 1; + /* out-of-bounds check */ + if (index >= list->size) { + return 1; + } + + cx_array_list *arl = (cx_array_list *) list; + + /* just move the elements starting at index to the left */ + int result = cx_array_copy( + &arl->data, + &list->size, + &list->capacity, + index, + ((char *) arl->data) + (index + 1) * list->itemsize, + list->itemsize, + list->size - index, + &arl->reallocator + ); + if (result == 0) { + /* decrease the size */ + list->size--; + } + return result; } static void *cx_arl_at(