#219 array list: implement compare

Sun, 20 Nov 2022 15:51:02 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 20 Nov 2022 15:51:02 +0100
changeset 618
1f5a8f6f3015
parent 617
cec11387c1be
child 619
5e58187ac707

#219 array list: implement compare

src/cx/list.h file | annotate | diff | comparison | revisions
src/list.c file | annotate | diff | comparison | revisions
test/test_list.cpp file | annotate | diff | comparison | revisions
--- a/src/cx/list.h	Sun Nov 20 12:17:34 2022 +0100
+++ b/src/cx/list.h	Sun Nov 20 15:51:02 2022 +0100
@@ -391,19 +391,19 @@
 /**
  * Compares a list to another list of the same type.
  *
- * First, the list sizes are compared. If they match, the lists are compared element-wise.
+ * First, the list sizes are compared.
+ * If they match, the lists are compared element-wise.
  *
  * @param list the list
  * @param other the list to compare to
- * @return zero, if both lists are equal element wise, negative if the first list is smaller, zero if the first list is larger
+ * @return zero, if both lists are equal element wise,
+ * negative if the first list is smaller, positive if the first list is larger
  */
 __attribute__((__nonnull__))
-static inline int cxListCompare(
-        CxList *list,
-        CxList *other
-) {
-    return list->cl->compare(list, other);
-}
+int cxListCompare(
+        CxList const *list,
+        CxList const *other
+);
 
 /**
  * Deallocates the memory of the specified list structure.
--- a/src/list.c	Sun Nov 20 12:17:34 2022 +0100
+++ b/src/list.c	Sun Nov 20 15:51:02 2022 +0100
@@ -51,3 +51,33 @@
     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) {
+            // TODO: we would need a const iterator
+            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;
+        }
+    }
+}
--- a/test/test_list.cpp	Sun Nov 20 12:17:34 2022 +0100
+++ b/test/test_list.cpp	Sun Nov 20 15:51:02 2022 +0100
@@ -951,7 +951,6 @@
 }
 
 TEST_F(LinkedList, cxListCompareWithArrayList) {
-    ASSERT_EQ(1,0); // TODO: remove when implemented
     auto left = linkedListFromTestData();
     auto right = arrayListFromTestData();
     verifyCompare(left, right);
@@ -970,7 +969,6 @@
 }
 
 TEST_F(PointerLinkedList, cxListCompareWithArrayList) {
-    ASSERT_EQ(1,0); // TODO: remove when implemented
     auto left = pointerLinkedListFromTestData();
     auto right = arrayListFromTestData();
     verifyCompare(left, right);
@@ -984,14 +982,12 @@
 }
 
 TEST_F(ArrayList, cxListCompareWithPtrList) {
-    ASSERT_EQ(1,0); // TODO: remove when implemented
     auto left = arrayListFromTestData();
     auto right = pointerLinkedListFromTestData();
     verifyCompare(left, right);
 }
 
 TEST_F(ArrayList, cxListCompareWithNormalList) {
-    ASSERT_EQ(1,0); // TODO: remove when implemented
     auto left = arrayListFromTestData();
     auto right = linkedListFromTestData();
     verifyCompare(left, right);

mercurial