fix inserting via iterator correctly increases element count

Thu, 29 Aug 2024 20:48:15 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 29 Aug 2024 20:48:15 +0200
changeset 874
cdce47f34d48
parent 872
d607a184925a
child 875
ee84ac776cbc

fix inserting via iterator correctly increases element count

fixes #423

src/array_list.c file | annotate | diff | comparison | revisions
src/linked_list.c file | annotate | diff | comparison | revisions
--- a/src/array_list.c	Tue Aug 20 18:02:39 2024 +0200
+++ b/src/array_list.c	Thu Aug 29 20:48:15 2024 +0200
@@ -284,14 +284,20 @@
                 iter->index + 1 - prepend,
                 elem
         );
-        if (result == 0 && prepend != 0) {
-            iter->index++;
-            iter->elem_handle = ((char *) iter->elem_handle) + list->collection.elem_size;
+        if (result == 0) {
+            iter->elem_count++;
+            if (prepend != 0) {
+                iter->index++;
+                iter->elem_handle = ((char *) iter->elem_handle) + list->collection.elem_size;
+            }
         }
         return result;
     } else {
         int result = cx_arl_insert_element(list, list->collection.size, elem);
-        iter->index = list->collection.size;
+        if (result == 0) {
+            iter->elem_count++;
+            iter->index = list->collection.size;
+        }
         return result;
     }
 }
--- a/src/linked_list.c	Tue Aug 20 18:02:39 2024 +0200
+++ b/src/linked_list.c	Thu Aug 29 20:48:15 2024 +0200
@@ -892,11 +892,19 @@
         assert(prepend >= 0 && prepend <= 1);
         cx_linked_list_node *choice[2] = {node, node->prev};
         int result = cx_ll_insert_at(list, choice[prepend], elem);
-        iter->index += prepend * (0 == result);
+        if (result == 0) {
+            iter->elem_count++;
+            if (prepend) {
+                iter->index++;
+            }
+        }
         return result;
     } else {
         int result = cx_ll_insert_element(list, list->collection.size, elem);
-        iter->index = list->collection.size;
+        if (result == 0) {
+            iter->elem_count++;
+            iter->index = list->collection.size;
+        }
         return result;
     }
 }

mercurial