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 |
--- 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; }