src/cx/list.h

changeset 655
7340c4255f1f
parent 647
2e6e9d9f2159
child 664
af5bf4603a5d
     1.1 --- a/src/cx/list.h	Wed Feb 08 20:26:26 2023 +0100
     1.2 +++ b/src/cx/list.h	Wed Feb 15 16:48:11 2023 +0100
     1.3 @@ -211,7 +211,8 @@
     1.4       */
     1.5      struct cx_iterator_s (*iterator)(
     1.6              struct cx_list_s const *list,
     1.7 -            size_t index
     1.8 +            size_t index,
     1.9 +            bool backward
    1.10      );
    1.11  };
    1.12  
    1.13 @@ -456,11 +457,30 @@
    1.14   * @return a new iterator
    1.15   */
    1.16  __attribute__((__nonnull__, __warn_unused_result__))
    1.17 -static inline CxIterator cxListIterator(
    1.18 +static inline CxIterator cxListIteratorAt(
    1.19          CxList const *list,
    1.20          size_t index
    1.21  ) {
    1.22 -    return list->cl->iterator(list, index);
    1.23 +    return list->cl->iterator(list, index, false);
    1.24 +}
    1.25 +
    1.26 +/**
    1.27 + * Returns a backwards iterator pointing to the item at the specified index.
    1.28 + *
    1.29 + * The returned iterator is position-aware.
    1.30 + *
    1.31 + * If the index is out of range, a past-the-end iterator will be returned.
    1.32 + *
    1.33 + * @param list the list
    1.34 + * @param index the index where the iterator shall point at
    1.35 + * @return a new iterator
    1.36 + */
    1.37 +__attribute__((__nonnull__, __warn_unused_result__))
    1.38 +static inline CxIterator cxListBackwardsIteratorAt(
    1.39 +        CxList const *list,
    1.40 +        size_t index
    1.41 +) {
    1.42 +    return list->cl->iterator(list, index, true);
    1.43  }
    1.44  
    1.45  /**
    1.46 @@ -475,7 +495,25 @@
    1.47   * @return a new iterator
    1.48   */
    1.49  __attribute__((__nonnull__, __warn_unused_result__))
    1.50 -CxMutIterator cxListMutIterator(
    1.51 +CxMutIterator cxListMutIteratorAt(
    1.52 +        CxList *list,
    1.53 +        size_t index
    1.54 +);
    1.55 +
    1.56 +/**
    1.57 + * Returns a mutating backwards iterator pointing to the item at the
    1.58 + * specified index.
    1.59 + *
    1.60 + * The returned iterator is position-aware.
    1.61 + *
    1.62 + * If the index is out of range, a past-the-end iterator will be returned.
    1.63 + *
    1.64 + * @param list the list
    1.65 + * @param index the index where the iterator shall point at
    1.66 + * @return a new iterator
    1.67 + */
    1.68 +__attribute__((__nonnull__, __warn_unused_result__))
    1.69 +CxMutIterator cxListMutBackwardsIteratorAt(
    1.70          CxList *list,
    1.71          size_t index
    1.72  );
    1.73 @@ -491,8 +529,8 @@
    1.74   * @return a new iterator
    1.75   */
    1.76  __attribute__((__nonnull__, __warn_unused_result__))
    1.77 -static inline CxIterator cxListBegin(CxList const *list) {
    1.78 -    return list->cl->iterator(list, 0);
    1.79 +static inline CxIterator cxListIterator(CxList const *list) {
    1.80 +    return list->cl->iterator(list, 0, false);
    1.81  }
    1.82  
    1.83  /**
    1.84 @@ -506,8 +544,39 @@
    1.85   * @return a new iterator
    1.86   */
    1.87  __attribute__((__nonnull__, __warn_unused_result__))
    1.88 -static inline CxMutIterator cxListBeginMut(CxList *list) {
    1.89 -    return cxListMutIterator(list, 0);
    1.90 +static inline CxMutIterator cxListMutIterator(CxList *list) {
    1.91 +    return cxListMutIteratorAt(list, 0);
    1.92 +}
    1.93 +
    1.94 +
    1.95 +/**
    1.96 + * Returns a backwards iterator pointing to the last item of the list.
    1.97 + *
    1.98 + * The returned iterator is position-aware.
    1.99 + *
   1.100 + * If the list is empty, a past-the-end iterator will be returned.
   1.101 + *
   1.102 + * @param list the list
   1.103 + * @return a new iterator
   1.104 + */
   1.105 +__attribute__((__nonnull__, __warn_unused_result__))
   1.106 +static inline CxIterator cxListBackwardsIterator(CxList const *list) {
   1.107 +    return list->cl->iterator(list, list->size - 1, true);
   1.108 +}
   1.109 +
   1.110 +/**
   1.111 + * Returns a mutating backwards iterator pointing to the last item of the list.
   1.112 + *
   1.113 + * The returned iterator is position-aware.
   1.114 + *
   1.115 + * If the list is empty, a past-the-end iterator will be returned.
   1.116 + *
   1.117 + * @param list the list
   1.118 + * @return a new iterator
   1.119 + */
   1.120 +__attribute__((__nonnull__, __warn_unused_result__))
   1.121 +static inline CxMutIterator cxListMutBackwardsIterator(CxList *list) {
   1.122 +    return cxListMutBackwardsIteratorAt(list, list->size - 1);
   1.123  }
   1.124  
   1.125  /**

mercurial