src/list.c

changeset 655
7340c4255f1f
parent 647
2e6e9d9f2159
child 664
af5bf4603a5d
--- a/src/list.c	Wed Feb 08 20:26:26 2023 +0100
+++ b/src/list.c	Wed Feb 15 16:48:11 2023 +0100
@@ -149,9 +149,10 @@
 
 static struct cx_iterator_s cx_pl_iterator(
         struct cx_list_s const *list,
-        size_t index
+        size_t index,
+        bool backwards
 ) {
-    struct cx_iterator_s iter = list->climpl->iterator(list, index);
+    struct cx_iterator_s iter = list->climpl->iterator(list, index, backwards);
     iter.base.current_impl = iter.base.current;
     iter.base.current = cx_pl_iter_current;
     return iter;
@@ -194,14 +195,14 @@
 void cxListDestroy(CxList *list) {
     switch (list->content_destructor_type) {
         case CX_DESTRUCTOR_SIMPLE: {
-            CxIterator iter = cxListBegin(list);
+            CxIterator iter = cxListIterator(list);
             cx_foreach(void*, elem, iter) {
                 list->simple_destructor(elem);
             }
             break;
         }
         case CX_DESTRUCTOR_ADVANCED: {
-            CxIterator iter = cxListBegin(list);
+            CxIterator iter = cxListIterator(list);
             cx_foreach(void*, elem, iter) {
                 list->advanced_destructor.func(list->advanced_destructor.data, elem);
             }
@@ -225,8 +226,8 @@
     } else {
         // different compare functions, use iterator
         if (list->size == other->size) {
-            CxIterator left = cxListBegin(list);
-            CxIterator right = cxListBegin(other);
+            CxIterator left = cxListIterator(list);
+            CxIterator right = cxListIterator(other);
             for (size_t i = 0; i < list->size; i++) {
                 void *leftValue = cxIteratorCurrent(left);
                 void *rightValue = cxIteratorCurrent(right);
@@ -244,11 +245,11 @@
     }
 }
 
-CxMutIterator cxListMutIterator(
+CxMutIterator cxListMutIteratorAt(
         CxList *list,
         size_t index
 ) {
-    CxIterator it = list->cl->iterator(list, index);
+    CxIterator it = list->cl->iterator(list, index, false);
     it.base.mutating = true;
 
     // we know the iterators share the same memory layout
@@ -256,3 +257,16 @@
     memcpy(&iter, &it, sizeof(CxMutIterator));
     return iter;
 }
+
+CxMutIterator cxListMutBackwardsIteratorAt(
+        CxList *list,
+        size_t index
+) {
+    CxIterator it = list->cl->iterator(list, index, true);
+    it.base.mutating = true;
+
+    // we know the iterators share the same memory layout
+    CxMutIterator iter;
+    memcpy(&iter, &it, sizeof(CxMutIterator));
+    return iter;
+}

mercurial