#219 array list: implement remove

Thu, 17 Nov 2022 18:32:59 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 17 Nov 2022 18:32:59 +0100
changeset 613
85c08391a090
parent 612
820ee59121b4
child 614
7aaec630cf15

#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(
--- a/test/test_list.cpp	Thu Nov 17 18:29:59 2022 +0100
+++ b/test/test_list.cpp	Thu Nov 17 18:32:59 2022 +0100
@@ -852,7 +852,6 @@
 }
 
 TEST_F(ArrayList, cxListRemove) {
-    ASSERT_EQ(1,0); // TODO: remove when implemented
     verifyRemove(arrayListFromTestData());
 }
 

mercurial