diff -r e3469b497eff -r 6ce8cfa10a96 src/cx/list.h --- a/src/cx/list.h Sat Jan 22 10:29:48 2022 +0100 +++ b/src/cx/list.h Sat Jan 22 17:15:14 2022 +0100 @@ -39,6 +39,7 @@ #include "common.h" #include "allocator.h" +#include "iterator.h" #ifdef __cplusplus extern "C" { @@ -119,6 +120,14 @@ * Member function for reversing the order of the items. */ void (*reverse)(cx_list_s *list); + + /** + * Returns an iterator pointing to the specified index. + */ + CxIterator (*iterator)( + cx_list_s const *list, + size_t index + ); } cx_list_class; /** @@ -227,6 +236,38 @@ } /** + * Returns an iterator pointing to the item at the specified index. + * + * The returned iterator is position-aware. + * + * If the index is out of range, a past-the-end iterator will be returned. + * + * @param list the list + * @param index the index where the iterator shall point at + * @return a new iterator + */ +static inline CxIterator cxListIterator( + CxList list, + size_t index +) { + return list->cl->iterator(list, index); +} + +/** + * Returns an iterator pointing to the first item of the list. + * + * The returned iterator is position-aware. + * + * If the list is empty, a past-the-end iterator will be returned. + * + * @param list the list + * @return a new iterator + */ +static inline CxIterator cxListBegin(CxList list) { + return list->cl->iterator(list, 0); +} + +/** * Returns the index of the first element that equals \p elem. * * Determining equality is performed by the list's comparator function.