H
H
hleb12022-02-20 13:04:12
C++ / C#
hleb1, 2022-02-20 13:04:12

There are two functions that differ in comparison sign. How to "combine"?

There are two functions of 20 lines each. Let there be make_greaterand make_less. They differ only by the comparison sign: in one ">", in the other "<". Can they be combined somehow? I write in C++.
Obviously, I can add one more argument and just compare it, they say, if mode == ">", then the corresponding one is executed. branch and vice versa.
I just heard about templates, and other solutions became interesting.

UPD:

C++ code, a lot, but it works as it should >:)
void make_list_less(LinkedList *facelist, LinkedList *less, int pivot)
{
    LinkedListItem *tmp, *ptmp;
    tmp = facelist->first;
    ptmp = tmp;
    while(tmp) {
        if(tmp->num <= pivot) { // <-------------------- "<="
            AddLL(less, tmp->num);
            if(tmp == facelist->first) {
                facelist->first = ptmp->next;
                if(tmp == facelist->last)
                    facelist->last = ptmp->next;
                ptmp = tmp->next;
                facelist->size = facelist->size - 1;
                delete tmp;
                tmp = ptmp;
                continue;
            }
            ptmp->next = tmp->next;
            facelist->size = facelist->size - 1;
            delete tmp;
            tmp = ptmp->next;
            continue;
        }
        ptmp = tmp;
        tmp = tmp->next;
    }
}

void make_list_greater(LinkedList *facelist, LinkedList *greater, int pivot)
{
    LinkedListItem *tmp, *ptmp;
    tmp = facelist->first;
    ptmp = tmp;
    while(tmp) {
        if(tmp->num > pivot) { // <-------------------- ">"
            AddLL(greater, tmp->num);
            if(tmp == facelist->first) {
                facelist->first = ptmp->next;
                if(tmp == facelist->last)
                    facelist->last = ptmp->next;
                ptmp = tmp->next;
                facelist->size = facelist->size - 1;
                delete tmp;
                tmp = ptmp;
                continue;
            }
            ptmp->next = tmp->next;
            facelist->size = facelist->size - 1;
            delete tmp;
            tmp = ptmp->next;
            continue;
        }
        ptmp = tmp;
        tmp = tmp->next;
    }
}

The first function selects from the linked list all elements that are less than the argument passed to it int pivot, the second, on the contrary, that is greater, thereby forming another list.
Ps: I tried to master the quick sort algorithm, everything was very simple in python in the example from the book, but when I started doing it myself...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2022-02-20
@hleb1

You can pass a comparison function.

int DoSomething(int a, int b, bool compare(int a, int b)) {
  if (compare(a, b)) return a;
  else return b;
}

DoSomething(1, 3, [](int a, int b){return a < b;});  // 1
DoSomething(1, 3, [](int a, int b){return a > b;});  // 3

You can also pass a comparison function to the standard std::sort function, so that it sorts in descending instead of ascending, or somehow cleverly compares objects.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question