src/cx/list.h

changeset 494
6ce8cfa10a96
parent 490
e66551b47466
child 495
2856c74e18ba
     1.1 --- a/src/cx/list.h	Sat Jan 22 10:29:48 2022 +0100
     1.2 +++ b/src/cx/list.h	Sat Jan 22 17:15:14 2022 +0100
     1.3 @@ -39,6 +39,7 @@
     1.4  
     1.5  #include "common.h"
     1.6  #include "allocator.h"
     1.7 +#include "iterator.h"
     1.8  
     1.9  #ifdef __cplusplus
    1.10  extern "C" {
    1.11 @@ -119,6 +120,14 @@
    1.12       * Member function for reversing the order of the items.
    1.13       */
    1.14      void (*reverse)(cx_list_s *list);
    1.15 +
    1.16 +    /**
    1.17 +     * Returns an iterator pointing to the specified index.
    1.18 +     */
    1.19 +    CxIterator (*iterator)(
    1.20 +            cx_list_s const *list,
    1.21 +            size_t index
    1.22 +    );
    1.23  } cx_list_class;
    1.24  
    1.25  /**
    1.26 @@ -227,6 +236,38 @@
    1.27  }
    1.28  
    1.29  /**
    1.30 + * Returns an iterator pointing to the item at the specified index.
    1.31 + *
    1.32 + * The returned iterator is position-aware.
    1.33 + *
    1.34 + * If the index is out of range, a past-the-end iterator will be returned.
    1.35 + *
    1.36 + * @param list the list
    1.37 + * @param index the index where the iterator shall point at
    1.38 + * @return a new iterator
    1.39 + */
    1.40 +static inline CxIterator cxListIterator(
    1.41 +        CxList list,
    1.42 +        size_t index
    1.43 +) {
    1.44 +    return list->cl->iterator(list, index);
    1.45 +}
    1.46 +
    1.47 +/**
    1.48 + * Returns an iterator pointing to the first item of the list.
    1.49 + *
    1.50 + * The returned iterator is position-aware.
    1.51 + *
    1.52 + * If the list is empty, a past-the-end iterator will be returned.
    1.53 + *
    1.54 + * @param list the list
    1.55 + * @return a new iterator
    1.56 + */
    1.57 +static inline CxIterator cxListBegin(CxList list) {
    1.58 +    return list->cl->iterator(list, 0);
    1.59 +}
    1.60 +
    1.61 +/**
    1.62   * Returns the index of the first element that equals \p elem.
    1.63   *
    1.64   * Determining equality is performed by the list's comparator function.

mercurial