Fri, 18 Nov 2022 22:32:17 +0100
#219 array list: add iterator
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:55:14 2022 +0100 1.2 +++ b/src/array_list.c Fri Nov 18 22:32:17 2022 +0100 1.3 @@ -263,12 +263,39 @@ 1.4 1.5 } 1.6 1.7 +static bool cx_arl_iter_valid(struct cx_iterator_s const *iter) { 1.8 + struct cx_list_s const *list = iter->src_handle; 1.9 + return iter->index < list->size; 1.10 +} 1.11 + 1.12 +static void *cx_arl_iter_current(struct cx_iterator_s const *iter) { 1.13 + return iter->elem_handle; 1.14 +} 1.15 + 1.16 +static void cx_arl_iter_next(struct cx_iterator_s *iter) { 1.17 + if (iter->remove) { 1.18 + iter->remove = false; 1.19 + cx_arl_remove(iter->src_handle, iter->index); 1.20 + } else { 1.21 + iter->index++; 1.22 + iter->elem_handle = cx_arl_at(iter->src_handle, iter->index); 1.23 + } 1.24 +} 1.25 + 1.26 static struct cx_iterator_s cx_arl_iterator( 1.27 struct cx_list_s *list, 1.28 size_t index 1.29 ) { 1.30 struct cx_iterator_s iter; 1.31 1.32 + iter.index = index; 1.33 + iter.src_handle = list; 1.34 + iter.elem_handle = cx_arl_at(list, index); 1.35 + iter.valid = cx_arl_iter_valid; 1.36 + iter.current = cx_arl_iter_current; 1.37 + iter.next = cx_arl_iter_next; 1.38 + iter.remove = false; 1.39 + 1.40 return iter; 1.41 } 1.42
2.1 --- a/test/test_list.cpp Thu Nov 17 18:55:14 2022 +0100 2.2 +++ b/test/test_list.cpp Fri Nov 18 22:32:17 2022 +0100 2.3 @@ -900,7 +900,6 @@ 2.4 } 2.5 2.6 TEST_F(ArrayList, Iterator) { 2.7 - ASSERT_EQ(1,0); // TODO: remove when implemented 2.8 verifyIterator(arrayListFromTestData()); 2.9 } 2.10