Sun, 20 Nov 2022 15:51:02 +0100
#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 |
1.1 --- a/src/cx/list.h Sun Nov 20 12:17:34 2022 +0100 1.2 +++ b/src/cx/list.h Sun Nov 20 15:51:02 2022 +0100 1.3 @@ -391,19 +391,19 @@ 1.4 /** 1.5 * Compares a list to another list of the same type. 1.6 * 1.7 - * First, the list sizes are compared. If they match, the lists are compared element-wise. 1.8 + * First, the list sizes are compared. 1.9 + * If they match, the lists are compared element-wise. 1.10 * 1.11 * @param list the list 1.12 * @param other the list to compare to 1.13 - * @return zero, if both lists are equal element wise, negative if the first list is smaller, zero if the first list is larger 1.14 + * @return zero, if both lists are equal element wise, 1.15 + * negative if the first list is smaller, positive if the first list is larger 1.16 */ 1.17 __attribute__((__nonnull__)) 1.18 -static inline int cxListCompare( 1.19 - CxList *list, 1.20 - CxList *other 1.21 -) { 1.22 - return list->cl->compare(list, other); 1.23 -} 1.24 +int cxListCompare( 1.25 + CxList const *list, 1.26 + CxList const *other 1.27 +); 1.28 1.29 /** 1.30 * Deallocates the memory of the specified list structure.
2.1 --- a/src/list.c Sun Nov 20 12:17:34 2022 +0100 2.2 +++ b/src/list.c Sun Nov 20 15:51:02 2022 +0100 2.3 @@ -51,3 +51,33 @@ 2.4 list->cl->destructor(list); 2.5 cxFree(list->allocator, list); 2.6 } 2.7 + 2.8 +int cxListCompare( 2.9 + CxList const *list, 2.10 + CxList const *other 2.11 +) { 2.12 + if (list->cl->compare == other->cl->compare) { 2.13 + /* same compare function, lists are compatible */ 2.14 + return list->cl->compare(list, other); 2.15 + } else { 2.16 + /* different compare functions, use iterator */ 2.17 + if (list->size == other->size) { 2.18 + // TODO: we would need a const iterator 2.19 + CxIterator left = cxListBegin(list); 2.20 + CxIterator right = cxListBegin(other); 2.21 + for (size_t i = 0; i < list->size; i++) { 2.22 + void *leftValue = cxIteratorCurrent(&left); 2.23 + void *rightValue = cxIteratorCurrent(&right); 2.24 + int d = list->cmpfunc(leftValue, rightValue); 2.25 + if (d != 0) { 2.26 + return d; 2.27 + } 2.28 + cxIteratorNext(&left); 2.29 + cxIteratorNext(&right); 2.30 + } 2.31 + return 0; 2.32 + } else { 2.33 + return list->size < other->size ? -1 : 1; 2.34 + } 2.35 + } 2.36 +}
3.1 --- a/test/test_list.cpp Sun Nov 20 12:17:34 2022 +0100 3.2 +++ b/test/test_list.cpp Sun Nov 20 15:51:02 2022 +0100 3.3 @@ -951,7 +951,6 @@ 3.4 } 3.5 3.6 TEST_F(LinkedList, cxListCompareWithArrayList) { 3.7 - ASSERT_EQ(1,0); // TODO: remove when implemented 3.8 auto left = linkedListFromTestData(); 3.9 auto right = arrayListFromTestData(); 3.10 verifyCompare(left, right); 3.11 @@ -970,7 +969,6 @@ 3.12 } 3.13 3.14 TEST_F(PointerLinkedList, cxListCompareWithArrayList) { 3.15 - ASSERT_EQ(1,0); // TODO: remove when implemented 3.16 auto left = pointerLinkedListFromTestData(); 3.17 auto right = arrayListFromTestData(); 3.18 verifyCompare(left, right); 3.19 @@ -984,14 +982,12 @@ 3.20 } 3.21 3.22 TEST_F(ArrayList, cxListCompareWithPtrList) { 3.23 - ASSERT_EQ(1,0); // TODO: remove when implemented 3.24 auto left = arrayListFromTestData(); 3.25 auto right = pointerLinkedListFromTestData(); 3.26 verifyCompare(left, right); 3.27 } 3.28 3.29 TEST_F(ArrayList, cxListCompareWithNormalList) { 3.30 - ASSERT_EQ(1,0); // TODO: remove when implemented 3.31 auto left = arrayListFromTestData(); 3.32 auto right = linkedListFromTestData(); 3.33 verifyCompare(left, right);