Comparator
In C++ tradition, the comparison condition is a predicate (a function that takes two arguments and returns
trueorfalseas a result of their comparison; satisfies thestd::predicateconcept), while in Python you may have seen thatsortuses a key function to compare elements. I decided to mix these two approaches and create aabl::comparatorstruct that can be used to compare elements by both key and predicate at the same time. Anyway, this still satisfies thestd::predicateconcept as it is called with two arguments.
Caution
Imports <concepts>, <type_traits>, <functional> and <utility>.
Tip
To import use #include "atl/compare/comparator.hpp".
abl::comparator
The abl::comparator struct is simply an abstraction around a key function and a predicate. It can be constructed with one of them or both. The only aim is style.
Constructing
abl::comparator can be constructed with one of the following:
- a default constructor,
std::identityas a key and astd::lesspredicate:abl::comparator<int>(); - a constructor with both a key function and a predicate, a full template should be specified:
auto square = [](const auto& a) { return a * a; };
auto leq = [](const auto& a, const auto& b) { return a <= b; };
auto comp = abl::comparator<int, decltype(square), decltype(leq)>(square, l);- a factory member function
abl::comparator<T>::makewith one or both of the key function and predicate specified:
auto comp1 = abl::comparator<int>::make(square, leq);
auto comp2 = abl::comparator<int>::make(square);
auto comp3 = abl::comparator<int>::make(leq);I personally find this syntax more readable than C++’s
std::make_pairand Co.