src/ucx/list.c

changeset 438
22eca559aded
parent 415
d938228c382e
child 490
d218607f5a7e
--- a/src/ucx/list.c	Sun Nov 20 12:43:44 2022 +0100
+++ b/src/ucx/list.c	Sat Nov 26 17:07:08 2022 +0100
@@ -51,3 +51,32 @@
     list->cl->destructor(list);
     cxFree(list->allocator, list);
 }
+
+int cxListCompare(
+        CxList const *list,
+        CxList const *other
+) {
+    if (list->cl->compare == other->cl->compare) {
+        // same compare function, lists are compatible
+        return list->cl->compare(list, other);
+    } else {
+        // different compare functions, use iterator
+        if (list->size == other->size) {
+            CxIterator left = cxListBegin(list);
+            CxIterator right = cxListBegin(other);
+            for (size_t i = 0; i < list->size; i++) {
+                void *leftValue = cxIteratorCurrent(left);
+                void *rightValue = cxIteratorCurrent(right);
+                int d = list->cmpfunc(leftValue, rightValue);
+                if (d != 0) {
+                    return d;
+                }
+                cxIteratorNext(left);
+                cxIteratorNext(right);
+            }
+            return 0;
+        } else {
+            return list->size < other->size ? -1 : 1;
+        }
+    }
+}

mercurial