Skip to content

Commit acd789d

Browse files
committed
It is probably best to let append functions return void, so that it can be a list of indices in future versions.
1 parent 4b10dcf commit acd789d

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

Source/DFPSR/api/algorithmAPI_List.h

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -504,43 +504,38 @@ static inline intptr_t list_insert_unique_sorted_descending(
504504
return list_insert_unique_sorted<T>(targetList, element, [](const T &leftSide, const T &rightSide) -> bool { return leftSide >= rightSide; }, compareEqual);
505505
}
506506

507-
// TODO: Should a list of new indices by returned instead of bool?
508-
// TODO: Create a varargs version starting with nothing and adding unique elements from all lists before returning by value, so that duplicates in the first list are also reduced.
509-
// TODO: Take a function for equality.
507+
// TODO: Create a varargs union function, starting with nothing and adding unique elements from all lists before returning by value, so that duplicates in the first list are also reduced.
508+
510509
// Pre-conditions:
511510
// * All elements in targetList must be unique, or else they will remain duplicated.
512511
// * targetList and sourceList may not refer to the same list.
513512
// Pushes all elements in sourceList that does not already exist in targetList.
514-
// Returns true iff any element was pushed to targetList.
515513
template <typename T>
516-
static inline bool list_append_unique_last(dsr::List<T> &targetList, const dsr::List<T> &sourceList) {
517-
bool result = false;
514+
static inline void list_append_unique_last(
515+
dsr::List<T> &targetList,
516+
const dsr::List<T> &sourceList,
517+
const TemporaryCallback<bool(const T &leftSide, const T &rightSide)> &compareEqual = [](const T &leftSide, const T &rightSide) -> bool { return leftSide == rightSide; }
518+
) {
518519
for (intptr_t e = 0; e < sourceList.length(); e++) {
519-
// Must store the result in a new variable to avoid lazy evaluation with side-effects.
520-
bool newResult = list_insert_unique_last<T>(targetList, sourceList[e]) != -1;
521-
result = result || newResult;
520+
list_insert_unique_last<T>(targetList, sourceList[e], compareEqual);
522521
}
523-
return result;
524522
}
525523

526-
// TODO: Create a varargs version starting with nothing and adding unique elements from all lists before returning by value, so that duplicates in the first list are also reduced.
527-
// TODO: Take functions for both equality and sorting.
528-
// TODO: Assert that the original list is sorted in debug mode.
529524
// Pre-conditions:
530525
// All elements in targetList must be unique, or else they will remain duplicated.
531526
// targetList must be sorted in ascending order.
532527
// targetList and sourceList may not refer to the same list.
533528
// Pushes all elements in sourceList that does not already exist in targetList.
534529
// Returns true iff any element was pushed to targetList.
535530
template <typename T>
536-
static inline bool list_append_unique_sorted_ascending(dsr::List<T> &targetList, const dsr::List<T> &sourceList) {
537-
bool result = false;
531+
static inline void list_append_unique_sorted_ascending(
532+
dsr::List<T> &targetList,
533+
const dsr::List<T> &sourceList,
534+
const TemporaryCallback<bool(const T &leftSide, const T &rightSide)> &compareEqual = [](const T &leftSide, const T &rightSide) -> bool { return leftSide == rightSide; }
535+
) {
538536
for (intptr_t e = 0; e < sourceList.length(); e++) {
539-
// Must store the result in a new variable to avoid lazy evaluation with side-effects.
540-
bool newResult = list_insert_unique_sorted_ascending<T>(targetList, sourceList[e]) != -1;
541-
result = result || newResult;
537+
list_insert_unique_sorted_ascending<T>(targetList, sourceList[e], compareEqual);
542538
}
543-
return result;
544539
}
545540

546541
// Pre-conditions:
@@ -550,14 +545,14 @@ static inline bool list_append_unique_sorted_ascending(dsr::List<T> &targetList,
550545
// Pushes all elements in sourceList that does not already exist in targetList.
551546
// Returns true iff any element was pushed to targetList.
552547
template <typename T>
553-
static inline bool list_append_unique_sorted_descending(dsr::List<T> &targetList, const dsr::List<T> &sourceList) {
554-
bool result = false;
548+
static inline void list_append_unique_sorted_descending(
549+
dsr::List<T> &targetList,
550+
const dsr::List<T> &sourceList,
551+
const TemporaryCallback<bool(const T &leftSide, const T &rightSide)> &compareEqual = [](const T &leftSide, const T &rightSide) -> bool { return leftSide == rightSide; }
552+
) {
555553
for (intptr_t e = 0; e < sourceList.length(); e++) {
556-
// Must store the result in a new variable to avoid lazy evaluation with side-effects.
557-
bool newResult = list_insert_unique_sorted_descending<T>(targetList, sourceList[e]) != -1;
558-
result = result || newResult;
554+
list_insert_unique_sorted_descending<T>(targetList, sourceList[e], compareEqual);
559555
}
560-
return result;
561556
}
562557

563558
// Helper function for heapSort.

Source/test/tests/ListAlgorithmTest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ START_TEST(ListAlgorithm)
202202
{
203203
List<int32_t> unsortedUnion(7, 5, 2, 4);
204204
// Nothing is inserted, because all inserted elements already exist.
205-
ASSERT_EQUAL(list_append_unique_last(unsortedUnion, List<int32_t>(5, 2)), false);
205+
list_append_unique_last(unsortedUnion, List<int32_t>(5, 2));
206206
ASSERT_EQUAL(unsortedUnion, List<int32_t>(7, 5, 2, 4));
207207
// Unique values (3 and 6) are inserted at the end.
208-
ASSERT_EQUAL(list_append_unique_last(unsortedUnion, List<int32_t>(3, 5, 6)), true);
208+
list_append_unique_last(unsortedUnion, List<int32_t>(3, 5, 6));
209209
ASSERT_EQUAL(unsortedUnion, List<int32_t>(7, 5, 2, 4, 3, 6));
210210
}
211211
{
@@ -231,10 +231,10 @@ START_TEST(ListAlgorithm)
231231
{ // Sorted unions, which are useful for comparing if two sets contain the same values.
232232
List<int32_t> sortedUnion(2, 4, 5, 7);
233233
// Nothing is inserted, because all inserted elements already exist.
234-
ASSERT_EQUAL(list_append_unique_sorted_ascending(sortedUnion, List<int32_t>(5, 2)), false);
234+
list_append_unique_sorted_ascending(sortedUnion, List<int32_t>(5, 2));
235235
ASSERT_EQUAL(sortedUnion, List<int32_t>(2, 4, 5, 7));
236236
// Unique values (3 and 6) are inserted in ascending order.
237-
ASSERT_EQUAL(list_append_unique_sorted_ascending(sortedUnion, List<int32_t>(3, 5, 6)), true);
237+
list_append_unique_sorted_ascending(sortedUnion, List<int32_t>(3, 5, 6));
238238
ASSERT_EQUAL(sortedUnion, List<int32_t>(2, 3, 4, 5, 6, 7));
239239
}
240240
// TODO: Test with custom comparison functions.

0 commit comments

Comments
 (0)