src/list.c

changeset 1480
83146195a1db
parent 1479
ac1baaed2fd7
--- a/src/list.c	Sat Nov 08 23:45:19 2025 +0100
+++ b/src/list.c	Sun Nov 09 16:12:07 2025 +0100
@@ -867,6 +867,11 @@
         return 1;
     }
 
+    // set the sorted flag when we know it's sorted
+    if (orig_size == 0 && src->collection.sorted) {
+        dst->collection.sorted = true;
+    }
+
     return 0;
 }
 
@@ -1028,37 +1033,31 @@
                 other_elem = cxIteratorCurrent(other_iter);
                 d = src->collection.cmpfunc(src_elem, other_elem);
             }
-            if (d <= 0) {
-                // source element is smaller or equal, clone it
-                void **dst_mem = cxListEmplace(dst);
-                void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem;
-                void* dst_ptr = clone_func(target, src_elem, clone_allocator, data);
-                if (dst_ptr == NULL) {
-                    cx_list_pop_uninitialized_elements(dst, 1);
-                    return 1;
-                }
-                if (cxCollectionStoresPointers(dst)) {
-                    *dst_mem = dst_ptr;
-                }
+            void *clone_from;
+            if (d < 0) {
+                // source element is smaller clone it
+                clone_from = src_elem;
                 cxIteratorNext(src_iter);
-                // if the other element was equal, skip it
-                if (d == 0) {
-                    cxIteratorNext(other_iter);
-                }
+            } else if (d == 0) {
+                // both elements are equal, clone from the source, skip other
+                clone_from = src_elem;
+                cxIteratorNext(src_iter);
+                cxIteratorNext(other_iter);
             } else {
                 // the other element is smaller, clone it
-                void **dst_mem = cxListEmplace(dst);
-                void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem;
-                void* dst_ptr = clone_func(target, other_elem, clone_allocator, data);
-                if (dst_ptr == NULL) {
-                    cx_list_pop_uninitialized_elements(dst, 1);
-                    return 1;
-                }
-                if (cxCollectionStoresPointers(dst)) {
-                    *dst_mem = dst_ptr;
-                }
+                clone_from = other_elem;
                 cxIteratorNext(other_iter);
             }
+            void **dst_mem = cxListEmplace(dst);
+            void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem;
+            void* dst_ptr = clone_func(target, clone_from, clone_allocator, data);
+            if (dst_ptr == NULL) {
+                cx_list_pop_uninitialized_elements(dst, 1);
+                return 1;
+            }
+            if (cxCollectionStoresPointers(dst)) {
+                *dst_mem = dst_ptr;
+            }
         }
 
         // if dst was empty, it is now guaranteed to be sorted

mercurial