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 |
1.1 --- a/src/array_list.c Thu Nov 17 18:29:59 2022 +0100 1.2 +++ b/src/array_list.c Thu Nov 17 18:32:59 2022 +0100 1.3 @@ -190,7 +190,29 @@ 1.4 struct cx_list_s *list, 1.5 size_t index 1.6 ) { 1.7 - return 1; 1.8 + /* out-of-bounds check */ 1.9 + if (index >= list->size) { 1.10 + return 1; 1.11 + } 1.12 + 1.13 + cx_array_list *arl = (cx_array_list *) list; 1.14 + 1.15 + /* just move the elements starting at index to the left */ 1.16 + int result = cx_array_copy( 1.17 + &arl->data, 1.18 + &list->size, 1.19 + &list->capacity, 1.20 + index, 1.21 + ((char *) arl->data) + (index + 1) * list->itemsize, 1.22 + list->itemsize, 1.23 + list->size - index, 1.24 + &arl->reallocator 1.25 + ); 1.26 + if (result == 0) { 1.27 + /* decrease the size */ 1.28 + list->size--; 1.29 + } 1.30 + return result; 1.31 } 1.32 1.33 static void *cx_arl_at(
2.1 --- a/test/test_list.cpp Thu Nov 17 18:29:59 2022 +0100 2.2 +++ b/test/test_list.cpp Thu Nov 17 18:32:59 2022 +0100 2.3 @@ -852,7 +852,6 @@ 2.4 } 2.5 2.6 TEST_F(ArrayList, cxListRemove) { 2.7 - ASSERT_EQ(1,0); // TODO: remove when implemented 2.8 verifyRemove(arrayListFromTestData()); 2.9 } 2.10