#219 array list: add iterator

Fri, 18 Nov 2022 22:32:17 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 18 Nov 2022 22:32:17 +0100
changeset 616
af7d8a29fbc5
parent 615
b52b66dcd44b
child 617
cec11387c1be

#219 array list: add iterator

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:55:14 2022 +0100
+++ b/src/array_list.c	Fri Nov 18 22:32:17 2022 +0100
@@ -263,12 +263,39 @@
 
 }
 
+static bool cx_arl_iter_valid(struct cx_iterator_s const *iter) {
+    struct cx_list_s const *list = iter->src_handle;
+    return iter->index < list->size;
+}
+
+static void *cx_arl_iter_current(struct cx_iterator_s const *iter) {
+    return iter->elem_handle;
+}
+
+static void cx_arl_iter_next(struct cx_iterator_s *iter) {
+    if (iter->remove) {
+        iter->remove = false;
+        cx_arl_remove(iter->src_handle, iter->index);
+    } else {
+        iter->index++;
+        iter->elem_handle = cx_arl_at(iter->src_handle, iter->index);
+    }
+}
+
 static struct cx_iterator_s cx_arl_iterator(
         struct cx_list_s *list,
         size_t index
 ) {
     struct cx_iterator_s iter;
 
+    iter.index = index;
+    iter.src_handle = list;
+    iter.elem_handle = cx_arl_at(list, index);
+    iter.valid = cx_arl_iter_valid;
+    iter.current = cx_arl_iter_current;
+    iter.next = cx_arl_iter_next;
+    iter.remove = false;
+
     return iter;
 }
 
--- a/test/test_list.cpp	Thu Nov 17 18:55:14 2022 +0100
+++ b/test/test_list.cpp	Fri Nov 18 22:32:17 2022 +0100
@@ -900,7 +900,6 @@
 }
 
 TEST_F(ArrayList, Iterator) {
-    ASSERT_EQ(1,0); // TODO: remove when implemented
     verifyIterator(arrayListFromTestData());
 }
 

mercurial