src/list.c

changeset 655
7340c4255f1f
parent 647
2e6e9d9f2159
child 664
af5bf4603a5d
     1.1 --- a/src/list.c	Wed Feb 08 20:26:26 2023 +0100
     1.2 +++ b/src/list.c	Wed Feb 15 16:48:11 2023 +0100
     1.3 @@ -149,9 +149,10 @@
     1.4  
     1.5  static struct cx_iterator_s cx_pl_iterator(
     1.6          struct cx_list_s const *list,
     1.7 -        size_t index
     1.8 +        size_t index,
     1.9 +        bool backwards
    1.10  ) {
    1.11 -    struct cx_iterator_s iter = list->climpl->iterator(list, index);
    1.12 +    struct cx_iterator_s iter = list->climpl->iterator(list, index, backwards);
    1.13      iter.base.current_impl = iter.base.current;
    1.14      iter.base.current = cx_pl_iter_current;
    1.15      return iter;
    1.16 @@ -194,14 +195,14 @@
    1.17  void cxListDestroy(CxList *list) {
    1.18      switch (list->content_destructor_type) {
    1.19          case CX_DESTRUCTOR_SIMPLE: {
    1.20 -            CxIterator iter = cxListBegin(list);
    1.21 +            CxIterator iter = cxListIterator(list);
    1.22              cx_foreach(void*, elem, iter) {
    1.23                  list->simple_destructor(elem);
    1.24              }
    1.25              break;
    1.26          }
    1.27          case CX_DESTRUCTOR_ADVANCED: {
    1.28 -            CxIterator iter = cxListBegin(list);
    1.29 +            CxIterator iter = cxListIterator(list);
    1.30              cx_foreach(void*, elem, iter) {
    1.31                  list->advanced_destructor.func(list->advanced_destructor.data, elem);
    1.32              }
    1.33 @@ -225,8 +226,8 @@
    1.34      } else {
    1.35          // different compare functions, use iterator
    1.36          if (list->size == other->size) {
    1.37 -            CxIterator left = cxListBegin(list);
    1.38 -            CxIterator right = cxListBegin(other);
    1.39 +            CxIterator left = cxListIterator(list);
    1.40 +            CxIterator right = cxListIterator(other);
    1.41              for (size_t i = 0; i < list->size; i++) {
    1.42                  void *leftValue = cxIteratorCurrent(left);
    1.43                  void *rightValue = cxIteratorCurrent(right);
    1.44 @@ -244,11 +245,11 @@
    1.45      }
    1.46  }
    1.47  
    1.48 -CxMutIterator cxListMutIterator(
    1.49 +CxMutIterator cxListMutIteratorAt(
    1.50          CxList *list,
    1.51          size_t index
    1.52  ) {
    1.53 -    CxIterator it = list->cl->iterator(list, index);
    1.54 +    CxIterator it = list->cl->iterator(list, index, false);
    1.55      it.base.mutating = true;
    1.56  
    1.57      // we know the iterators share the same memory layout
    1.58 @@ -256,3 +257,16 @@
    1.59      memcpy(&iter, &it, sizeof(CxMutIterator));
    1.60      return iter;
    1.61  }
    1.62 +
    1.63 +CxMutIterator cxListMutBackwardsIteratorAt(
    1.64 +        CxList *list,
    1.65 +        size_t index
    1.66 +) {
    1.67 +    CxIterator it = list->cl->iterator(list, index, true);
    1.68 +    it.base.mutating = true;
    1.69 +
    1.70 +    // we know the iterators share the same memory layout
    1.71 +    CxMutIterator iter;
    1.72 +    memcpy(&iter, &it, sizeof(CxMutIterator));
    1.73 +    return iter;
    1.74 +}

mercurial