#219 array list: implement insert via iterator

Sun, 20 Nov 2022 16:21:03 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 20 Nov 2022 16:21:03 +0100
changeset 619
5e58187ac707
parent 618
1f5a8f6f3015
child 620
f220695aded6

#219 array list: implement insert via iterator

src/array_list.c file | annotate | diff | comparison | revisions
test/test_list.cpp file | annotate | diff | comparison | revisions
--- a/src/array_list.c	Sun Nov 20 15:51:02 2022 +0100
+++ b/src/array_list.c	Sun Nov 20 16:21:03 2022 +0100
@@ -183,7 +183,23 @@
         void const *elem,
         int prepend
 ) {
-    return 1;
+    struct cx_list_s *list = iter->src_handle;
+    if (iter->index < list->size) {
+        int result = cx_arl_insert(
+                list,
+                iter->index + 1 - prepend,
+                elem
+        );
+        if (result == 0 && prepend != 0) {
+            iter->index++;
+            iter->elem_handle = ((char *) iter->elem_handle) + list->itemsize;
+        }
+        return result;
+    } else {
+        int result = cx_arl_add(list, elem);
+        iter->index = list->size;
+        return result;
+    }
 }
 
 static int cx_arl_remove(
--- a/test/test_list.cpp	Sun Nov 20 15:51:02 2022 +0100
+++ b/test/test_list.cpp	Sun Nov 20 16:21:03 2022 +0100
@@ -917,7 +917,6 @@
 }
 
 TEST_F(ArrayList, InsertViaIterator) {
-    ASSERT_EQ(1,0); // TODO: remove when implemented
     int fivenums[] = {0, 1, 2, 3, 4, 5};
     CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4));
     // TODO: replace with cxListAddArray

mercurial