# HG changeset patch # User Mike Becker # Date 1668955862 -3600 # Node ID 1f5a8f6f3015b24713f281be79a8b8d83bc31d39 # Parent cec11387c1bee452c8bc8177de85178fbe570a6e #219 array list: implement compare diff -r cec11387c1be -r 1f5a8f6f3015 src/cx/list.h --- 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. diff -r cec11387c1be -r 1f5a8f6f3015 src/list.c --- 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; + } + } +} diff -r cec11387c1be -r 1f5a8f6f3015 test/test_list.cpp --- 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);